|
4 | 4 | Note: you need to provide a function as `key` that returns a unique string
|
5 | 5 | for each option.
|
6 | 6 |
|
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. |
13 | 14 | -->
|
14 | 15 | <Select
|
15 |
| - key={(fruit) => `${(fruit && fruit.id) || ''}`} |
| 16 | + key={(fruit) => `${fruit ? fruit.id : ''}`} |
16 | 17 | bind:value={valueA}
|
17 |
| - label="Standard" |
| 18 | + label="Objects" |
18 | 19 | >
|
19 | 20 | <Option value={null} />
|
20 | 21 | {#each fruits as fruit (fruit.label)}
|
|
28 | 29 | </div>
|
29 | 30 |
|
30 | 31 | <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> |
40 | 35 | {/each}
|
41 | 36 | </Select>
|
42 | 37 |
|
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> |
46 | 39 | </div>
|
47 | 40 |
|
48 | 41 | <div>
|
49 | 42 | <Select
|
50 |
| - variant="outlined" |
51 |
| - key={(fruit) => `${(fruit && fruit.id) || ''}`} |
| 43 | + key={(value) => `${value == null ? '' : value}`} |
52 | 44 | bind:value={valueC}
|
53 |
| - label="Outlined" |
| 45 | + label="Integers" |
54 | 46 | >
|
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> |
58 | 50 | {/each}
|
59 | 51 | </Select>
|
60 | 52 |
|
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> |
64 | 54 | </div>
|
65 | 55 | </div>
|
66 | 56 |
|
|
75 | 65 | { id: 4, label: 'Mango', price: 25 },
|
76 | 66 | ];
|
77 | 67 |
|
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; |
81 | 71 | </script>
|
0 commit comments