@@ -21,6 +21,7 @@ import { CHECKBOX_VALUE } from '@Common/Types'
21
21
import { Checkbox } from '@Common/Checkbox'
22
22
import { ReactSelectInputAction } from '@Common/Constants'
23
23
import { SelectPickerOptionType , SelectPickerProps } from './type'
24
+ import { getGroupCheckboxValue } from './utils'
24
25
25
26
export const SelectPickerDropdownIndicator = ( props : DropdownIndicatorProps < SelectPickerOptionType > ) => {
26
27
const { isDisabled } = props
@@ -207,24 +208,16 @@ export const SelectPickerGroupHeading = ({
207
208
const selectedOptions = ( selectProps . value as MultiValue < SelectPickerOptionType > ) ?? [ ]
208
209
const groupHeadingOptions = data . options ?? [ ]
209
210
210
- const selectedOptionsMapByValue = selectedOptions . reduce < Record < string , true > > ( ( acc , option ) => {
211
- acc [ selectProps . getOptionValue ( option ) ] = true
212
- return acc
213
- } , { } )
214
-
215
- const isGroupSelected =
216
- isGroupHeadingSelectable &&
217
- groupHeadingOptions
218
- . map ( ( option ) => selectProps . getOptionValue ( option ) )
219
- . every ( ( value ) => selectedOptionsMapByValue [ value ] )
211
+ const checkboxValue = getGroupCheckboxValue ( groupHeadingOptions , selectedOptions , selectProps . getOptionValue )
220
212
221
213
const toggleGroupHeadingSelection = ( ) => {
222
214
const groupOptionsMapByValue = groupHeadingOptions . reduce < Record < string , true > > ( ( acc , option ) => {
223
215
acc [ selectProps . getOptionValue ( option ) ] = true
224
216
return acc
225
217
} , { } )
226
218
227
- if ( isGroupSelected ) {
219
+ // Clear all the selection(s) in the group if any of the option is selected
220
+ if ( checkboxValue ) {
228
221
selectProps ?. onChange ?.(
229
222
selectedOptions . filter (
230
223
( selectedOption ) => ! groupOptionsMapByValue [ selectProps . getOptionValue ( selectedOption ) ] ,
@@ -238,6 +231,7 @@ export const SelectPickerGroupHeading = ({
238
231
return
239
232
}
240
233
234
+ // Select all options
241
235
selectProps ?. onChange ?.(
242
236
[
243
237
...selectedOptions . filter (
@@ -267,8 +261,8 @@ export const SelectPickerGroupHeading = ({
267
261
< Checkbox
268
262
onChange = { noop }
269
263
onClick = { handleToggleCheckbox }
270
- isChecked = { isGroupSelected }
271
- value = { CHECKBOX_VALUE . CHECKED }
264
+ isChecked = { ! ! checkboxValue }
265
+ value = { checkboxValue }
272
266
rootClassName = "mb-0 w-20 p-2 dc__align-self-start dc__no-shrink"
273
267
/>
274
268
) }
0 commit comments