Skip to content

Commit f8ec33b

Browse files
committed
docs: clarify the key prop in select menu with objects demo
1 parent d69b05e commit f8ec33b

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

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

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
<div class="columns margins">
22
<div>
33
<!--
4-
Note: you need to provide a function
5-
as `key` that returns a unique string
4+
Note: you need to provide a function as `key` that returns a unique string
65
for each option.
6+
7+
It *must* be a string. (Hence the `${fruit.id}` part.)
78
-->
8-
<Select
9-
key={(fruit) => (fruit && fruit.label) || ''}
10-
bind:value={valueA}
11-
label="Standard"
12-
>
9+
<Select key={(fruit) => `${fruit.id}`} bind:value={valueA} label="Standard">
1310
<Option value={null} />
1411
{#each fruits as fruit (fruit.label)}
1512
<Option value={fruit}>{fruit.label}</Option>
1613
{/each}
1714
</Select>
1815

19-
<pre
20-
class="status">Selected: {valueA ? valueA.label : 'None'}, Price: {valueA ? valueA.price : '-'}¢</pre>
16+
<pre class="status">Selected: {valueA
17+
? valueA.label
18+
: 'None'}, Price: {valueA ? valueA.price : '-'}¢</pre>
2119
</div>
2220

2321
<div>
2422
<Select
2523
variant="filled"
26-
key={(fruit) => (fruit && fruit.label) || ''}
24+
key={(fruit) => `${fruit.id}`}
2725
bind:value={valueB}
2826
label="Filled"
2927
>
@@ -33,14 +31,15 @@
3331
{/each}
3432
</Select>
3533

36-
<pre
37-
class="status">Selected: {valueB ? valueB.label : 'None'}, Price: {valueB ? valueB.price : '-'}¢</pre>
34+
<pre class="status">Selected: {valueB
35+
? valueB.label
36+
: 'None'}, Price: {valueB ? valueB.price : '-'}¢</pre>
3837
</div>
3938

4039
<div>
4140
<Select
4241
variant="outlined"
43-
key={(fruit) => (fruit && fruit.label) || ''}
42+
key={(fruit) => `${fruit.id}`}
4443
bind:value={valueC}
4544
label="Outlined"
4645
>
@@ -50,20 +49,21 @@
5049
{/each}
5150
</Select>
5251

53-
<pre
54-
class="status">Selected: {valueC ? valueC.label : 'None'}, Price: {valueC ? valueC.price : '-'}¢</pre>
52+
<pre class="status">Selected: {valueC
53+
? valueC.label
54+
: 'None'}, Price: {valueC ? valueC.price : '-'}¢</pre>
5555
</div>
5656
</div>
5757

5858
<script lang="ts">
5959
import Select, { Option } from '@smui/select';
6060
61-
type Fruit = { label: string; price: number };
61+
type Fruit = { id: number; label: string; price: number };
6262
let fruits: Fruit[] = [
63-
{ label: 'Apple', price: 35 },
64-
{ label: 'Orange', price: 38 },
65-
{ label: 'Banana', price: 28 },
66-
{ label: 'Mango', price: 25 },
63+
{ id: 1, label: 'Apple', price: 35 },
64+
{ id: 2, label: 'Orange', price: 38 },
65+
{ id: 3, label: 'Banana', price: 28 },
66+
{ id: 4, label: 'Mango', price: 25 },
6767
];
6868
6969
let valueA: Fruit | undefined = undefined;

0 commit comments

Comments
 (0)