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

Allow the by prop (as a string) to handle null values #1814

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@headlessui-react/CHANGELOG.md
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))
- 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 `@headlessui/tailwindcss` plugin ([#1487](https://github.com/tailwindlabs/headlessui/pull/1487))

### Fixed
Expand Down
91 changes: 91 additions & 0 deletions packages/@headlessui-react/src/components/listbox/listbox.test.tsx
Expand Up @@ -264,6 +264,97 @@ describe('Rendering', () => {
})
)

it(
'should be possible to use the by prop (as a string) with a null initial value',
suppressConsoleLogs(async () => {
function Example() {
let [value, setValue] = useState(null)

return (
<Listbox value={value} onChange={setValue} by="id">
<Listbox.Button>Trigger</Listbox.Button>
<Listbox.Options>
<Listbox.Option value={{ id: 1, name: 'alice' }}>alice</Listbox.Option>
<Listbox.Option value={{ id: 2, name: 'bob' }}>bob</Listbox.Option>
<Listbox.Option value={{ id: 3, name: 'charlie' }}>charlie</Listbox.Option>
</Listbox.Options>
</Listbox>
)
}

render(<Example />)

await click(getListboxButton())
let [alice, bob, charlie] = getListboxOptions()
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'false')

await click(getListboxOptions()[2])
await click(getListboxButton())
;[alice, bob, charlie] = getListboxOptions()
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'true')

await click(getListboxOptions()[1])
await click(getListboxButton())
;[alice, bob, charlie] = getListboxOptions()
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'true')
expect(charlie).toHaveAttribute('aria-selected', 'false')
})
)

// TODO: Does this test prove anything useful?
it(
'should be possible to use the by prop (as a string) with a null listbox option',
suppressConsoleLogs(async () => {
function Example() {
let [value, setValue] = useState(null)

return (
<Listbox value={value} onChange={setValue} by="id">
<Listbox.Button>Trigger</Listbox.Button>
<Listbox.Options>
<Listbox.Option value={null} disabled>
Please select an option
</Listbox.Option>
<Listbox.Option value={{ id: 1, name: 'alice' }}>alice</Listbox.Option>
<Listbox.Option value={{ id: 2, name: 'bob' }}>bob</Listbox.Option>
<Listbox.Option value={{ id: 3, name: 'charlie' }}>charlie</Listbox.Option>
</Listbox.Options>
</Listbox>
)
}

render(<Example />)

await click(getListboxButton())
let [disabled, alice, bob, charlie] = getListboxOptions()
expect(disabled).toHaveAttribute('aria-selected', 'true')
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'false')

await click(getListboxOptions()[3])
await click(getListboxButton())
;[disabled, alice, bob, charlie] = getListboxOptions()
expect(disabled).toHaveAttribute('aria-selected', 'false')
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'true')

await click(getListboxOptions()[2])
await click(getListboxButton())
;[disabled, alice, bob, charlie] = getListboxOptions()
expect(disabled).toHaveAttribute('aria-selected', 'false')
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'true')
expect(charlie).toHaveAttribute('aria-selected', 'false')
})
)

it(
'should be possible to use completely new objects while rendering (single mode)',
suppressConsoleLogs(async () => {
Expand Down
Expand Up @@ -353,7 +353,7 @@ let ListboxRoot = forwardRefWithAs(function Listbox<
typeof by === 'string'
? (a: TActualType, z: TActualType) => {
let property = by as unknown as keyof TActualType
return a[property] === z[property]
return a?.[property] === z?.[property]
}
: by
),
Expand Down
2 changes: 1 addition & 1 deletion packages/@headlessui-vue/CHANGELOG.md
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))
- 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 `@headlessui/tailwindcss` plugin ([#1487](https://github.com/tailwindlabs/headlessui/pull/1487))

### Fixed
Expand Down
89 changes: 89 additions & 0 deletions packages/@headlessui-vue/src/components/listbox/listbox.test.tsx
Expand Up @@ -289,6 +289,95 @@ describe('Rendering', () => {
})
)

it(
'should be possible to use the by prop (as a string) with a null initial value',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Listbox v-model="value" by="id">
<ListboxButton>Trigger</ListboxButton>
<ListboxOptions>
<ListboxOption :value="{ id: 1, name: 'alice' }">alice</ListboxOption>
<ListboxOption :value="{ id: 2, name: 'bob' }">bob</ListboxOption>
<ListboxOption :value="{ id: 3, name: 'charlie' }">charlie</ListboxOption>
</ListboxOptions>
</Listbox>
`,
setup: () => {
let value = ref(null)
return { options, value }
},
})

await click(getListboxButton())
let [alice, bob, charlie] = getListboxOptions()
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'false')

await click(getListboxOptions()[2])
await click(getListboxButton())
;[alice, bob, charlie] = getListboxOptions()
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'true')

await click(getListboxOptions()[1])
await click(getListboxButton())
;[alice, bob, charlie] = getListboxOptions()
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'true')
expect(charlie).toHaveAttribute('aria-selected', 'false')
})
)

// TODO: Does this test prove anything useful?
it(
'should be possible to use the by prop (as a string) with a null listbox option',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Listbox v-model="value" by="id">
<ListboxButton>Trigger</ListboxButton>
<ListboxOptions>
<ListboxOption :value="null" disabled>Please select an option</ListboxOption>
<ListboxOption :value="{ id: 1, name: 'alice' }">alice</ListboxOption>
<ListboxOption :value="{ id: 2, name: 'bob' }">bob</ListboxOption>
<ListboxOption :value="{ id: 3, name: 'charlie' }">charlie</ListboxOption>
</ListboxOptions>
</Listbox>
`,
setup: () => {
let value = ref(null)
return { options, value }
},
})

await click(getListboxButton())
let [disabled, alice, bob, charlie] = getListboxOptions()
expect(disabled).toHaveAttribute('aria-selected', 'true')
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'false')

await click(getListboxOptions()[3])
await click(getListboxButton())
;[disabled, alice, bob, charlie] = getListboxOptions()
expect(disabled).toHaveAttribute('aria-selected', 'false')
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'false')
expect(charlie).toHaveAttribute('aria-selected', 'true')

await click(getListboxOptions()[2])
await click(getListboxButton())
;[disabled, alice, bob, charlie] = getListboxOptions()
expect(disabled).toHaveAttribute('aria-selected', 'false')
expect(alice).toHaveAttribute('aria-selected', 'false')
expect(bob).toHaveAttribute('aria-selected', 'true')
expect(charlie).toHaveAttribute('aria-selected', 'false')
})
)

it(
'should be possible to use completely new objects while rendering (single mode)',
suppressConsoleLogs(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@headlessui-vue/src/components/listbox/listbox.ts
Expand Up @@ -180,7 +180,7 @@ export let Listbox = 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