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

Fix nullable by prop for combobox and radio group #1815

Merged
merged 2 commits into from
Sep 2, 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
2 changes: 1 addition & 1 deletion packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `by` prop for `Listbox`, `Combobox` and `RadioGroup` ([#1482](https://github.com/tailwindlabs/headlessui/pull/1482), [#1717](https://github.com/tailwindlabs/headlessui/pull/1717), [#1814](https://github.com/tailwindlabs/headlessui/pull/1814))
- Add `by` prop for `Listbox`, `Combobox` and `RadioGroup` ([#1482](https://github.com/tailwindlabs/headlessui/pull/1482), [#1717](https://github.com/tailwindlabs/headlessui/pull/1717), [#1814](https://github.com/tailwindlabs/headlessui/pull/1814), [#1815](https://github.com/tailwindlabs/headlessui/pull/1815))
- Add `@headlessui/tailwindcss` plugin ([#1487](https://github.com/tailwindlabs/headlessui/pull/1487))

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,44 @@ describe('Rendering', () => {
})
)

it(
'should be possible to compare null values by a field',
suppressConsoleLogs(async () => {
render(
<Combobox value={null} onChange={console.log} by="id">
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
{options.map((option) => (
<Combobox.Option
key={option.id}
value={option}
className={(info) => JSON.stringify(info)}
>
{option.name}
</Combobox.Option>
))}
</Combobox.Options>
</Combobox>
)

await click(getComboboxButton())

let [alice, bob, charlie] = getComboboxOptions()
expect(alice).toHaveAttribute(
'class',
JSON.stringify({ active: true, selected: false, disabled: false })
)
expect(bob).toHaveAttribute(
'class',
JSON.stringify({ active: false, selected: false, disabled: false })
)
expect(charlie).toHaveAttribute(
'class',
JSON.stringify({ active: false, selected: false, disabled: false })
)
})
)

it(
'should be possible to compare objects by a field',
suppressConsoleLogs(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ function ComboboxFn<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_T
typeof by === 'string'
? (a, z) => {
let property = by as unknown as keyof TValue
return a[property] === z[property]
return a?.[property] === z?.[property]
}
: by
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,39 @@ describe('Rendering', () => {
})
)

it(
'should be possible to compare null values by a field',
suppressConsoleLogs(async () => {
render(
<RadioGroup value={null} onChange={console.log} by="id">
{options.map((option) => (
<RadioGroup.Option
key={option.id}
value={option}
className={(info) => JSON.stringify(info)}
>
{option.name}
</RadioGroup.Option>
))}
</RadioGroup>
)

let [alice, bob, charlie] = getRadioGroupOptions()
expect(alice).toHaveAttribute(
'class',
JSON.stringify({ checked: false, disabled: false, active: false })
)
expect(bob).toHaveAttribute(
'class',
JSON.stringify({ checked: false, disabled: false, active: false })
)
expect(charlie).toHaveAttribute(
'class',
JSON.stringify({ checked: false, disabled: false, active: false })
)
})
)

it(
'should be possible to compare objects by a field',
suppressConsoleLogs(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ let RadioGroupRoot = forwardRefWithAs(function RadioGroup<
typeof by === 'string'
? (a: TType, z: TType) => {
let property = by as unknown as keyof TType
return a[property] === z[property]
return a?.[property] === z?.[property]
}
: by
)
Expand Down
2 changes: 1 addition & 1 deletion packages/@headlessui-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `by` prop for `Listbox`, `Combobox` and `RadioGroup` ([#1482](https://github.com/tailwindlabs/headlessui/pull/1482), [#1717](https://github.com/tailwindlabs/headlessui/pull/1717), [#1814](https://github.com/tailwindlabs/headlessui/pull/1814))
- Add `by` prop for `Listbox`, `Combobox` and `RadioGroup` ([#1482](https://github.com/tailwindlabs/headlessui/pull/1482), [#1717](https://github.com/tailwindlabs/headlessui/pull/1717), [#1814](https://github.com/tailwindlabs/headlessui/pull/1814), [#1815](https://github.com/tailwindlabs/headlessui/pull/1815))
- Add `@headlessui/tailwindcss` plugin ([#1487](https://github.com/tailwindlabs/headlessui/pull/1487))

### Fixed
Expand Down
39 changes: 39 additions & 0 deletions packages/@headlessui-vue/src/components/combobox/combobox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,45 @@ describe('Rendering', () => {
})
)

it(
'should be possible to compare null values by a field',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Combobox v-model="value" by="id">
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption
v-for="option in options"
:key="option.id"
:value="option"
v-slot="data"
>{{ JSON.stringify(data) }}</ComboboxOption
>
</ComboboxOptions>
</Combobox>
`,
setup: () => {
let value = ref(null)
return { options, value }
},
})

await click(getComboboxButton())

let [alice, bob, charlie] = getComboboxOptions()
expect(alice).toHaveTextContent(
JSON.stringify({ active: true, selected: false, disabled: false })
)
expect(bob).toHaveTextContent(
JSON.stringify({ active: false, selected: false, disabled: false })
)
expect(charlie).toHaveTextContent(
JSON.stringify({ active: false, selected: false, disabled: false })
)
})
)

it(
'should be possible to compare objects by a field',
suppressConsoleLogs(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export let Combobox = defineComponent({
compare(a: any, z: any) {
if (typeof props.by === 'string') {
let property = props.by as unknown as any
return a[property] === z[property]
return a?.[property] === z?.[property]
}
return props.by(a, z)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,41 @@ describe('Rendering', () => {
})
)

it(
'should be possible to compare null values by a field',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<RadioGroup v-model="value" by="id">
<RadioGroupButton>Trigger</RadioGroupButton>
<RadioGroupOption
v-for="option in options"
:key="option.id"
:value="option"
v-slot="data"
>{{ JSON.stringify(data) }}</RadioGroupOption
>
</RadioGroup>
`,
setup: () => {
let value = ref(null)
return { options, value }
},
})

let [alice, bob, charlie] = getRadioGroupOptions()
expect(alice).toHaveTextContent(
JSON.stringify({ checked: false, disabled: false, active: false })
)
expect(bob).toHaveTextContent(
JSON.stringify({ checked: false, disabled: false, active: false })
)
expect(charlie).toHaveTextContent(
JSON.stringify({ checked: false, disabled: false, active: false })
)
})
)

it(
'should be possible to compare objects by a field',
suppressConsoleLogs(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export let RadioGroup = defineComponent({
compare(a: any, z: any) {
if (typeof props.by === 'string') {
let property = props.by as unknown as any
return a[property] === z[property]
return a?.[property] === z?.[property]
}
return props.by(a, z)
},
Expand Down