|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | /* eslint-disable no-nested-ternary */ |
21 | | -import { FC, useState, useEffect, useRef } from 'react'; |
| 21 | +import { FC, useState, useEffect, useRef, useCallback } from 'react'; |
22 | 22 | import { Dropdown, Button, Form } from 'react-bootstrap'; |
23 | 23 | import { useTranslation } from 'react-i18next'; |
24 | 24 |
|
| 25 | +import debounce from 'lodash/debounce'; |
25 | 26 | import { marked } from 'marked'; |
26 | 27 | import classNames from 'classnames'; |
27 | 28 |
|
@@ -69,7 +70,7 @@ const TagSelector: FC<IProps> = ({ |
69 | 70 | const [currentIndex, setCurrentIndex] = useState<number>(0); |
70 | 71 | const [repeatIndex, setRepeatIndex] = useState(-1); |
71 | 72 | const [searchValue, setSearchValue] = useState<string>(''); |
72 | | - const [tags, setTags] = useState<Type.Tag[] | null>(null); |
| 73 | + const [tags, setTags] = useState<Type.Tag[] | null>([]); |
73 | 74 | const [requiredTags, setRequiredTags] = useState<Type.Tag[] | null>(null); |
74 | 75 | const { t } = useTranslation('translation', { keyPrefix: 'tag_selector' }); |
75 | 76 | const { data: userPermission } = useUserPermission('tag.add'); |
@@ -146,20 +147,23 @@ const TagSelector: FC<IProps> = ({ |
146 | 147 | handleMenuShow(false); |
147 | 148 | }; |
148 | 149 |
|
149 | | - const fetchTags = (str) => { |
150 | | - if (!showRequiredTag && !str) { |
151 | | - setTags([]); |
152 | | - return; |
153 | | - } |
154 | | - queryTags(str).then((res) => { |
155 | | - const tagArray: Type.Tag[] = filterTags(res || []); |
156 | | - if (str === '') { |
157 | | - setRequiredTags(res); |
| 150 | + const fetchTags = useCallback( |
| 151 | + debounce((str) => { |
| 152 | + if (!showRequiredTag && !str) { |
| 153 | + setTags([]); |
| 154 | + return; |
158 | 155 | } |
159 | | - handleMenuShow(tagArray.length > 0); |
160 | | - setTags(tagArray); |
161 | | - }); |
162 | | - }; |
| 156 | + queryTags(str).then((res) => { |
| 157 | + const tagArray: Type.Tag[] = filterTags(res || []); |
| 158 | + if (str === '') { |
| 159 | + setRequiredTags(res); |
| 160 | + } |
| 161 | + handleMenuShow(tagArray.length > 0); |
| 162 | + setTags(tagArray); |
| 163 | + }); |
| 164 | + }, 400), |
| 165 | + [], |
| 166 | + ); |
163 | 167 |
|
164 | 168 | const resetSearch = () => { |
165 | 169 | setCurrentIndex(0); |
|
0 commit comments