From 9c9ce02bbe3301ae50d15a627bee355619a1b08d Mon Sep 17 00:00:00 2001 From: arihanv Date: Fri, 8 Mar 2024 23:08:50 -0600 Subject: [PATCH] feat: support custom trigger keys --- cmdk/src/index.tsx | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/cmdk/src/index.tsx b/cmdk/src/index.tsx index bba2c89..653b782 100644 --- a/cmdk/src/index.tsx +++ b/cmdk/src/index.tsx @@ -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 = { @@ -193,6 +196,7 @@ const Command = React.forwardRef((props, forwarded loop, disablePointerSelection = false, vimBindings = true, + triggerKeys = ['Enter'], ...etc } = props @@ -598,19 +602,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) } } }