-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Two-way binding + reactive declaration behaviour differs between objects and strings #4613
Comments
I can't answer your questions specifically, but I can tell you that binding to a reactive declaration isn't a supported way of doing things. Reactive declarations are supposed to be read-only. I'm not surprised that it doesn't work predictably. There's quite a nice autocomplete component here: http://simple-svelte-autocomplete.surge.sh/ which might be worth studying. |
I apologize, I misunderstood your example. You just want the input to be editable, so that when the user types in it, you can update the list of fruits (like a search field, not like a tool for renaming fruits 🤦). In that case you can remove the part about renaming fruits and instead update the fruit list / typeahead options. |
duplicate #4448 |
dupe |
I think this might be a bug, but I'm not sure. It could be by design.
I'm attempting to build a "ComboBox" (a.k.a "typeahead" or "autocomplete") component.
The kind of component where a user can start typing into an input field, and a list of matches/suggestions appears.
The user may continue typing, or they may choose a suggestion from the list.
The component has internal state (
value
) that can potentially update in two ways, either:To account for this, I initially tried having
value
be both a two-way binding and a reactive declaration, e.g.This works fine. You can type anything into the input field, but if you click an item in the list, the input value updates to the clicked item.
REPL: https://svelte.dev/repl/468ccba36afa48b29525581ad2694d2b?version=3.20.1
Now let's make three small changes:
fruits
array from simple string values to objects.name
to the reactive declaration (value = selectedFruit.name
).name
to the text content of the<li>
It is now no longer possible to type into the input field. It seems that the reactive declaration keeps resetting the value back immediately.
REPL: https://svelte.dev/repl/9f48844e524b4b97a3865453437edc07?version=3.20.1
Finally, lets change from a two-way binding to a one-way binding plus an
on:input
handler:...and now everything works again.
https://svelte.dev/repl/78f80085c7ec420ebde425c555326ef2?version=3.20.1
I'm interested in understanding why:
on:input
handler worksThe text was updated successfully, but these errors were encountered: