Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 10 additions & 7 deletions src/runtime/components/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,19 @@ function onCreate(e: Event) {
}

function onSelect(e: Event, item: SelectMenuItem) {
if (!isSelectItem(item)) {
return
}
if (isSelectItem(item)) {
if (item.disabled) {
e.preventDefault()
return
}

if (item.disabled) {
e.preventDefault()
return
item.onSelect?.(e)
}

item.onSelect?.(e)
// Selecting the current item updates nothing, so the search is reset here too.
if (!e.defaultPrevented && props.resetSearchTermOnSelect) {
searchTerm.value = ''
}
}

function isSelectItem(item: SelectMenuItem): item is Exclude<SelectMenuItem, SelectMenuValue> {
Expand Down
14 changes: 14 additions & 0 deletions test/components/SelectMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ describe('SelectMenu', () => {
input.vm.$emit('update:open', false)
expect(wrapper.emitted()).toMatchObject({ blur: [[{ type: 'blur' }]] })
})

test.each([
['controlled', { modelValue: 'Option 1' }],
['uncontrolled', { defaultValue: 'Option 1' }]
])('resets the search term when selecting the current item %s', async (_, model) => {
const wrapper = mount(SelectMenu, { props: { items: ['Option 1', 'Option 2'], ...model, searchTerm: 'Option', resetSearchTermOnBlur: false, defaultOpen: true, portal: false }, attachTo: document.body })
await flushPromises()
await wrapper.find('[role="option"][data-state="checked"]').trigger('click')

expect(wrapper.emitted('update:searchTerm')).toEqual([['']])
expect(wrapper.emitted('change')).toBeUndefined()

wrapper.unmount()
})
})

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