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

Listbox compare null value bugfix #3962

Merged
merged 2 commits into from
Jan 21, 2024
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
9 changes: 8 additions & 1 deletion packages/ui/src/listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ function handleRoot(el, Alpine) {

if (typeof by === 'string') {
let property = by
by = (a, b) => a[property] === b[property]
by = (a, b) => {
// Handle null values
if ((! a || typeof a !== 'object') || (! b || typeof b !== 'object')) {
return Alpine.raw(a) === Alpine.raw(b)
}

return a[property] === b[property];
}
}

return by(a, b)
Expand Down
56 changes: 56 additions & 0 deletions tests/cypress/integration/plugins/ui/listbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,62 @@ test('"horizontal" keyboard controls',
},
)

test('"by" prop with string value',
[html`
<div
x-data="{ active: null, people: [
{ id: 1, name: 'Wade Cooper' },
{ id: 2, name: 'Arlene Mccoy' },
{ id: 3, name: 'Devon Webb', disabled: true },
{ id: 4, name: 'Tom Cook' },
{ id: 5, name: 'Tanya Fox', disabled: true },
{ id: 6, name: 'Hellen Schmidt' },
{ id: 7, name: 'Caroline Schultz' },
{ id: 8, name: 'Mason Heaney' },
{ id: 9, name: 'Claudie Smitham' },
{ id: 10, name: 'Emil Schaefer' },
]}"
x-listbox
x-model="active"
by="id"
>
<label x-listbox:label>Assigned to</label>

<button x-listbox:button x-text="active ? active.name : 'Select Person'"></button>

<ul x-listbox:options options>
<template x-for="person in people" :key="person.id">
<li
:option="person.id"
x-listbox:option
:value="person"
:disabled="person.disabled"
:class="{
'selected': $listboxOption.isSelected,
'active': $listboxOption.isActive,
}"
>
<span x-text="person.name"></span>
</li>
</template>
</ul>
</div>
`],
({ get }) => {
get('ul').should(notBeVisible())
get('button')
.should(haveText('Select Person'))
.click()
get('ul').should(beVisible())
get('button').click()
get('ul').should(notBeVisible())
get('button').click()
get('[option="2"]').click()
get('ul').should(notBeVisible())
get('button').should(haveText('Arlene Mccoy'))
},
)

test('search',
[html`
<div
Expand Down