Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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 @@ -204,6 +207,7 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
loop,
disablePointerSelection = false,
vimBindings = true,
triggerKeys = ['Enter'],
...etc
} = props

Expand Down Expand Up @@ -609,19 +613,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