Skip to content

Commit c0fa871

Browse files
committed
docs: improve select keys example
1 parent 6ea1e26 commit c0fa871

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

packages/site/src/routes/demo/select/_Keys.svelte

+22-32
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
Note: you need to provide a function as `key` that returns a unique string
55
for each option.
66
7-
It *must* be a string. (Hence the `${(fruit && fruit.id) || ''}` part.
8-
That returns a string for the numberic `id` field and the `null` and
9-
`undefined` values even.) If the string is empty (""), the label will stop
10-
floating when that option is selected and the component is unfocused.
11-
Therefore, the option for that value shouldn't have any text, or the
12-
floating label will overlap it.
7+
It *must* be a string. (Hence `${fruit ? fruit.id : ''}` in this
8+
example. That returns a string for the numberic `id` field and `null` and
9+
`undefined` values even.)
10+
11+
If the string is empty (""), the label will stop floating when that option
12+
is selected and the component is unfocused. Therefore, the option for that
13+
value shouldn't have any text, or the floating label will overlap it.
1314
-->
1415
<Select
15-
key={(fruit) => `${(fruit && fruit.id) || ''}`}
16+
key={(fruit) => `${fruit ? fruit.id : ''}`}
1617
bind:value={valueA}
17-
label="Standard"
18+
label="Objects"
1819
>
1920
<Option value={null} />
2021
{#each fruits as fruit (fruit.label)}
@@ -28,39 +29,28 @@
2829
</div>
2930

3031
<div>
31-
<Select
32-
variant="filled"
33-
key={(fruit) => `${(fruit && fruit.id) || ''}`}
34-
bind:value={valueB}
35-
label="Filled"
36-
>
37-
<Option value={null} />
38-
{#each fruits as fruit (fruit.label)}
39-
<Option value={fruit}>{fruit.label}</Option>
32+
<Select key={(bool) => `${bool}`} bind:value={valueB} label="Booleans">
33+
{#each [true, false] as value}
34+
<Option {value}>{value ? 'Yes' : 'No'}</Option>
4035
{/each}
4136
</Select>
4237

43-
<pre class="status">Selected: {valueB
44-
? valueB.label
45-
: 'None'}, Price: {valueB ? valueB.price : '-'}¢</pre>
38+
<pre class="status">Selected: {JSON.stringify(valueB)}</pre>
4639
</div>
4740

4841
<div>
4942
<Select
50-
variant="outlined"
51-
key={(fruit) => `${(fruit && fruit.id) || ''}`}
43+
key={(value) => `${value == null ? '' : value}`}
5244
bind:value={valueC}
53-
label="Outlined"
45+
label="Integers"
5446
>
55-
<Option value={undefined} />
56-
{#each fruits as fruit (fruit.label)}
57-
<Option value={fruit}>{fruit.label}</Option>
47+
<Option value={null} />
48+
{#each [0, 1, 2, 3, 4] as value}
49+
<Option {value}>{value}</Option>
5850
{/each}
5951
</Select>
6052

61-
<pre class="status">Selected: {valueC
62-
? valueC.label
63-
: 'None'}, Price: {valueC ? valueC.price : '-'}¢</pre>
53+
<pre class="status">Selected: {JSON.stringify(valueC)}</pre>
6454
</div>
6555
</div>
6656

@@ -75,7 +65,7 @@
7565
{ id: 4, label: 'Mango', price: 25 },
7666
];
7767
78-
let valueA: Fruit | undefined = undefined;
79-
let valueB: Fruit | undefined = undefined;
80-
let valueC: Fruit | undefined = undefined;
68+
let valueA: Fruit | null = null;
69+
let valueB: boolean = true;
70+
let valueC: number | null = null;
8171
</script>

0 commit comments

Comments
 (0)