Skip to content

Commit

Permalink
Make sure that the input syncs when the combobox closes (#1137)
Browse files Browse the repository at this point in the history
* make sure that the input syncs when the combobox closes

* update changelog
  • Loading branch information
RobinMalfait authored Feb 23, 2022
1 parent 0c213b5 commit 475568b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased - @headlessui/vue]

- Nothing yet!
### Fixed

- Make sure that the input syncs when the combobox closes ([#1137](https://github.com/tailwindlabs/headlessui/pull/1137))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2053,6 +2053,39 @@ describe('Keyboard interactions', () => {
expect(spy).toHaveBeenCalledTimes(2)
})
)

it(
'should sync the input field correctly and reset it when pressing Escape',
suppressConsoleLogs(async () => {
render(
<Combobox value="option-b" onChange={console.log}>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
<Combobox.Option value="option-a">Option A</Combobox.Option>
<Combobox.Option value="option-b">Option B</Combobox.Option>
<Combobox.Option value="option-c">Option C</Combobox.Option>
</Combobox.Options>
</Combobox>
)

// Open combobox
await click(getComboboxButton())

// Verify the input has the selected value
expect(getComboboxInput()?.value).toBe('option-b')

// Override the input by typing something
await type(word('test'), getComboboxInput())
expect(getComboboxInput()?.value).toBe('test')

// Close combobox
await press(Keys.Escape)

// Verify the input is reset correctly
expect(getComboboxInput()?.value).toBe('option-b')
})
)
})

describe('`ArrowDown` key', () => {
Expand Down
36 changes: 36 additions & 0 deletions packages/@headlessui-vue/src/components/combobox/combobox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,42 @@ describe('Keyboard interactions', () => {
expect(spy).toHaveBeenCalledTimes(2)
})
)

it(
'should sync the input field correctly and reset it when pressing Escape',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Combobox v-model="value">
<ComboboxInput />
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption value="option-a">Option A</ComboboxOption>
<ComboboxOption value="option-b">Option B</ComboboxOption>
<ComboboxOption value="option-c">Option C</ComboboxOption>
</ComboboxOptions>
</Combobox>
`,
setup: () => ({ value: ref('option-b') }),
})

// Open combobox
await click(getComboboxButton())

// Verify the input has the selected value
expect(getComboboxInput()?.value).toBe('option-b')

// Override the input by typing something
await type(word('test'), getComboboxInput())
expect(getComboboxInput()?.value).toBe('test')

// Close combobox
await press(Keys.Escape)

// Verify the input is reset correctly
expect(getComboboxInput()?.value).toBe('option-b')
})
)
})

describe('`ArrowDown` key', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/@headlessui-vue/src/components/combobox/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ export let Combobox = defineComponent({
api.closeCombobox()
})

watch([api.value, api.inputRef], () => api.syncInputValue(), { immediate: true })
watch([api.value, api.inputRef, api.comboboxState], () => api.syncInputValue(), {
immediate: true,
})

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

0 comments on commit 475568b

Please sign in to comment.