Skip to content
Merged
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
15 changes: 11 additions & 4 deletions src/runtime/components/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,17 @@ function onUpdate(value: any) {
}
}

function onInputUpdate(value: string) {
if (!isAutocomplete.value) {
searchTerm.value = value
function onInputUpdate(value: string, open: boolean, modelValue: unknown) {
if (isAutocomplete.value) {
return
}

// While closed, reka resets the input to the label of the selected item, which isn't a search.
if (!open && value && value === displayValue(modelValue as GetItemValue<T, VK, ExcludeItem>)) {
return
}

searchTerm.value = value
}

function onBlur(event: FocusEvent) {
Expand Down Expand Up @@ -758,7 +765,7 @@ defineExpose({
@blur="onBlur"
@focus="onFocus"
@change.stop
@update:model-value="onInputUpdate"
@update:model-value="onInputUpdate($event, open, modelValue)"
/>

<slot :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :open="open" :ui="ui" />
Expand Down
33 changes: 33 additions & 0 deletions test/components/InputMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,39 @@ describe('InputMenu', () => {
expect(emissions).toBeTruthy()
expect(emissions![emissions!.length - 1]).toEqual(['Option 2'])
})

test('searchTerm ignores the label when parent updates modelValue', async () => {
const wrapper = mount(InputMenu, { props: { items: ['Option 1', 'Option 2'], modelValue: 'Option 1' } })
await flushPromises()

await wrapper.setProps({ modelValue: 'Option 2' })
await flushPromises()

expect(wrapper.emitted('update:searchTerm')?.flat() ?? []).not.toContain('Option 2')
})

test('searchTerm ignores the label after selecting without v-model', async () => {
const wrapper = mount(InputMenu, { props: { items: ['Option 1', 'Option 2'], defaultValue: 'Option 1', resetSearchTermOnBlur: false, defaultOpen: true, portal: false }, attachTo: document.body })
await flushPromises()

await wrapper.findAll('[role="option"]')[1]!.trigger('click')
await flushPromises()

expect(wrapper.emitted('update:searchTerm')?.flat() ?? []).not.toContain('Option 2')

wrapper.unmount()
})

test('searchTerm follows typing the selected label in a menu that starts open', async () => {
const wrapper = mount(InputMenu, { props: { items: ['Option 1', 'Option 2'], modelValue: 'Option 2', defaultOpen: true, portal: false } })
await flushPromises()

const input = wrapper.find('input')
await input.setValue('Option ')
await input.setValue('Option 2')

expect(wrapper.emitted('update:searchTerm')?.at(-1)).toEqual(['Option 2'])
})
})

describe('clear', () => {
Expand Down
Loading