pacocoursey / cmdk

Fast, unstyled command menu React component.
https://cmdk.paco.me
MIT License
9.98k stars 287 forks source link

Disable "Home" and "End" keys for selecting items in list #293

Closed tobiasSchaathun closed 3 months ago

tobiasSchaathun commented 3 months ago

Hello, is there a way to disable that the "Home" and "End" jump to the first and last index of the item list? I want to use it to do this in the input and wish to disable this behaviour for the items themselves.

tobiasSchaathun commented 3 months ago

I figured it out, leaving it here if anyone is interested i the answer in the future....

You can add an "onKeyDown" to the Command itself and use event.preventDefault to achieve this :)

skirsten commented 2 months ago

Hi, I am running into the same issue. The problem is that with event.preventDefault the input will also not handle the event.

What I want is the default functionality: Home goes to beginning of the input text and End to the end of the text. Unfortunately this seems not possible to achieve looking at the code: https://github.com/pacocoursey/cmdk/blob/92344c58a63b995f6eb149245c56ea548540dd18/cmdk/src/index.tsx#L571-L575

kevin-lonestone commented 2 weeks ago

This workaround worked for me: stop the keydown event from propagating from the input.

<Command.Input
  [...]
  onKeyDown={(event) => {
    switch (event.key) {
      // NOTE: prevents unwanted cmdk behaviour
      case 'Home':
      case 'End':
        event.stopPropagation()
        break
    }
  }}
/>
skirsten commented 2 weeks ago

@kevin-lonestone Awesome workaround. Works perfectly. Thanks a lot! :tada: