From 1ccfa744b3e6f79b03400d4b0a0cbba7848d61c6 Mon Sep 17 00:00:00 2001 From: wangning Date: Sat, 19 Nov 2022 12:11:46 +0800 Subject: [PATCH] feat: reset and force update --- components/mentions/demo/async.md | 60 ++++++++++++++++++++++++++ components/mentions/demo/async.tsx | 31 +++++++------ components/mentions/demo/autoSize.tsx | 25 ++++++++--- components/mentions/demo/basic.tsx | 26 +++++++---- components/mentions/demo/form.tsx | 47 +++++++++++++++----- components/mentions/demo/placement.tsx | 25 ++++++++--- components/mentions/demo/prefix.tsx | 15 +++---- components/mentions/demo/readonly.tsx | 31 +++++++------ components/mentions/demo/status.tsx | 47 ++++++++++++-------- components/mentions/index.en-US.md | 23 ++++++++-- components/mentions/index.tsx | 34 ++++++++------- components/mentions/index.zh-CN.md | 29 ++++++++++--- package.json | 2 +- 13 files changed, 284 insertions(+), 111 deletions(-) diff --git a/components/mentions/demo/async.md b/components/mentions/demo/async.md index da9d1ffefe3f..33bf9f02bd51 100644 --- a/components/mentions/demo/async.md +++ b/components/mentions/demo/async.md @@ -6,6 +6,66 @@ async +```tsx +import { Mentions } from 'antd'; +import debounce from 'lodash/debounce'; +import React, { useCallback, useRef, useState } from 'react'; + +const App: React.FC = () => { + const [loading, setLoading] = useState(false); + const [users, setUsers] = useState<{ login: string; avatar_url: string }[]>([]); + const ref = useRef(); + + const loadGithubUsers = (key: string) => { + if (!key) { + setUsers([]); + return; + } + + fetch(`https://api.github.com/search/users?q=${key}`) + .then(res => res.json()) + .then(({ options = [] }) => { + if (ref.current !== key) return; + + setLoading(false); + setUsers(options.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 ( + ({ + key: login, + value: login, + className: 'antd-demo-dynamic-option', + label: ( + <> + {login} + {login} + + ), + }))} + /> + ); +}; + +export default App; +``` +