Skip to content

Commit

Permalink
Listbox compare null value bugfix (#3962)
Browse files Browse the repository at this point in the history
* Add test

* Handle null values in listbox __compare
  • Loading branch information
gdebrauwer authored Jan 21, 2024
1 parent 2a64880 commit 4fceb4c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
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

0 comments on commit 4fceb4c

Please sign in to comment.