Skip to content

How to detect if item was selected via "Click" or "Enter"? #309

Closed Answered by EdoAPP
EdoAPP asked this question in Q&A
Discussion options

You must be logged in to vote

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);

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by EdoAPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant