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

Add inputEnterFunction prop to ComboboxRoot #1577

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions docs/content/meta/ComboboxRoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
'type': '((val: string[] | number[] | false[] | true[] | Record<string, any>[], term: string) => string[] | number[] | false[] | true[] | Record<string, any>[])',
'required': false
},
{
'name': 'inputEnterFunction',
'description': '<p>Custom function for handling combobox input enter event that preserves native event. By default, when user presses enter on <code>ComboboxInput</code>, the currently selected item is automatically selected, triggering a click event. This might not be ideal in some cases (example: search suggestions box), where pressing enter should trigger a different action. For such cases, you can provide a custom function to handle the enter event.</p>\n',
'type': '((event: InputEvent, selectedValue: AcceptableValue | undefined) => void)',
'required': false
}
{
'name': 'modelValue',
'description': '<p>The controlled value of the Combobox. Can be binded with with <code>v-model</code>.</p>\n',
Expand Down
7 changes: 7 additions & 0 deletions packages/radix-vue/src/Combobox/ComboboxRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export interface ComboboxRootProps<T = AcceptableValue> extends PrimitiveProps {
dir?: Direction
/** The custom filter function for filtering `ComboboxItem`. */
filterFunction?: (val: ArrayOrWrapped<T>, term: string) => ArrayOrWrapped<T>
/** Custom function for handling combobox input enter event that preserves native event. */
inputEnterFunction?: (event: InputEvent, selectedValue: T | undefined) => void
/** The display value of input for selected item. Does not work with `multiple`. */
displayValue?: (val: T) => string
/**
Expand Down Expand Up @@ -267,6 +269,11 @@ function onCompositionEnd() {
})
}
async function onInputEnter(event: InputEvent) {
if (props.inputEnterFunction) {
props.inputEnterFunction(event, selectedValue.value)
return
}

if (filteredOptions.value.length && selectedValue.value && selectedElement.value instanceof Element) {
event.preventDefault()
event.stopPropagation()
Expand Down