Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlbeggs committed Sep 21, 2023
1 parent aa9c207 commit e179f7b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/ui/src/combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ function handleRoot(el, Alpine) {
if (typeof by === 'string') {
let property = by
by = (a, b) => {
if (typeof a !== 'object' || typeof b !== 'object') return Alpine.raw(a) === Alpine.raw(b)
if ((! a || typeof a !== 'object') || (! b || typeof b !== 'object')) {
return Alpine.raw(a) === Alpine.raw(b)
}


return a[property] === b[property];
}
Expand Down
76 changes: 76 additions & 0 deletions tests/cypress/integration/plugins/ui/combobox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,82 @@ test('"by" prop with string value',
},
);

test('"by" prop with string value and "nullable"',
[html`
<div
x-data="{
people: [
{ id: 1, name: 'Wade Cooper' },
{ id: 2, name: 'Arlene Mccoy' },
{ id: 3, name: 'Devon Webb' },
{ 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-combobox
by="id"
default-value="5"
nullable
>
<label x-combobox:label>Assigned to</label>
<input x-combobox:input :display-value="(person) => person?.name" type="text">
<button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select People'"></button>
<ul x-combobox:options>
<template x-for="person in people" :key="person.id">
<li
:option="person.id"
x-combobox:option
:value="person"
:disabled="person.disabled"
:class="{
'selected': $comboboxOption.isSelected,
'active': $comboboxOption.isActive,
}"
>
<span x-text="person.name"></span>
</li>
</template>
</ul>
</div>
`],
({ get }) => {
get('ul').should(notBeVisible())
get('button').click()
get('ul').should(beVisible())
get('button').click()
get('ul').should(notBeVisible())
get('button').click()
get('[option="2"]').click()
get('ul').should(notBeVisible())
get('input').should(haveValue('Arlene Mccoy'))
get('button').should(haveText('Arlene Mccoy'))
get('button').click()
get('ul').should(contain('Wade Cooper'))
.should(contain('Arlene Mccoy'))
.should(contain('Devon Webb'))
get('[option="3"]').click()
get('ul').should(notBeVisible())
get('input').should(haveValue('Devon Webb'))
get('button').should(haveText('Devon Webb'))
get('button').click()
get('ul').should(contain('Wade Cooper'))
.should(contain('Arlene Mccoy'))
.should(contain('Devon Webb'))
get('[option="1"]').click()
get('ul').should(notBeVisible())
get('input').should(haveValue('Wade Cooper'))
get('button').should(haveText('Wade Cooper'))
},
);


test('keyboard controls',
[html`
<div
Expand Down

0 comments on commit e179f7b

Please sign in to comment.