rob-balfre / svelte-select

Svelte Select. A select component for Svelte
https://svelte-select-examples.vercel.app
Other
1.27k stars 180 forks source link

input type=hidden value equals the label and not the value #383

Closed karmaral closed 2 years ago

karmaral commented 2 years ago

I'm a bit baffled, but shouldn't the hidden input for the selected option not use the label as the value? In the code it seems to be getting populated with getSelectionLabel, which returns the label.

Is this correct and I'm not using it properly?

rob-balfre commented 2 years ago

@karmaral honestly can't even remember why we added those hidden fields TBH.

What are you using them for?

karmaral commented 2 years ago

I'm using them in a form, to send data, like I'd use a default select. I gather the hidden input is just there to act as an equivalent. In Select.svelte, lines 970:990

{#if !isMulti || (isMulti && !showMultiSelect)}
    <input
        name={inputAttributes.name}
        type="hidden"
        value={value ? getSelectionLabel(value) : null} /> // this
{/if}

{#if isMulti && showMultiSelect}
    {#each value as item}
        <input
            name={inputAttributes.name}
            type="hidden"
            value={item ? getSelectionLabel(item) : null} /> // this
    {/each}
{/if}

If I change them to be like value={value ? value[optionIdentifier] : null} />

I get the exact same behaviour as if I were using a regular select. I'd say this is the way it should be. I don't see why you would pass the label as the value otherwise.

If you want I could make a PR?

rob-balfre commented 2 years ago

All good, I'll add the change to the next v5 beta as its 'technically' a breaking change.

rob-balfre commented 2 years ago

Fixed in v5