-
-
Notifications
You must be signed in to change notification settings - Fork 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
Svelte 5: <select>
/<option>
elements get incorrect/inconsistent values when applying undefined
with spread or reactive value
#11616
Comments
undefined
with prop or spreadundefined
with prop or spread
undefined
with prop or spread<select>
/<option>
elements get incorrect/inconsistent values when applying undefined
with prop or spread
<select>
/<option>
elements get incorrect/inconsistent values when applying undefined
with prop or spread<select>
/<option>
elements get incorrect/inconsistent values when applying undefined
with spread or reactive value
The problem here is that nullish values ( What's the use case for setting the value of an option to Svelte 4 has the same problem for spread, works in the other cases though. |
I do this in order to set a variable to
Somewhere in a docs app there was a preview that uses a select element to switch between the different prop values, something like:
The
After reading this I went back to try it again, and to my confusion it wasn't working, and when I tried to reproduce it in the REPL it was working. In my application the I also started playing around with inputs with Personally I get why nullish attributes are removed, it makes sense in most cases to provide a convenience API. My main concern isn't what is "technically" correct, but rather the inconsistent behaviour between different scenarios, especially the scenario in the REPLs above seems odd (I would guess that's a bug?). The inconsistency between non-reactive values, reactive values, and spread makes things a bit confusing, at least for me. |
I'm currently working around this by using an empty string as a value:
In my opinion not the prettiest, but it works for my specific use case. |
Describe the bug
<select>
and<option>
elements are getting incorrect/inconsistentvalue
attribute when the value is nullish when using dynamic assignment of the value attribute or when using object spread.It seems to be due to how nullish values are applied to the
<option>
element. I'm running into the following scenario's:<option value={undefined}>
renders with an emptyvalue
attribute.<option {value}>
with a non-reactive variable renders with an emptyvalue
attribute.<option {value}>
with a reactive variable renders without avalue
attribute.<option {...value}>
or<option {...{value}}>
with a non-reactive variable renders withoutvalue
attribute.<option {...value}>
or<option {...{value}}>
with a reactive variable renders withoutvalue
attribute.Which makes it impossible to set the value of an
<option>
toundefined
ornull
in specific scenarios, since thevalue
prop of anHTMLOptionElement
fallback to the label when novalue
attribute is provided.This is causing troubles when using
<select bind:value>
with a default value (nullish), on initial mount the value will be nullish, once mounted the value is updated to the label of whatever the associated<option>
has.I have not tested this, but I would assume similar scenarios might apply to checkbox and radio inputs.
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE7WST0vEMBDFv0oIHnahtoe91WxBEI8e9Gg9ZJupG8gmJZkWJfS7S5p122X_aAWPmfdm-L1HPK2lAkfzV0813wHN6X3T0ITiZxMergOFQBPqTGurMGGusrLBotQlVkY7JB1XLTiyJjcOOcLCB6nEWn6AyEmrBdRSg0jiWBttgVcoOzgVLyuTNddY4GcuX9f75V2pR-jJPbIercGDCpBMxH2ssM-yMb1m21URs7Nsu4ojIbviMQSPreTEv6CV-n0RjelQyrJnWTCWmjlQUCHZSC3ywbL2U2c_1MxMg9JosjccYPviAWreKmRZdBQD4XByxHky-vaQpuNW8o06BZv08Su8if8s5JH-G8znHxFn8V2Fm0d2VOD357pcX3TMLTFuHdP6NE39_h9N-5xZ6AXkP_D-AyxN6M4IWUsQNEfbQv_WfwEXGNU4lQQAAA==
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: