-
As the title says, I need a way to detect if the user selected the item clicking the Item or by pressing Enter. Is there a way to do so?
|
Beta Was this translation helpful? Give feedback.
Answered by
EdoAPP
Sep 30, 2024
Replies: 1 comment
-
For anyone curious on how to do it. I was able to get around it by adding a similar "Enter" logic to the lib implementation: <Command
onKeyDown={(e) => {
if (e.key === 'Enter') {
// prevents default so it doesn't call the internal "Enter" event immediately
e.preventDefault();
const item = listInnerRef.current?.querySelector(`[cmdk-item=""][aria-selected="true"]`);
if (item) {
// ADD your custom "Enter" logic here
// Emits internal selection event (onSelectValue will be triggered)
const event = new Event('cmdk-item-select');
item.dispatchEvent(event);
}
}
}}
>
<CommandInput ... />
<CommandList ref={listInnerRef}>
// your items here.
</CommandList>
</Command> |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
EdoAPP
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone curious on how to do it. I was able to get around it by adding a similar "Enter" logic to the lib implementation: