mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-05 01:19:45 +08:00
ff88851c4c
* chore: bump select related version * test: Fix test case * chore: bump rc-select version * test: Update snapshot * chore: Update Select ts * test: Update snapshot * test: Update snapshot * test: Update snapshot * chore: Update ts definition * chore: fix internal ts def * test: update auto complete snapshot * chore: update snapshot * chore: bump version * chore: fix demo ts * chore: Move ValueType to the first place * test: Update test case
107 lines
2.6 KiB
Markdown
107 lines
2.6 KiB
Markdown
---
|
|
order: 12
|
|
title:
|
|
zh-CN: 搜索用户
|
|
en-US: Search and Select Users
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
一个带有远程搜索,防抖控制,请求时序控制,加载状态的多选示例。
|
|
|
|
## en-US
|
|
|
|
A complete multiple select sample with remote search, debounce fetch, ajax callback order flow, and loading state.
|
|
|
|
```tsx
|
|
import { Select, Spin } from 'antd';
|
|
import { SelectProps } from 'antd/es/select';
|
|
import debounce from 'lodash/debounce';
|
|
|
|
export interface DebounceSelectProps<ValueType = any>
|
|
extends Omit<SelectProps<ValueType>, 'options' | 'children'> {
|
|
fetchOptions: (search: string) => Promise<ValueType[]>;
|
|
debounceTimeout?: number;
|
|
}
|
|
|
|
function DebounceSelect<
|
|
ValueType extends { key?: string; label: React.ReactNode; value: string | number } = any,
|
|
>({ fetchOptions, debounceTimeout = 800, ...props }: DebounceSelectProps) {
|
|
const [fetching, setFetching] = React.useState(false);
|
|
const [options, setOptions] = React.useState<ValueType[]>([]);
|
|
const fetchRef = React.useRef(0);
|
|
|
|
const debounceFetcher = React.useMemo(() => {
|
|
const loadOptions = (value: string) => {
|
|
fetchRef.current += 1;
|
|
const fetchId = fetchRef.current;
|
|
setOptions([]);
|
|
setFetching(true);
|
|
|
|
fetchOptions(value).then(newOptions => {
|
|
if (fetchId !== fetchRef.current) {
|
|
// for fetch callback order
|
|
return;
|
|
}
|
|
|
|
setOptions(newOptions);
|
|
setFetching(false);
|
|
});
|
|
};
|
|
|
|
return debounce(loadOptions, debounceTimeout);
|
|
}, [fetchOptions, debounceTimeout]);
|
|
|
|
return (
|
|
<Select<ValueType>
|
|
labelInValue
|
|
filterOption={false}
|
|
onSearch={debounceFetcher}
|
|
notFoundContent={fetching ? <Spin size="small" /> : null}
|
|
{...props}
|
|
options={options}
|
|
/>
|
|
);
|
|
}
|
|
|
|
// Usage of DebounceSelect
|
|
interface UserValue {
|
|
label: string;
|
|
value: string;
|
|
}
|
|
|
|
async function fetchUserList(username: string): Promise<UserValue[]> {
|
|
console.log('fetching user', username);
|
|
|
|
return fetch('https://randomuser.me/api/?results=5')
|
|
.then(response => response.json())
|
|
.then(body =>
|
|
body.results.map(
|
|
(user: { name: { first: string; last: string }; login: { username: string } }) => ({
|
|
label: `${user.name.first} ${user.name.last}`,
|
|
value: user.login.username,
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
const Demo = () => {
|
|
const [value, setValue] = React.useState<UserValue[]>([]);
|
|
|
|
return (
|
|
<DebounceSelect
|
|
mode="multiple"
|
|
value={value}
|
|
placeholder="Select users"
|
|
fetchOptions={fetchUserList}
|
|
onChange={newValue => {
|
|
setValue(newValue);
|
|
}}
|
|
style={{ width: '100%' }}
|
|
/>
|
|
);
|
|
};
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
```
|