-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #692 from oceanbase/dengfuping-dev
demo(design): Add remote search demo for Select
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useMemo, useRef, useState } from 'react'; | ||
import { Select, Spin } from '@oceanbase/design'; | ||
import type { SelectProps } from '@oceanbase/design'; | ||
import debounce from 'lodash/debounce'; | ||
|
||
export interface DebounceSelectProps<ValueType = any> | ||
extends Omit<SelectProps<ValueType | 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<ValueType>) { | ||
const [fetching, setFetching] = useState(false); | ||
const [options, setOptions] = useState<ValueType[]>([]); | ||
const fetchRef = useRef(0); | ||
|
||
const debounceFetcher = 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 | ||
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 App: React.FC = () => { | ||
const [value, setValue] = useState<UserValue[]>([]); | ||
|
||
return ( | ||
<DebounceSelect | ||
mode="multiple" | ||
value={value} | ||
placeholder="Select users" | ||
fetchOptions={fetchUserList} | ||
onChange={newValue => { | ||
setValue(newValue as UserValue[]); | ||
}} | ||
style={{ width: '100%' }} | ||
/> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters