Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom trigger keys #232

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
Loading