|
|
@ -1,19 +1,23 @@ |
|
|
|
import { Select, Spin } from 'antd'; |
|
|
|
import { SelectProps } from 'antd/es/select'; |
|
|
|
import type { SelectProps } from 'antd/es/select'; |
|
|
|
import debounce from 'lodash/debounce'; |
|
|
|
import React from 'react'; |
|
|
|
import { useEffect } from '../../.umi/.cache/.mfsu/mf-dep_vendors-node_modules_react_index_js.598bdd42.async'; |
|
|
|
|
|
|
|
export interface DebounceSelectProps<ValueType = any> |
|
|
|
export interface DebounceSelectProps<ValueType> |
|
|
|
extends Omit<SelectProps<ValueType>, 'options' | 'children'> { |
|
|
|
fetchOptions: (search: string) => Promise<ValueType[]>; |
|
|
|
fetchOptions: (search: string) => Promise<ValueType[] | undefined>; |
|
|
|
debounceTimeout?: number; |
|
|
|
initOption?: string; |
|
|
|
} |
|
|
|
|
|
|
|
function DebounceSelect< |
|
|
|
ValueType extends { key?: string; label: React.ReactNode; value: string | number } = any, |
|
|
|
>({ fetchOptions, debounceTimeout = 800, initOption, ...props }: DebounceSelectProps) { |
|
|
|
export type ValueType = { key?: number; label?: string; value?: number }; |
|
|
|
|
|
|
|
function DebounceSelect({ |
|
|
|
fetchOptions, |
|
|
|
debounceTimeout = 800, |
|
|
|
initOption, |
|
|
|
...props |
|
|
|
}: DebounceSelectProps<ValueType>) { |
|
|
|
const [fetching, setFetching] = React.useState(false); |
|
|
|
const initOptions = []; |
|
|
|
let defaultValue = null; |
|
|
@ -27,22 +31,18 @@ function DebounceSelect< |
|
|
|
const fetchRef = React.useRef(0); |
|
|
|
|
|
|
|
const debounceFetcher = React.useMemo(() => { |
|
|
|
const loadOptions = (value: string | number) => { |
|
|
|
const loadOptions = (value: string) => { |
|
|
|
fetchRef.current += 1; |
|
|
|
const fetchId = fetchRef.current; |
|
|
|
setOptions([]); |
|
|
|
setFetching(true); |
|
|
|
|
|
|
|
fetchOptions({ query: value }).then((newOptions) => { |
|
|
|
fetchOptions(value).then((newOptions) => { |
|
|
|
if (fetchId !== fetchRef.current) { |
|
|
|
// for fetch callback order
|
|
|
|
return; |
|
|
|
} |
|
|
|
const list = newOptions.data.list.map((user: { id: number; name: string }) => ({ |
|
|
|
label: user.name, |
|
|
|
value: user.id, |
|
|
|
})); |
|
|
|
setOptions(list); |
|
|
|
setOptions(newOptions); |
|
|
|
setFetching(false); |
|
|
|
}); |
|
|
|
}; |
|
|
|