@@ -61,7 +61,7 @@ export default function KernDropdown(props: KernDropdownProps) {
6161 setDropdownCaptions ( prepareOptions ) ;
6262 setSearchIndexes ( null ) ;
6363 }
64- } , [ props . options , searchText , selectedCheckboxes , props . hasSearchBar , props . hasCheckboxes , props . selectedCheckboxes , props . hasSelectAll , props . valuePropertyPath ] ) ;
64+ } , [ props . options , searchText , props . hasSearchBar , props . hasCheckboxes , props . valuePropertyPath , props . selectedCheckboxes , props . hasSelectAll ] ) ;
6565
6666 useEffect ( ( ) => {
6767 if ( ! props . disabledOptions || ! props . options ) return ;
@@ -94,8 +94,7 @@ export default function KernDropdown(props: KernDropdownProps) {
9494 return { "maxHeight" : `${ maxHeight } rem` , "overflowY" : "auto" } ;
9595 } , [ props . scrollAfterNOptions ] ) ;
9696
97- function setOptionsWithCheckboxes ( options : any [ ] ) {
98- if ( selectedCheckboxes . length > 0 ) return ;
97+ const setOptionsWithCheckboxes = useCallback ( ( options : any [ ] ) => {
9998 const newSelectedCheckboxes = options . map ( ( option : any , index : number ) => {
10099 return {
101100 name : option ,
@@ -105,25 +104,27 @@ export default function KernDropdown(props: KernDropdownProps) {
105104 if ( props . hasSelectAll ) {
106105 newSelectedCheckboxes . push ( {
107106 name : SELECT_ALL ,
108- checked : false
107+ checked : newSelectedCheckboxes . every ( ( checkbox ) => checkbox . checked )
109108 } ) ;
110109 }
111110 setSelectedCheckboxes ( newSelectedCheckboxes ) ;
112111 setDropdownCaptions ( newSelectedCheckboxes . map ( ( option : any ) => option . name ) ) ;
113- }
112+ } , [ props . hasSelectAll , props . selectedCheckboxes ] ) ;
114113
115114 function toggleDropdown ( ) {
116115 if ( isDisabled && ! props . hasCheckboxes ) return ; // if the dropdown has checkboxes, it shouldn't be disabled because the user can still select options
117116 if ( isOpen && props . keepDrownOpen ) return ;
118117 setIsOpen ( ! isOpen ) ;
119118 }
120119
121- function handleSelectedCheckboxes ( option : string , index : number , e : any ) {
120+ const handleSelectedCheckboxes = useCallback ( ( option : string , index : number , e : any ) => {
122121 let newSelectedCheckboxes = [ ...selectedCheckboxes ] ;
123122 if ( option == SELECT_ALL ) {
124123 newSelectedCheckboxes . forEach ( ( checkbox ) => {
125124 checkbox . checked = e . target . checked ;
126125 } ) ;
126+ const allSelected = newSelectedCheckboxes . every ( ( checkbox ) => checkbox . checked ) ;
127+ newSelectedCheckboxes [ newSelectedCheckboxes . length - 1 ] . checked = allSelected ;
127128 } else {
128129 const lastIdx = newSelectedCheckboxes . length - 1 ;
129130 if ( props . hasSelectAll && newSelectedCheckboxes [ lastIdx ] . checked ) {
@@ -136,7 +137,7 @@ export default function KernDropdown(props: KernDropdownProps) {
136137 newSelectedCheckboxes = newSelectedCheckboxes . filter ( ( checkbox ) => checkbox . name != SELECT_ALL ) ;
137138 }
138139 props . selectedOption ( newSelectedCheckboxes ) ;
139- }
140+ } , [ selectedCheckboxes , props . hasSelectAll ] ) ;
140141
141142 function handleSelectedCheckboxesThreeStates ( index : number ) {
142143 const optionSave = { ...props . options [ index ] } ;
@@ -161,8 +162,7 @@ export default function KernDropdown(props: KernDropdownProps) {
161162 setSavedIndex ( index ) ;
162163 }
163164
164-
165- function performActionOnClick ( option : string , index : number ) {
165+ const performActionOnClick = useCallback ( ( option : string , index : number ) => {
166166 if ( props . hasCheckboxes ) {
167167 handleSelectedCheckboxes ( option , index , { target : { checked : ! selectedCheckboxes [ index ] . checked } } ) ;
168168 return ;
@@ -180,8 +180,9 @@ export default function KernDropdown(props: KernDropdownProps) {
180180 props . selectedOption ( props . options [ index ] ) ;
181181 }
182182 setIsOpen ( false ) ;
183+ setSelectedCheckboxes ( [ ] ) ;
183184 }
184- }
185+ } , [ props , selectedCheckboxes , searchIndexes ] ) ;
185186
186187 return (
187188 < Menu ref = { dropdownRef } as = "div" className = { `relative inline-block text-left ${ props . dropdownWidth ?? 'w-full' } ${ props . dropdownClasses ?? '' } ${ props . fontClass ?? '' } ` } >
@@ -213,8 +214,8 @@ export default function KernDropdown(props: KernDropdownProps) {
213214 </ Menu . Button >
214215 ) : ( < Menu . Button onClick = { toggleDropdown } className = { `inline-flex w-full justify-between items-center rounded-md border border-gray-300 px-4 py-2 text-sm font-semibold text-gray-700 shadow-sm focus:outline-none focus:ring-2
215216 focus:ring-gray-300 focus:ring-offset-2 focus:ring-offset-gray-100 disabled:opacity-50 disabled:cursor-not-allowed ${ props . buttonClasses ?? '' } ${ props . buttonCaptionBgColor ?? 'bg-white hover:bg-gray-50' } ` }
216- disabled = { isDisabled && ! props . hasCheckboxes } >
217- < div className = ' flex items-center gap-x-1' >
217+ disabled = { isDisabled } >
218+ < div className = { ` flex items-center gap-x-1 ${ props . truncateButtonName ? 'max-w-[300px] truncate' : '' } ` } >
218219 { props . buttonPrefixIcon }
219220 { ! props . hasCheckboxesThreeStates && props . buttonName }
220221 </ div >
@@ -267,7 +268,7 @@ export default function KernDropdown(props: KernDropdownProps) {
267268 if ( ! props . optionsHaveHoverBox ) return ;
268269 setHoverBoxPosition ( null ) ;
269270 } } >
270- { props . hasCheckboxes && < input checked = { selectedCheckboxes [ index ] . checked } name = "option" type = "checkbox" className = "mr-3 cursor-pointer"
271+ { props . hasCheckboxes && < input checked = { ! ! selectedCheckboxes [ index ] ? .checked } name = "option" type = "checkbox" className = "mr-3 cursor-pointer"
271272 onChange = { ( e ) => handleSelectedCheckboxes ( option , index , e ) } /> }
272273 { props . hasCheckboxesThreeStates && < div className = "h-4 w-4 border-gray-300 mr-3 border rounded hover:bg-gray-200 min-w-4"
273274 style = { { backgroundColor : getActiveNegateGroupColor ( props . options [ index ] ) , borderColor : getActiveNegateGroupColor ( props . options [ index ] ) } } >
0 commit comments