@@ -8,8 +8,10 @@ import {
88 useEffect ,
99 useRef ,
1010 useState ,
11- useMemo
11+ useMemo ,
12+ useCallback
1213} from 'react'
14+ import { XIcon } from '@heroicons/react/outline'
1315import { ChevronDownIcon } from '@heroicons/react/solid'
1416import { StyleObject } from 'lib/styles'
1517import { composeClasses } from 'lib/classes'
@@ -72,9 +74,8 @@ function MultiSelect({
7274 const selectRef = useRef < HTMLInputElement > ( null )
7375 const [ isOpen , setIsOpen ] = useState ( false )
7476 const [ selectedOptions , setSelectedOptions ] = useState < IMultiSelectOption [ ] > (
75- [ ]
77+ optionsList . filter ( ( opt ) => ! opt . disabled )
7678 )
77-
7879 const [ options , setOptions ] = useState < IMultiSelectOption [ ] > ( optionsList )
7980
8081 const styles = {
@@ -94,10 +95,10 @@ function MultiSelect({
9495 )
9596 }
9697
97- const handleClick = ( ) => {
98+ const handleClick = useCallback ( ( ) => {
9899 if ( isDisabled ) return
99100 setIsOpen ( ! isOpen )
100- }
101+ } , [ isDisabled , isOpen , setIsOpen ] )
101102
102103 const handleOptionToggle = ( option : IMultiSelectOption ) => {
103104 if ( option . disabled ) return
@@ -119,13 +120,20 @@ function MultiSelect({
119120 . every ( ( opt ) => selectedOptions . some ( ( sel ) => sel . value === opt . value ) ) ,
120121 [ options , selectedOptions ]
121122 )
122- const handleSelectAll = ( ) => {
123+ const handleSelectAll = useCallback ( ( ) => {
123124 allSelected
124125 ? setSelectedOptions ( [ ] )
125126 : setSelectedOptions ( options . filter ( ( opt ) => ! opt . disabled ) )
126- }
127+ } , [ allSelected , options , setSelectedOptions ] )
127128
128- const handleClear = ( ) => setSelectedOptions ( [ ] )
129+ const handleClear = useCallback ( ( ) => {
130+ setSelectedOptions ( [ ] )
131+ } , [ setSelectedOptions ] )
132+
133+ const onSubmit = useCallback ( ( ) => {
134+ buttonSubmit ?. onClick ( )
135+ setIsOpen ( false )
136+ } , [ buttonSubmit , setIsOpen ] )
129137
130138 useEffect ( ( ) => {
131139 const handleClickOutside = ( e : globalThis . MouseEvent ) => {
@@ -153,7 +161,9 @@ function MultiSelect({
153161 role = "select-container"
154162 className = { composeClasses (
155163 styles . container ,
156- selectedOptions . length > 0 && 'bg-blue-100 border border-blue-600'
164+ selectedOptions . length > 0 &&
165+ ! allSelected &&
166+ 'bg-blue-100 border border-blue-600'
157167 ) }
158168 style = { { zIndex : 2 , ...style } }
159169 onClick = { handleClick }
@@ -180,19 +190,23 @@ function MultiSelect({
180190 ) }
181191 </ div >
182192 </ div >
183- < Transition
184- alwaysRender
185- show = { isOpen && ! isDisabled }
186- animationStart = "rotateRight"
187- animationEnd = "rotateRightBack"
188- duration = { 200 }
189- >
190- < ChevronDownIcon
191- role = "chevron"
192- className = "text-gray-400"
193- width = { 20 }
194- />
195- </ Transition >
193+ { selectedOptions . length > 0 && ! allSelected ? (
194+ < XIcon onClick = { handleClear } className = "text-gray-400" width = { 30 } />
195+ ) : (
196+ < Transition
197+ alwaysRender
198+ show = { isOpen && ! isDisabled }
199+ animationStart = "rotateRight"
200+ animationEnd = "rotateRightBack"
201+ duration = { 200 }
202+ >
203+ < ChevronDownIcon
204+ role = "chevron"
205+ className = "text-gray-400"
206+ width = { 20 }
207+ />
208+ </ Transition >
209+ ) }
196210 </ div >
197211 { isOpen && ! isDisabled && (
198212 < Transition
@@ -241,7 +255,7 @@ function MultiSelect({
241255 >
242256 { buttonClear ?. label }
243257 </ Button >
244- < Button onClick = { buttonSubmit ?. onClick } className = "px-8" >
258+ < Button onClick = { onSubmit } className = "px-8" >
245259 { buttonSubmit ?. label }
246260 </ Button >
247261 </ Flex >
0 commit comments