Skip to content

Commit 94965ee

Browse files
zernoniaclaude
andauthored
fix(Autocomplete): clear stale filter when cancelling a blurred input (#2989)
* fix(Autocomplete): clear stale filter when cancelling a blurred input ComboboxCancel focused the input before resetting modelValue. With `openOnFocus`, that focus re-opens the popup synchronously, and AutocompleteRoot restores the filter from the not-yet-cleared modelValue, leaving the list filtered by the old text. Reset modelValue first. Fixes #2988 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(Autocomplete): reset filter after focus so controlled v-model is covered With a controlled `v-model`, the root's modelValue still reads the old prop when the focus-triggered re-open runs, so resetting it before focusing was not enough. Reset `filterSearch` after focusing instead, and cover both the controlled and uncontrolled cases in the regression test. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 1451c7a commit 94965ee

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

‎packages/core/src/Autocomplete/Autocomplete.test.ts‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { DOMWrapper, VueWrapper } from '@vue/test-utils'
22
import { mount } from '@vue/test-utils'
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
44
import { axe } from 'vitest-axe'
5-
import { nextTick } from 'vue'
5+
import { nextTick, ref } from 'vue'
66
import { handleSubmit, sleep } from '@/test'
7+
import { AutocompleteAnchor, AutocompleteCancel, AutocompleteContent, AutocompleteInput, AutocompleteItem, AutocompleteRoot, AutocompleteViewport } from '.'
78
import Autocomplete from './story/_Autocomplete.vue'
89

910
describe('given default Autocomplete', () => {
@@ -296,3 +297,54 @@ describe('given autocomplete in a form', () => {
296297
})
297298
})
298299
})
300+
301+
describe('given autocomplete with cancel button and openOnFocus', () => {
302+
const components = { AutocompleteRoot, AutocompleteAnchor, AutocompleteInput, AutocompleteCancel, AutocompleteContent, AutocompleteViewport, AutocompleteItem }
303+
const options = ['Apple', 'Banana', 'Cherry']
304+
305+
// https://github.com/unovue/reka-ui/issues/2988
306+
it.each([
307+
['uncontrolled', '<AutocompleteRoot open-on-focus open-on-click>'],
308+
['controlled', '<AutocompleteRoot v-model="model" open-on-focus open-on-click>'],
309+
])('should show all items after clearing a closed, blurred input (%s)', async (_, root) => {
310+
document.body.innerHTML = ''
311+
const wrapper = mount({
312+
components,
313+
setup() {
314+
return { options, model: ref('') }
315+
},
316+
template: `
317+
${root}
318+
<AutocompleteAnchor>
319+
<AutocompleteInput />
320+
<AutocompleteCancel>X</AutocompleteCancel>
321+
</AutocompleteAnchor>
322+
<AutocompleteContent>
323+
<AutocompleteViewport>
324+
<AutocompleteItem v-for="option in options" :key="option" :value="option">
325+
{{ option }}
326+
</AutocompleteItem>
327+
</AutocompleteViewport>
328+
</AutocompleteContent>
329+
</AutocompleteRoot>
330+
`,
331+
}, { attachTo: document.body })
332+
const input = wrapper.find('input')
333+
334+
input.element.focus()
335+
await nextTick()
336+
await wrapper.findAll('[role=option]')[1].trigger('click')
337+
await nextTick()
338+
expect(input.element.value).toBe('Banana')
339+
expect(wrapper.find('[role=listbox]').exists()).toBe(false)
340+
341+
input.element.blur()
342+
await wrapper.find('button').trigger('click')
343+
await nextTick()
344+
await nextTick()
345+
346+
expect(input.element.value).toBe('')
347+
expect(document.activeElement).toBe(input.element)
348+
expect(wrapper.findAll('[role=option]').map(i => i.text())).toEqual(options)
349+
})
350+
})

‎packages/core/src/Combobox/ComboboxCancel.vue‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ useForwardExpose()
1717
const rootContext = injectComboboxRootContext()
1818
1919
function handleClick() {
20-
// Reset the search to show all options.
21-
rootContext.filterSearch.value = ''
22-
2320
if (rootContext.inputElement.value) {
2421
rootContext.inputElement.value.value = ''
2522
rootContext.inputElement.value.focus()
@@ -28,6 +25,10 @@ function handleClick() {
2825
if (rootContext.resetModelValueOnClear?.value) {
2926
rootContext.modelValue.value = rootContext.multiple.value ? [] : null
3027
}
28+
29+
// Reset the search to show all options.
30+
// Done last: focusing can re-open the popup (`openOnFocus`), which may restore the filter from the previous value.
31+
rootContext.filterSearch.value = ''
3132
}
3233
</script>
3334

0 commit comments

Comments
 (0)