Fix command menu selection (#10248)

- Created a state `hasUserSelectedCommandState` : This state is set to
`true` when the user selects an element in the command menu list. It is
set to false upon redirection or when the command menu is closed.
- Modified `CommandMenuDefaultSelectionEffect` to have the expected
selection behavior for the command menu
This commit is contained in:
Raphaël Bosi
2025-02-17 12:02:17 +01:00
committed by GitHub
parent 14478afa7e
commit 7ed142e987
6 changed files with 44 additions and 4 deletions

View File

@ -23,8 +23,9 @@ export const SelectableList = ({
selectableItemIdArray,
selectableItemIdMatrix,
onEnter,
onSelect,
}: SelectableListProps) => {
useSelectableListHotKeys(selectableListId, hotkeyScope);
useSelectableListHotKeys(selectableListId, hotkeyScope, onSelect);
const { setSelectableItemIds, setSelectableListOnEnter, setSelectedItemId } =
useSelectableList(selectableListId);

View File

@ -11,6 +11,7 @@ type Direction = 'up' | 'down' | 'left' | 'right';
export const useSelectableListHotKeys = (
scopeId: string,
hotkeyScope: string,
onSelect?: (itemId: string) => void,
) => {
const findPosition = (
selectableItemIds: string[][],
@ -105,6 +106,7 @@ export const useSelectableListHotKeys = (
if (isNonEmptyString(nextId)) {
set(isSelectedItemIdSelector(nextId), true);
set(selectedItemIdState, nextId);
onSelect?.(nextId);
}
if (isNonEmptyString(selectedItemId)) {
@ -112,7 +114,12 @@ export const useSelectableListHotKeys = (
}
}
},
[isSelectedItemIdSelector, selectableItemIdsState, selectedItemIdState],
[
isSelectedItemIdSelector,
onSelect,
selectableItemIdsState,
selectedItemIdState,
],
);
useScopedHotkeys(Key.ArrowUp, () => handleSelect('up'), hotkeyScope, []);