forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-box.vue
84 lines (72 loc) · 1.58 KB
/
search-box.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<docs>
---
order: 8
title:
zh-CN: 搜索框
en-US: Search Box
---
## zh-CN
搜索和远程数据结合。
## en-US
Search with remote data.
</docs>
<template>
<a-select
v-model:value="value"
show-search
placeholder="input search text"
style="width: 200px"
:default-active-first-option="false"
:show-arrow="false"
:filter-option="false"
:not-found-content="null"
:options="data"
@search="handleSearch"
@change="handleChange"
></a-select>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import jsonp from 'fetch-jsonp';
let timeout: any;
let currentValue = '';
function fetch(value: string, callback: any) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
currentValue = value;
function fake() {
const params = new URLSearchParams({
code: 'utf-8',
q: value,
});
jsonp(`https://suggest.taobao.com/sug?${params}`)
.then(response => response.json())
.then(d => {
if (currentValue === value) {
const result = d.result;
const data: any[] = [];
result.forEach((r: any) => {
data.push({
value: r[0],
label: r[0],
});
});
callback(data);
}
});
}
timeout = setTimeout(fake, 300);
}
const data = ref<any[]>([]);
const value = ref();
const handleSearch = (val: string) => {
fetch(val, (d: any[]) => (data.value = d));
};
const handleChange = (val: string) => {
console.log(val);
value.value = val;
fetch(val, (d: any[]) => (data.value = d));
};
</script>