-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Multiple on Listbox or Combobox doesn't work if v-model is separate from dropdown options #1766
Comments
This happens because the objects are different instances and currently comparison relies on object identity. The only reason the non-multiple version appears to work is because the value is overwritten. There is little comparison of the object itself to the current "list of values" because there's only one. I would recommend that you:
Alternatively, in our insiders build, we have a You can look here for details on the |
Thanks for that explanation. In my case, I needed to use comparisons due to some other constraints, but I agree that using the same object instance is best when possible. The If anyone else comes across this, this is where const people = [
{ id: 1, name: 'Durward Reynolds' },
{ id: 2, name: 'Kenton Towne' },
{ id: 3, name: 'Therese Wunsch' },
{ id: 4, name: 'Benedict Kessler' },
{ id: 5, name: 'Katelyn Rohan' },
];
// this now works using by
const selectedPeople = $ref([
{ id: 2, name: 'Kenton Towne', foo: 'bar' },
{ id: 5, name: 'Katelyn Rohan' },
]); The Listbox will work comparing either on id or name.
|
What package within Headless UI are you using?
"@headlessui/vue"
What version of that package are you using?
"^1.6.7"
What browser are you using?
Chrome 104
Reproduction URL
https://stackblitz.com/edit/headless-ui-listbox-multiselect-with-pinia?file=src%2Fcomponents%2FListbox.vue
Describe your issue
I need to use a multiselect with a Pinia store. Initially I thought it was a store issue, but have since discovered that v-model binding breaks (or at least half-breaks - initial values are not shown and tracked wrong after initialization) when using a separate array for the v-model.
Quick example - this works (default Headless UI example):
But if
selectedPeople
is defined separately, using the same objects of course, it breaks:In my app, I'm using a store getter to provide the dropdown options (ie.
people
in top example), but I manged to get the error even with the minimum reproduction above. I've built a Stackblitz with a full example, both using a Pinia store and a regular component.I even tried separating the
modelValue
and@update:modelValue
to use my own model updating function, but the value (val
in code example below) returned is already wrong - so the issue is caused somewhere before it gets to v-model.Is my use case just unsupported or am I missing something?
When using a regular (non-multiselect) Listbox using the same approach (store getter for options, separate array of objects for model), everything works.
Thanks.
The text was updated successfully, but these errors were encountered: