svelteuidev / svelteui

SvelteUI Monorepo
https://svelteui.dev
MIT License
1.28k stars 64 forks source link

`NativeSelect` object value type coerced #385

Open notramo opened 1 year ago

notramo commented 1 year ago

What package has an issue

@svelteuidev/core

A clear and concise description of what the bug is

When using bind:value with NativeSelect, the assigned value is always a string. E.g. the data property contains this entry: [{ label: 'one', value: 1 }, { label: 'two', value: 2 }]. One would expect that when the "one" option is selected, the assigned value will be 1 (number), but it's "1" (string) instead.

khalibloo commented 1 year ago

NativeSelect uses the <select /> element. As such, it cannot support data types other than string. We should enforce string values even when using objects, or just keep the coercion. Mantine's version also has the same limitation.

The upcoming Select component is probably what you need if the value type is important.

Edit: Looks like I might be wrong about that, as far as Svelte is concerned. https://svelte.dev/tutorial/select-bindings The note indicates that in Svelte, <select /> is fine with non-string values.

BeeMargarida commented 1 year ago

It's not yet clear if this is something we want to do so this has been left in the backlog for now. Svelte allows it, so it's definitely something we could change

notramo commented 1 year ago

This is really annoying. If the API provides a number in an object property, we can't use bind:value, so we have to use value={product.price} and on:change={event => product.price = parseInt(event.target.value)}.