konstaui / konsta

Mobile UI components made with Tailwind CSS
https://konstaui.com
MIT License
3.55k stars 131 forks source link

List Input type Select ignores default value and only takes the first argument. #125

Closed Antoine-lb closed 1 year ago

Antoine-lb commented 1 year ago

Check that this is really a bug

Reproduction link

https://stackblitz.com/edit/konsta-svelte-osskwt?file=App.svelte

Bug description

List Input type Select ignores default value and only takes the first argument.

Expected Behavior

Uses the default value

Actual Behavior

Takes the first <option> as the default selected

Konsta UI version

1.0.2

Platform/Target and Browser Versions

Chrome macOS

Validations

Would you like to open a PR for this bug?

ibilux commented 1 year ago

@Antoine-lb You need to use selected attribute to indicate that the option is initially selected.

So you need to change the line 37 in the example you provided like this:

<option value="Male">Male</option>

to:

<option value="Male" selected>Male</option>
laryhills commented 1 year ago

@ibilux @Antoine-lb You can also do this for a list of options

let defaultValue = "Male"
{#each gender as list_of_genders}
    {#if gender == defaultValue}
        <option value={gender} selected>{gender}</option>
    {:else}
    <option value={gender}>{gender}</option>
    {/if}
{/each}
Antoine-lb commented 1 year ago

@Antoine-lb You need to use selected attribute to indicate that the option is initially selected.

So you need to change the line 37 in the example you provided like this:

<option value="Male">Male</option>

to:

<option value="Male" selected>Male</option>

It works, thanks!

This means that defaultValue is not useful

Antoine-lb commented 1 year ago

@ibilux @Antoine-lb You can also do this for a list of options

let defaultValue = "Male"
{#each gender as list_of_genders}
    {#if gender == defaultValue}
        <option value={gender} selected>{gender}</option>
    {:else}
  <option value={gender}>{gender}</option>
    {/if}
{/each}

Good solution, I made it even simpler:

let defaultValue = "Female";
let genders = ["Female", "Male"]

{#each genders as gender}
  <option value={gender} selected={gender == defaultValue}>{gender}</option>    
{/each}