Skip to content

Commit 9ef269e

Browse files
authored
Add null as a valid type for Listbox and Combobox in Vue (#2064)
* add `null` as a valid type for Listbox and Combobox in Vue * update changelog
1 parent 219901c commit 9ef269e

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,30 @@ describe('Rendering', () => {
456456
})
457457
)
458458
})
459+
460+
it(
461+
'null should be a valid value for the Listbox',
462+
suppressConsoleLogs(async () => {
463+
render(
464+
<Listbox value={null} onChange={console.log}>
465+
<Listbox.Button>Trigger</Listbox.Button>
466+
<Listbox.Options>
467+
<Listbox.Option value="a">Option A</Listbox.Option>
468+
<Listbox.Option value="b">Option B</Listbox.Option>
469+
<Listbox.Option value="c">Option C</Listbox.Option>
470+
</Listbox.Options>
471+
</Listbox>
472+
)
473+
474+
assertListboxButton({ state: ListboxState.InvisibleUnmounted })
475+
assertListbox({ state: ListboxState.InvisibleUnmounted })
476+
477+
await click(getListboxButton())
478+
479+
assertListboxButton({ state: ListboxState.Visible })
480+
assertListbox({ state: ListboxState.Visible })
481+
})
482+
)
459483
})
460484

461485
describe('Listbox.Label', () => {

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Improve syncing of the `ComboboxInput` value ([#2042](https://github.com/tailwindlabs/headlessui/pull/2042))
1616
- Fix crash when using `multiple` mode without `value` prop (uncontrolled) for `Listbox` and `Combobox` components ([#2058](https://github.com/tailwindlabs/headlessui/pull/2058))
1717
- Allow passing in your own `id` prop ([#2060](https://github.com/tailwindlabs/headlessui/pull/2060))
18+
- Add `null` as a valid type for Listbox and Combobox in Vue ([#2064](https://github.com/tailwindlabs/headlessui/pull/2064))
1819

1920
## [1.7.4] - 2022-11-03
2021

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ export let Combobox = defineComponent({
118118
as: { type: [Object, String], default: 'template' },
119119
disabled: { type: [Boolean], default: false },
120120
by: { type: [String, Function], default: () => defaultComparator },
121-
modelValue: { type: [Object, String, Number, Boolean], default: undefined },
121+
modelValue: {
122+
type: [Object, String, Number, Boolean] as PropType<
123+
object | string | number | boolean | null
124+
>,
125+
default: undefined,
126+
},
122127
defaultValue: { type: [Object, String, Number, Boolean], default: undefined },
123128
name: { type: String },
124129
nullable: { type: Boolean, default: false },

packages/@headlessui-vue/src/components/listbox/listbox.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,33 @@ describe('Rendering', () => {
495495
})
496496
)
497497
})
498+
499+
it(
500+
'null should be a valid value for the Listbox',
501+
suppressConsoleLogs(async () => {
502+
renderTemplate({
503+
template: html`
504+
<Listbox v-model="value" by="id">
505+
<ListboxButton>Trigger</ListboxButton>
506+
<ListboxOptions>
507+
<ListboxOption :value="{ id: 1, name: 'alice' }">alice</ListboxOption>
508+
<ListboxOption :value="{ id: 2, name: 'bob' }">bob</ListboxOption>
509+
<ListboxOption :value="{ id: 3, name: 'charlie' }">charlie</ListboxOption>
510+
</ListboxOptions>
511+
</Listbox>
512+
`,
513+
setup: () => ({ value: null }),
514+
})
515+
516+
assertListboxButton({ state: ListboxState.InvisibleUnmounted })
517+
assertListbox({ state: ListboxState.InvisibleUnmounted })
518+
519+
await click(getListboxButton())
520+
521+
assertListboxButton({ state: ListboxState.Visible })
522+
assertListbox({ state: ListboxState.Visible })
523+
})
524+
)
498525
})
499526

500527
describe('ListboxLabel', () => {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
InjectionKey,
1919
Ref,
2020
UnwrapNestedRefs,
21+
PropType,
2122
} from 'vue'
2223

2324
import { Features, render, omit, compact } from '../../utils/render'
@@ -119,7 +120,12 @@ export let Listbox = defineComponent({
119120
disabled: { type: [Boolean], default: false },
120121
by: { type: [String, Function], default: () => defaultComparator },
121122
horizontal: { type: [Boolean], default: false },
122-
modelValue: { type: [Object, String, Number, Boolean], default: undefined },
123+
modelValue: {
124+
type: [Object, String, Number, Boolean] as PropType<
125+
object | string | number | boolean | null
126+
>,
127+
default: undefined,
128+
},
123129
defaultValue: { type: [Object, String, Number, Boolean], default: undefined },
124130
name: { type: String, optional: true },
125131
multiple: { type: [Boolean], default: false },

0 commit comments

Comments
 (0)