2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 9
|
2016-10-06 18:54:28 +08:00
|
|
|
title:
|
2016-07-10 08:55:43 +08:00
|
|
|
zh-CN: 搜索框
|
|
|
|
en-US: Search Box
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2016-07-10 08:55:43 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2018-06-19 14:37:35 +08:00
|
|
|
搜索和远程数据结合。
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2016-07-10 08:55:43 +08:00
|
|
|
## en-US
|
|
|
|
|
2018-06-19 14:37:35 +08:00
|
|
|
Search with remote data.
|
2016-07-10 08:55:43 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-09-19 18:01:16 +08:00
|
|
|
import React, { useState } from 'react';
|
2017-01-05 15:47:19 +08:00
|
|
|
import { Select } from 'antd';
|
2016-12-19 17:54:25 +08:00
|
|
|
import jsonp from 'fetch-jsonp';
|
2021-12-03 23:54:19 +08:00
|
|
|
import qs from 'qs';
|
2022-09-19 18:01:16 +08:00
|
|
|
import type { SelectProps } from 'antd';
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
let timeout: ReturnType<typeof setTimeout> | null;
|
|
|
|
let currentValue: string;
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2022-09-19 18:01:16 +08:00
|
|
|
const fetch = (value: string, callback: Function) => {
|
2016-02-24 15:11:50 +08:00
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = null;
|
|
|
|
}
|
|
|
|
currentValue = value;
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const fake = () => {
|
2021-12-03 23:54:19 +08:00
|
|
|
const str = qs.stringify({
|
2016-02-24 15:11:50 +08:00
|
|
|
code: 'utf-8',
|
|
|
|
q: value,
|
|
|
|
});
|
2016-12-19 17:54:25 +08:00
|
|
|
jsonp(`https://suggest.taobao.com/sug?${str}`)
|
2022-05-19 09:46:26 +08:00
|
|
|
.then((response: any) => response.json())
|
|
|
|
.then((d: any) => {
|
2016-12-19 17:54:25 +08:00
|
|
|
if (currentValue === value) {
|
2019-06-19 19:09:08 +08:00
|
|
|
const { result } = d;
|
2022-05-19 09:46:26 +08:00
|
|
|
const data = result.map((item: any) => ({
|
|
|
|
value: item[0],
|
|
|
|
text: item[0],
|
|
|
|
}));
|
2016-12-19 17:54:25 +08:00
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
});
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
2016-02-24 15:11:50 +08:00
|
|
|
|
|
|
|
timeout = setTimeout(fake, 300);
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const SearchInput: React.FC<{ placeholder: string; style: React.CSSProperties }> = props => {
|
2022-09-19 18:01:16 +08:00
|
|
|
const [data, setData] = useState<SelectProps['options']>([]);
|
2022-05-19 09:46:26 +08:00
|
|
|
const [value, setValue] = useState<string>();
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleSearch = (newValue: string) => {
|
|
|
|
if (newValue) {
|
|
|
|
fetch(newValue, setData);
|
2019-08-12 18:09:32 +08:00
|
|
|
} else {
|
2022-05-19 09:46:26 +08:00
|
|
|
setData([]);
|
2019-08-12 18:09:32 +08:00
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 16:14:14 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleChange = (newValue: string) => {
|
|
|
|
setValue(newValue);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
showSearch
|
|
|
|
value={value}
|
|
|
|
placeholder={props.placeholder}
|
|
|
|
style={props.style}
|
|
|
|
defaultActiveFirstOption={false}
|
|
|
|
showArrow={false}
|
|
|
|
filterOption={false}
|
|
|
|
onSearch={handleSearch}
|
|
|
|
onChange={handleChange}
|
|
|
|
notFoundContent={null}
|
2022-09-19 18:01:16 +08:00
|
|
|
options={(data || []).map(d => ({
|
|
|
|
value: d.value,
|
|
|
|
label: d.text,
|
|
|
|
}))}
|
|
|
|
/>
|
2022-05-19 09:46:26 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const App: React.FC = () => <SearchInput placeholder="input search text" style={{ width: 200 }} />;
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|