Skip to content

Commit 67995b6

Browse files
authored
Reset Combobox Input when the value gets reset (#1181)
* reset input if value is reset Fixes: #1177 * update changelog
1 parent c4d8289 commit 67995b6

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fix `<Transition>` flickering issue ([#1118](https://github.com/tailwindlabs/headlessui/pull/1118))
1818
- Improve outside click support ([#1175](https://github.com/tailwindlabs/headlessui/pull/1175))
1919
- Ensure that `appear` works regardless of multiple rerenders ([#1179](https://github.com/tailwindlabs/headlessui/pull/1179))
20+
- Reset Combobox Input when the value gets reset ([#1181](https://github.com/tailwindlabs/headlessui/pull/1181))
2021

2122
## [Unreleased - @headlessui/vue]
2223

@@ -29,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2930
- Fix `hover` scroll ([#1161](https://github.com/tailwindlabs/headlessui/pull/1161))
3031
- Guarantee DOM sort order when performing actions ([#1168](https://github.com/tailwindlabs/headlessui/pull/1168))
3132
- Improve outside click support ([#1175](https://github.com/tailwindlabs/headlessui/pull/1175))
33+
- Reset Combobox Input when the value gets reset ([#1181](https://github.com/tailwindlabs/headlessui/pull/1181))
3234

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

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,4 +4349,46 @@ describe('Mouse interactions', () => {
43494349
assertActiveComboboxOption(options[1])
43504350
})
43514351
)
4352+
4353+
it(
4354+
'should sync the input field correctly and reset it when resetting the value from outside',
4355+
suppressConsoleLogs(async () => {
4356+
function Example() {
4357+
let [value, setValue] = useState<string | null>('bob')
4358+
4359+
return (
4360+
<>
4361+
<Combobox value={value} onChange={setValue}>
4362+
<Combobox.Input onChange={NOOP} />
4363+
<Combobox.Button>Trigger</Combobox.Button>
4364+
<Combobox.Options>
4365+
<Combobox.Option value="alice">alice</Combobox.Option>
4366+
<Combobox.Option value="bob">bob</Combobox.Option>
4367+
<Combobox.Option value="charlie">charlie</Combobox.Option>
4368+
</Combobox.Options>
4369+
</Combobox>
4370+
<button onClick={() => setValue(null)}>reset</button>
4371+
</>
4372+
)
4373+
}
4374+
4375+
render(<Example />)
4376+
4377+
// Open combobox
4378+
await click(getComboboxButton())
4379+
4380+
// Verify the input has the selected value
4381+
expect(getComboboxInput()?.value).toBe('bob')
4382+
4383+
// Override the input by typing something
4384+
await type(word('test'), getComboboxInput())
4385+
expect(getComboboxInput()?.value).toBe('test')
4386+
4387+
// Reset from outside
4388+
await click(getByText('reset'))
4389+
4390+
// Verify the input is reset correctly
4391+
expect(getComboboxInput()?.value).toBe('')
4392+
})
4393+
)
43524394
})

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
330330
inputRef.current.value = displayValue(value)
331331
} else if (typeof value === 'string') {
332332
inputRef.current.value = value
333+
} else {
334+
inputRef.current.value = ''
333335
}
334336
}, [value, inputRef, inputPropsRef])
335337

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4576,4 +4576,41 @@ describe('Mouse interactions', () => {
45764576
assertActiveComboboxOption(options[1])
45774577
})
45784578
)
4579+
4580+
it(
4581+
'should sync the input field correctly and reset it when resetting the value from outside',
4582+
suppressConsoleLogs(async () => {
4583+
renderTemplate({
4584+
template: html`
4585+
<Combobox v-model="value">
4586+
<ComboboxInput />
4587+
<ComboboxButton>Trigger</ComboboxButton>
4588+
<ComboboxOptions>
4589+
<ComboboxOption value="alice">alice</ComboboxOption>
4590+
<ComboboxOption value="bob">bob</ComboboxOption>
4591+
<ComboboxOption value="charlie">charlie</ComboboxOption>
4592+
</ComboboxOptions>
4593+
</Combobox>
4594+
<button @click="value = null">reset</button>
4595+
`,
4596+
setup: () => ({ value: ref('bob') }),
4597+
})
4598+
4599+
// Open combobox
4600+
await click(getComboboxButton())
4601+
4602+
// Verify the input has the selected value
4603+
expect(getComboboxInput()?.value).toBe('bob')
4604+
4605+
// Override the input by typing something
4606+
await type(word('test'), getComboboxInput())
4607+
expect(getComboboxInput()?.value).toBe('test')
4608+
4609+
// Reset from outside
4610+
await click(getByText('reset'))
4611+
4612+
// Verify the input is reset correctly
4613+
expect(getComboboxInput()?.value).toBe('')
4614+
})
4615+
)
45794616
})

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ export let Combobox = defineComponent({
177177
api.inputRef!.value!.value = displayValue(value)
178178
} else if (typeof value === 'string') {
179179
api.inputRef!.value!.value = value
180+
} else {
181+
api.inputRef!.value!.value = ''
180182
}
181183
},
182184
selectOption(id: string) {

0 commit comments

Comments
 (0)