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: reset input on init so default value is displayed #3881

Merged
merged 4 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
3 changes: 3 additions & 0 deletions packages/ui/src/combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ function handleRoot(el, Alpine) {
// to settle up currently selected Values (this prevents this next bit
// of code from running multiple times on startup...)
queueMicrotask(() => {
// Set initial combobox values in the input...
queueMicrotask(() => this.__resetInput())

Alpine.effect(() => {
// Everytime the value changes, we need to re-render the hidden inputs,
// if a user passed the "name" prop...
Expand Down
64 changes: 63 additions & 1 deletion tests/cypress/integration/plugins/ui/combobox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,68 @@ test('it works with x-model',
},
)

test('initial value is set from x-model',
[html`
<div
x-data="{
query: '',
selected: { id: 1, name: 'Wade Cooper' },
people: [
{ id: 1, name: 'Wade Cooper' },
],
get filteredPeople() {
return this.query === ''
? this.people
: this.people.filter((person) => {
return person.name.toLowerCase().includes(this.query.toLowerCase())
})
},
}"
>
<div x-combobox x-model="selected">
<label x-combobox:label>Select person</label>

<div>
<div>
<input
x-combobox:input
:display-value="person => person.name"
@change="query = $event.target.value"
placeholder="Search..."
/>

<button x-combobox:button>Toggle</button>
</div>

<div x-combobox:options>
<ul>
<template
x-for="person in filteredPeople"
:key="person.id"
hidden
>
<li
x-combobox:option
:option="person.id"
:value="person"
:disabled="person.disabled"
x-text="person.name"
>
</li>
</template>
</ul>
</div>
</div>

<article x-text="selected?.name"></article>
</div>
</div>
`],
({ get }) => {
get('input').should(haveValue('Wade Cooper'))
},
)

test('it works with internal state',
[html`
<div
Expand Down Expand Up @@ -581,7 +643,7 @@ test('"multiple" prop',
>
<label x-combobox:label>Assigned to</label>

<input x-combobox:input :display-value="(person) => person.name" type="text">
<input x-combobox:input type="text">
<button x-combobox:button x-text="$combobox.value ? $combobox.value.join(',') : 'Select People'"></button>

<ul x-combobox:options>
Expand Down
Loading