-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
select option with default selection.... broken in 3.42? #6954
Comments
The repro doesn't use a |
The code in the message wasn't displaying correctly because it wasn't wrapped in code blocks - I've just edited the original message. |
Pasted OP's code into a REPL, and found that https://svelte.dev/repl/01e1ffe5302d4ed19a9f180bcd87dbe4?version=3.42.1 works but https://svelte.dev/repl/01e1ffe5302d4ed19a9f180bcd87dbe4?version=3.42.2 does not. (Here "works" means "select element shows a value"). |
Probably caused by #6170, as that's one of the PRs mentioned in the 3.42.2 changelog. |
In fact, now that I look closely at the repro code, this is certainly caused by #6170, and deliberately so. The select options are: let options = [
{ id: "4", value: "2019" },
{ id: "3", value: "2018" },
{ id: "2", value: "2017" },
{ id: "1", value: "2016" },
]; And the selected value is |
Thanks for fixing my post. Didn't realize I need to post it in a code block. |
@rmunn no, in that case, I agree that I typoed that but if you change it to a correct option it still has the issue. updated |
I found the cause. The option.selected = option_selected_value = /*selected*/ ctx[0].id === /*option*/ ctx[3].id; However, svelte/src/runtime/internal/dom.ts Lines 536 to 547 in 03ef0e4
I think the way to fix this is to support JavaScript expressions in |
really I am just looking for a way to set a select box to edit a record that has already been selected. Each select option needs to be an object though. If there is a better way to do that? |
This is one of the workaround. https://svelte.dev/repl/9e1d77dc021e410db95958e53c3b416d?version=3.44.2 |
Then what is being loaded from db wouldn't match. Your storing an object but looking up a string. |
I was able to get a workaround with this though. https://svelte.dev/repl/e4698e605aca48d8937cb1ee32220835?version=3.44.2 |
https://svelte.dev/repl/5699ad3517074fe8a7333ae52880b255?version=3.44.2 or use codepen, inspect the HTML and check the selected attr value both are strings so select takes the last value, #6874 is the PR to fix the regression which checks if |
using the primitive type value to check will fix the behaviour in both versions but not sure about |
@RaiVaibhav but I don't want it to go back to the default one I want it to go to the correct one. I created a more real example of what I am trying to do here: https://svelte.dev/repl/e4698e605aca48d8937cb1ee32220835?version=3.44.2. With @baseballyama's workaround, I can set additional properties of the object but in the case of an object that has 4+ properties, it isn't that useful. |
IMO, in general, it would be better to use the Nevertheless, I think it would be useful to have object type support. |
SO I'm not sure if I did something incorrectly or if there is another issue with select somehow. I took @baseballyama advice and change to a on:change event. That seemed to work at first glance but if you change the select multiple time is starts giving back weird results. In this repl for example. If you chanhge it to "unarmed" Everything works as it should but if you then changhe it back to the starting value of "Horrible Claws & Teeth" it doesn't update correctly. That being said I am not sure why this is happening but I would doubt it is related to the same select issue. Man this is frustrating is seems like it should be so easy to do this. I might be better off just using vue? I check out that repl and that is basically what I am looking for. |
I'm not sure about your specific use-case. You use property of object for |
This is expected behavior, IMO. It works fine to bind to an object, but the Basically this line: let selected = { id: "2", value: "2017" }; should be replaced with: $: selected = options.find(o => o.id === "2"); |
Closing as "works as designed". Whether or not the value is selected is determined by object equality ( |
Describe the bug
This looks to work in 3.41 and before but breaks at 3.42 or newer. Docs don't mention it at all so it is hard to trouble shoot.
Reproduction
Run this code.
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: