Skip to content

Commit 62ff1ee

Browse files
authored
fix(CommandMenu): navigation with sections (#755)
1 parent 6430fed commit 62ff1ee

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

.changeset/orange-jeans-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
Fix navigation for CommandMenu with sections.

src/components/actions/CommandMenu/CommandMenu.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,24 @@ function CommandMenu<T extends object>(
337337

338338
// Helper function to find the first selectable item from filtered results
339339
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+
}
348353
}
349-
}
354+
return null;
355+
};
350356

351-
return null;
357+
return visit(filteredCollectionItems);
352358
}, [filteredCollectionItems, treeState.selectionManager]);
353359

354360
// Create a ref for the menu container
@@ -584,21 +590,21 @@ function CommandMenu<T extends object>(
584590
const currentKey =
585591
focusedKeyRef.current || selectionManager.focusedKey;
586592

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
588594
const getVisibleItemKeys = (): Key[] => {
589595
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);
598603
}
599604
}
600-
}
605+
};
601606

607+
visit(filteredCollectionItems);
602608
return keys;
603609
};
604610

0 commit comments

Comments
 (0)