mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-03 04:30:06 +08:00
502dac12aa
* docs: fix code * feat: lint * feat: prettier * feat: test * feat: review * feat: format html * feat: format html
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import React, { useCallback, useRef, useState } from 'react';
|
|
import { Mentions } from 'antd';
|
|
import debounce from 'lodash/debounce';
|
|
|
|
const App: React.FC = () => {
|
|
const [loading, setLoading] = useState(false);
|
|
const [users, setUsers] = useState<{ login: string; avatar_url: string }[]>([]);
|
|
const ref = useRef<string>();
|
|
|
|
const loadGithubUsers = (key: string) => {
|
|
if (!key) {
|
|
setUsers([]);
|
|
return;
|
|
}
|
|
|
|
fetch(`https://api.github.com/search/users?q=${key}`)
|
|
.then((res) => res.json())
|
|
.then(({ items = [] }) => {
|
|
if (ref.current !== key) return;
|
|
|
|
setLoading(false);
|
|
setUsers(items.slice(0, 10));
|
|
});
|
|
};
|
|
|
|
const debounceLoadGithubUsers = useCallback(debounce(loadGithubUsers, 800), []);
|
|
|
|
const onSearch = (search: string) => {
|
|
console.log('Search:', search);
|
|
ref.current = search;
|
|
setLoading(!!search);
|
|
setUsers([]);
|
|
|
|
debounceLoadGithubUsers(search);
|
|
};
|
|
|
|
return (
|
|
<Mentions
|
|
style={{ width: '100%' }}
|
|
loading={loading}
|
|
onSearch={onSearch}
|
|
options={users.map(({ login, avatar_url: avatar }) => ({
|
|
key: login,
|
|
value: login,
|
|
className: 'antd-demo-dynamic-option',
|
|
label: (
|
|
<>
|
|
<img src={avatar} alt={login} />
|
|
<span>{login}</span>
|
|
</>
|
|
),
|
|
}))}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default App;
|