Skip to content

Svelte 5: <select>/<option> elements get incorrect/inconsistent values when applying undefined with spread or reactive value #11616

@sheijne

Description

@sheijne

Describe the bug

<select> and <option> elements are getting incorrect/inconsistent value 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:

  1. <option value={undefined}> renders with an empty value attribute.
  2. <option {value}> with a non-reactive variable renders with an empty value attribute.
  3. <option {value}> with a reactive variable renders without a value attribute.
  4. <option {...value}> or <option {...{value}}> with a non-reactive variable renders without value attribute.
  5. <option {...value}> or <option {...{value}}> with a reactive variable renders without value attribute.

Which makes it impossible to set the value of an <option> to undefined or null in specific scenarios, since the value prop of an HTMLOptionElement fallback to the label when no value 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

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 370.41 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 8.15.6 - ~/.nvm/versions/node/v20.11.1/bin/pnpm
    bun: 1.1.4 - ~/.bun/bin/bun
  Browsers:
    Chrome: 124.0.6367.202
    Edge: 124.0.2478.97
    Safari: 17.4.1

Severity

annoyance

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions