1
- import { useCallback , useMemo , useState } from 'react'
1
+ import { useCallback , useEffect , useMemo , useState } from 'react'
2
2
import ReactSelect , { MenuListProps , ValueContainerProps } from 'react-select'
3
3
import { Option , OptionType } from '../../../Common'
4
4
import { LoadingIndicator , MenuListWithApplyButton , MultiSelectValueContainer } from '../ReactSelect'
@@ -14,10 +14,13 @@ const PluginTagSelect = ({
14
14
hasError,
15
15
reloadTags,
16
16
} : PluginTagSelectProps ) => {
17
+ const getInitialSelectedOptions = ( ) => selectedTags ?. map ( ( tag ) => ( { label : tag , value : tag } ) ) ?? [ ]
17
18
const [ isMenuOpen , setIsMenuOpen ] = useState < boolean > ( false )
18
- const [ localSelectedOptions , setLocalSelectedOptions ] = useState < OptionType [ ] > (
19
- selectedTags ?. map ( ( tag ) => ( { label : tag , value : tag } ) ) ?? [ ] ,
20
- )
19
+ const [ localSelectedOptions , setLocalSelectedOptions ] = useState < OptionType [ ] > ( getInitialSelectedOptions ( ) )
20
+
21
+ useEffect ( ( ) => {
22
+ setLocalSelectedOptions ( getInitialSelectedOptions ( ) )
23
+ } , [ selectedTags ] )
21
24
22
25
const tagOptions : OptionType [ ] = useMemo (
23
26
( ) => availableTags ?. map ( ( tag ) => ( { label : tag , value : tag } ) ) || [ ] ,
@@ -34,7 +37,7 @@ const PluginTagSelect = ({
34
37
35
38
const handleMenuClose = ( ) => {
36
39
setIsMenuOpen ( false )
37
- setLocalSelectedOptions ( selectedTags ?. map ( ( tag ) => ( { label : tag , value : tag } ) ) ?? [ ] )
40
+ setLocalSelectedOptions ( getInitialSelectedOptions ( ) )
38
41
}
39
42
40
43
const renderNoOptionsMessage = ( ) => {
@@ -44,16 +47,19 @@ const PluginTagSelect = ({
44
47
return < p className = "m-0 cn-7 fs-13 fw-4 lh-20 py-6 px-8" > No tags found</ p >
45
48
}
46
49
47
- const renderMenuList = useCallback ( ( props : MenuListProps < OptionType , true > ) => {
48
- const selectedOptions = props . getValue ( ) || [ ]
50
+ const renderMenuList = useCallback (
51
+ ( props : MenuListProps < OptionType , true > ) => {
52
+ const selectedOptions = props . getValue ( ) || [ ]
49
53
50
- const handleApplyFilters = ( ) => {
51
- handleMenuClose ( )
52
- handleUpdateSelectedTags ( selectedOptions . map ( ( option ) => option . value ) )
53
- }
54
+ const handleApplyFilters = ( ) => {
55
+ handleMenuClose ( )
56
+ handleUpdateSelectedTags ( selectedOptions . map ( ( option ) => option . value ) )
57
+ }
54
58
55
- return < MenuListWithApplyButton { ...props } handleApplyFilter = { handleApplyFilters } />
56
- } , [ ] )
59
+ return < MenuListWithApplyButton { ...props } handleApplyFilter = { handleApplyFilters } />
60
+ } ,
61
+ [ handleUpdateSelectedTags ] ,
62
+ )
57
63
58
64
const renderValueContainer = useCallback (
59
65
( props : ValueContainerProps < OptionType , true > ) => < MultiSelectValueContainer { ...props } title = "Category" /> ,
0 commit comments