@@ -337,18 +337,24 @@ function CommandMenu<T extends object>(
337
337
338
338
// Helper function to find the first selectable item from filtered results
339
339
const findFirstSelectableItem = useCallback ( ( ) => {
340
- // Use the filtered collection items instead of the full tree state collection
341
- for ( const item of filteredCollectionItems ) {
342
- if (
343
- item &&
344
- item . type === 'item' &&
345
- ! treeState . selectionManager . isDisabled ( item . key )
346
- ) {
347
- return item . key ;
340
+ const { selectionManager } = treeState ;
341
+
342
+ const visit = ( items : any [ ] ) : Key | null => {
343
+ for ( const node of items ) {
344
+ if ( node ?. type === 'section' ) {
345
+ const childResult = visit ( Array . from ( node . childNodes ?? [ ] ) ) ;
346
+ if ( childResult != null ) return childResult ;
347
+ } else if (
348
+ node ?. type === 'item' &&
349
+ ! selectionManager . isDisabled ( node . key )
350
+ ) {
351
+ return node . key ;
352
+ }
348
353
}
349
- }
354
+ return null ;
355
+ } ;
350
356
351
- return null ;
357
+ return visit ( filteredCollectionItems ) ;
352
358
} , [ filteredCollectionItems , treeState . selectionManager ] ) ;
353
359
354
360
// Create a ref for the menu container
@@ -584,21 +590,21 @@ function CommandMenu<T extends object>(
584
590
const currentKey =
585
591
focusedKeyRef . current || selectionManager . focusedKey ;
586
592
587
- // Helper function to get all visible item keys by applying filter to tree state collection
593
+ // Helper function to get all visible item keys accounting for sections
588
594
const getVisibleItemKeys = ( ) : Key [ ] => {
589
595
const keys : Key [ ] = [ ] ;
590
- const term = searchValue . trim ( ) ;
591
-
592
- // Use the tree state's collection and apply filter manually
593
- for ( const item of treeState . collection ) {
594
- if ( item . type === 'item' ) {
595
- const text = item . textValue ?? String ( item . rendered ?? '' ) ;
596
- if ( enhancedFilter ( text , term , item . props ) ) {
597
- keys . push ( item . key ) ;
596
+
597
+ const visit = ( items : any [ ] ) => {
598
+ for ( const node of items ) {
599
+ if ( node ?. type === 'section' ) {
600
+ visit ( Array . from ( node . childNodes ?? [ ] ) ) ;
601
+ } else if ( node ?. type === 'item' ) {
602
+ keys . push ( node . key ) ;
598
603
}
599
604
}
600
- }
605
+ } ;
601
606
607
+ visit ( filteredCollectionItems ) ;
602
608
return keys ;
603
609
} ;
604
610
0 commit comments