@@ -198,6 +198,10 @@ type PickerProps<T extends object> = {
198
198
* Whether the picker should be searchable. If true, renders a search input.
199
199
*/
200
200
searchable ?: boolean ;
201
+ /**
202
+ * Initial state for group toggles. If provided, groups will start with these states instead of all being disabled.
203
+ */
204
+ initialGroupStates ?: Record < string , boolean > ;
201
205
} ;
202
206
203
207
export type PickerContextState < T extends object > = {
@@ -312,7 +316,10 @@ const flattenOptions = <T extends object>(options: OptionOrGroup<T>[]): T[] => {
312
316
313
317
type GroupStatusMap = Record < string , boolean > ;
314
318
315
- const useTogglableGroups = < T extends object > ( options : OptionOrGroup < T > [ ] ) => {
319
+ const useTogglableGroups = < T extends object > (
320
+ options : OptionOrGroup < T > [ ] ,
321
+ initialGroupStates ?: Record < string , boolean >
322
+ ) => {
316
323
const groupsWithOptions = useMemo ( ( ) => {
317
324
const ids : string [ ] = [ ] ;
318
325
for ( const optionOrGroup of options ) {
@@ -332,14 +339,16 @@ const useTogglableGroups = <T extends object>(options: OptionOrGroup<T>[]) => {
332
339
const groupStatusMap = $groupStatusMap . get ( ) ;
333
340
const newMap : GroupStatusMap = { } ;
334
341
for ( const id of groupsWithOptions ) {
335
- if ( newMap [ id ] = == undefined ) {
336
- newMap [ id ] = false ;
342
+ if ( initialGroupStates && initialGroupStates [ id ] ! == undefined ) {
343
+ newMap [ id ] = initialGroupStates [ id ] ;
337
344
} else if ( groupStatusMap [ id ] !== undefined ) {
338
345
newMap [ id ] = groupStatusMap [ id ] ;
346
+ } else {
347
+ newMap [ id ] = false ;
339
348
}
340
349
}
341
350
$groupStatusMap . set ( newMap ) ;
342
- } , [ groupsWithOptions , $groupStatusMap ] ) ;
351
+ } , [ groupsWithOptions , $groupStatusMap , initialGroupStates ] ) ;
343
352
344
353
const toggleGroup = useCallback (
345
354
( idToToggle : string ) => {
@@ -511,10 +520,14 @@ export const Picker = typedMemo(<T extends object>(props: PickerProps<T>) => {
511
520
OptionComponent = DefaultOptionComponent ,
512
521
NextToSearchBar,
513
522
searchable,
523
+ initialGroupStates,
514
524
} = props ;
515
525
const rootRef = useRef < HTMLDivElement > ( null ) ;
516
526
const inputRef = useRef < HTMLInputElement > ( null ) ;
517
- const { $groupStatusMap, $areAllGroupsDisabled, toggleGroup } = useTogglableGroups ( optionsOrGroups ) ;
527
+ const { $groupStatusMap, $areAllGroupsDisabled, toggleGroup } = useTogglableGroups (
528
+ optionsOrGroups ,
529
+ initialGroupStates
530
+ ) ;
518
531
const $activeOptionId = useAtom ( getFirstOptionId ( optionsOrGroups , getOptionId ) ) ;
519
532
const $compactView = useAtom ( true ) ;
520
533
const $optionsOrGroups = useAtom ( optionsOrGroups ) ;
0 commit comments