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

fix(combobox): when backspacing searchTerm highlight the first available option #1009

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
32 changes: 32 additions & 0 deletions packages/radix-vue/src/Combobox/Combobox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,38 @@ describe('given default Combobox', () => {
})
})
})

describe('after keypress input', () => {
beforeEach(async () => {
await valueBox.setValue('B')
})

describe('if filter-function provided', () => {
beforeEach(async () => {
await wrapper.setProps({
filterFunction: (list: any[], term: string) => {
return list.filter(i => i.toLowerCase().includes(term.toLowerCase()))
},
})
})
it('should filter with the searchTerm (Bl', async () => {
await valueBox.setValue('Bl')

const selection = wrapper.findAll('[data-highlighted]').filter(i => i.attributes('style') !== 'display: none;')
expect(selection.length).toBe(1)
expect(selection[0].element.innerHTML).contains('Blueberry')
})

it('should filter with the searchTerm (B', async () => {
await valueBox.setValue('Bl')
await valueBox.setValue('B')

const selection = wrapper.findAll('[data-highlighted]').filter(i => i.attributes('style') !== 'display: none;')
expect(selection.length).toBe(1)
expect(selection[0].element.innerHTML).contains('Banana')
})
})
})
})
})

Expand Down
4 changes: 2 additions & 2 deletions packages/radix-vue/src/Combobox/ComboboxRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ watch(stringifiedModelValue, async () => {
immediate: !props.searchTerm,
})

watch(() => filteredOptions.value.length, async (length) => {
watch(() => [filteredOptions.value.length, searchTerm.value.length], async ([length, searchTermLength], [oldLength, oldSearchTermLength]) => {

Choose a reason for hiding this comment

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

If searchTerm.value is null/undefined this causes a .length error and bugs out the Combobox

await nextTick()
await nextTick()
if (length && activeIndex.value === -1)
if (length && (oldSearchTermLength > searchTermLength || activeIndex.value === -1))
selectedValue.value = filteredOptions.value[0]
})

Expand Down
Loading