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
12 changes: 12 additions & 0 deletions src/runtime/components/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,17 @@ function onClear() {
emits('clear')
}

function onTagInputKeydown(e: KeyboardEvent) {
if (e.key !== 'Enter') return

// reka-ui's TagsInputRoot adds the raw search text as a tag on Enter regardless of
// whether it matches a real item or createItem is enabled. Block that so
// we never render a chip that isn't reflected in modelValue.
if (!props.createItem) {
e.preventDefault()
}
}
Comment on lines +551 to +560

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @al1maher! I don't think createItem is the right check though, the chip is still there with createItem: true and the menu closed. And preventing every Enter breaks form submission inside a UForm.

I think I would use searchTerm instead:

Suggested change
function onTagInputKeydown(e: KeyboardEvent) {
if (e.key !== 'Enter') return
// reka-ui's TagsInputRoot adds the raw search text as a tag on Enter regardless of
// whether it matches a real item or createItem is enabled. Block that so
// we never render a chip that isn't reflected in modelValue.
if (!props.createItem) {
e.preventDefault()
}
}
function onTagsInputKeydown(event: KeyboardEvent) {
if (event.isComposing || !searchTerm.value) {
return
}
event.preventDefault()
}

What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your approach is more correct, I made it that way as I was focusing on not breaking the create functionality but I think your solution is more robust


const viewportRef = useTemplateRef('viewportRef')

const comboboxRootRef = useTemplateRef('comboboxRootRef')
Expand Down Expand Up @@ -694,6 +705,7 @@ defineExpose({
data-slot="tagsInput"
:class="ui.tagsInput({ class: props.ui?.tagsInput })"
@change.stop
@keydown.enter="onTagInputKeydown"
/>
</Component.Input>
</TagsInputRoot>
Expand Down
Loading