Skip to content

Commit

Permalink
feat: support custom trigger keys
Browse files Browse the repository at this point in the history
  • Loading branch information
arihanv committed Mar 9, 2024
1 parent 9ed2523 commit 9c9ce02
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ type CommandProps = Children &
* Set to `false` to disable ctrl+n/j/p/k shortcuts. Defaults to `true`.
*/
vimBindings?: boolean

/** Keys for triggering an item */
triggerKeys?: string[]
}

type Context = {
Expand Down Expand Up @@ -193,6 +196,7 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
loop,
disablePointerSelection = false,
vimBindings = true,
triggerKeys = ['Enter'],
...etc
} = props

Expand Down Expand Up @@ -598,19 +602,21 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
last()
break
}
case 'Enter': {
// Check if IME composition is finished before triggering onSelect
// This prevents unwanted triggering while user is still inputting text with IME
// e.keyCode === 229 is for the Japanese IME and Safari.
// isComposing does not work with Japanese IME and Safari combination.
if (!e.nativeEvent.isComposing && e.keyCode !== 229) {
// Trigger item onSelect
e.preventDefault()
const item = getSelectedItem()
if (item) {
const event = new Event(SELECT_EVENT)
item.dispatchEvent(event)
}
}

// Trigger item onSelect with the specified triggerKeys
if (triggerKeys && triggerKeys.includes(e.key)) {
// Check if IME composition is finished before triggering onSelect
// This prevents unwanted triggering while user is still inputting text with IME
// e.keyCode === 229 is for the Japanese IME and Safari.
// isComposing does not work with Japanese IME and Safari combination.
if (!e.nativeEvent.isComposing && e.keyCode !== 229) {
// Trigger item onSelect
e.preventDefault()
const item = getSelectedItem()
if (item) {
const event = new Event(SELECT_EVENT)
item.dispatchEvent(event)
}
}
}
Expand Down

0 comments on commit 9c9ce02

Please sign in to comment.