Skip to content

Commit 475568b

Browse files
authored
Make sure that the input syncs when the combobox closes (#1137)
* make sure that the input syncs when the combobox closes * update changelog
1 parent 0c213b5 commit 475568b

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
## [Unreleased - @headlessui/vue]
1313

14-
- Nothing yet!
14+
### Fixed
15+
16+
- Make sure that the input syncs when the combobox closes ([#1137](https://github.com/tailwindlabs/headlessui/pull/1137))
1517

1618
## [@headlessui/react@v1.5.0] - 2022-02-17
1719

packages/@headlessui-react/src/components/combobox/combobox.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,39 @@ describe('Keyboard interactions', () => {
20532053
expect(spy).toHaveBeenCalledTimes(2)
20542054
})
20552055
)
2056+
2057+
it(
2058+
'should sync the input field correctly and reset it when pressing Escape',
2059+
suppressConsoleLogs(async () => {
2060+
render(
2061+
<Combobox value="option-b" onChange={console.log}>
2062+
<Combobox.Input onChange={NOOP} />
2063+
<Combobox.Button>Trigger</Combobox.Button>
2064+
<Combobox.Options>
2065+
<Combobox.Option value="option-a">Option A</Combobox.Option>
2066+
<Combobox.Option value="option-b">Option B</Combobox.Option>
2067+
<Combobox.Option value="option-c">Option C</Combobox.Option>
2068+
</Combobox.Options>
2069+
</Combobox>
2070+
)
2071+
2072+
// Open combobox
2073+
await click(getComboboxButton())
2074+
2075+
// Verify the input has the selected value
2076+
expect(getComboboxInput()?.value).toBe('option-b')
2077+
2078+
// Override the input by typing something
2079+
await type(word('test'), getComboboxInput())
2080+
expect(getComboboxInput()?.value).toBe('test')
2081+
2082+
// Close combobox
2083+
await press(Keys.Escape)
2084+
2085+
// Verify the input is reset correctly
2086+
expect(getComboboxInput()?.value).toBe('option-b')
2087+
})
2088+
)
20562089
})
20572090

20582091
describe('`ArrowDown` key', () => {

packages/@headlessui-vue/src/components/combobox/combobox.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,42 @@ describe('Keyboard interactions', () => {
21952195
expect(spy).toHaveBeenCalledTimes(2)
21962196
})
21972197
)
2198+
2199+
it(
2200+
'should sync the input field correctly and reset it when pressing Escape',
2201+
suppressConsoleLogs(async () => {
2202+
renderTemplate({
2203+
template: html`
2204+
<Combobox v-model="value">
2205+
<ComboboxInput />
2206+
<ComboboxButton>Trigger</ComboboxButton>
2207+
<ComboboxOptions>
2208+
<ComboboxOption value="option-a">Option A</ComboboxOption>
2209+
<ComboboxOption value="option-b">Option B</ComboboxOption>
2210+
<ComboboxOption value="option-c">Option C</ComboboxOption>
2211+
</ComboboxOptions>
2212+
</Combobox>
2213+
`,
2214+
setup: () => ({ value: ref('option-b') }),
2215+
})
2216+
2217+
// Open combobox
2218+
await click(getComboboxButton())
2219+
2220+
// Verify the input has the selected value
2221+
expect(getComboboxInput()?.value).toBe('option-b')
2222+
2223+
// Override the input by typing something
2224+
await type(word('test'), getComboboxInput())
2225+
expect(getComboboxInput()?.value).toBe('test')
2226+
2227+
// Close combobox
2228+
await press(Keys.Escape)
2229+
2230+
// Verify the input is reset correctly
2231+
expect(getComboboxInput()?.value).toBe('option-b')
2232+
})
2233+
)
21982234
})
21992235

22002236
describe('`ArrowDown` key', () => {

packages/@headlessui-vue/src/components/combobox/combobox.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ export let Combobox = defineComponent({
229229
api.closeCombobox()
230230
})
231231

232-
watch([api.value, api.inputRef], () => api.syncInputValue(), { immediate: true })
232+
watch([api.value, api.inputRef, api.comboboxState], () => api.syncInputValue(), {
233+
immediate: true,
234+
})
233235

234236
// @ts-expect-error Types of property 'dataRef' are incompatible.
235237
provide(ComboboxContext, api)

0 commit comments

Comments
 (0)