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

Make sure that the input syncs when the combobox closes #1137

Merged
merged 2 commits into from
Feb 23, 2022
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
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