diff --git a/cmdk/src/index.tsx b/cmdk/src/index.tsx index 1547b27..4caa52d 100644 --- a/cmdk/src/index.tsx +++ b/cmdk/src/index.tsx @@ -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 = { @@ -204,6 +207,7 @@ const Command = React.forwardRef((props, forwarded loop, disablePointerSelection = false, vimBindings = true, + triggerKeys = ['Enter'], ...etc } = props @@ -609,19 +613,21 @@ const Command = React.forwardRef((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) } } }