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

@ -1,3 +1,4 @@
import { hasUserSelectedCommandState } from '@/command-menu/states/hasUserSelectedCommandState';
import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList';
import { useEffect } from 'react';
import { useRecoilValue } from 'recoil';
@ -13,10 +14,13 @@ export const CommandMenuDefaultSelectionEffect = ({
const selectedItemId = useRecoilValue(selectedItemIdState);
const hasUserSelectedCommand = useRecoilValue(hasUserSelectedCommandState);
useEffect(() => {
if (
isDefined(selectedItemId) &&
selectableItemIds.includes(selectedItemId)
selectableItemIds.includes(selectedItemId) &&
hasUserSelectedCommand
) {
return;
}
@ -24,7 +28,12 @@ export const CommandMenuDefaultSelectionEffect = ({
if (selectableItemIds.length > 0) {
setSelectedItemId(selectableItemIds[0]);
}
}, [selectableItemIds, selectedItemId, setSelectedItemId]);
}, [
hasUserSelectedCommand,
selectableItemIds,
selectedItemId,
setSelectedItemId,
]);
return null;
};