Skip to content

Commit f0bcb33

Browse files
author
Rich Harris
committed
update select multiple exercise
1 parent c65a55f commit f0bcb33

File tree

4 files changed

+27
-92
lines changed

4 files changed

+27
-92
lines changed

content/tutorial/01-svelte/06-bindings/06-multiple-select-bindings/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
title: Select multiple
33
---
44

5-
A select can have a `multiple` attribute, in which case it will populate an array rather than selecting a single value.
5+
A `<select>` element can have a `multiple` attribute, in which case it will populate an array rather than selecting a single value.
66

7-
Returning to our [earlier ice cream example](/tutorial/group-inputs), we can replace the checkboxes with a `<select multiple>`:
7+
Replace the checkboxes with a `<select multiple>`:
88

99
```svelte
1010
/// file: App.svelte
1111
<h2>Flavours</h2>
1212
13-
<select multiple bind:value={flavours}>
14-
{#each menu as flavour}
15-
<option value={flavour}>
16-
{flavour}
17-
</option>
13+
+++<select multiple bind:value={flavours}>+++
14+
{#each ['cookies and cream', 'mint choc chip', 'raspberry ripple'] as flavour}
15+
+++ <option>{flavour}</option>+++
1816
{/each}
19-
</select>
17+
+++</select>+++
2018
```
2119

22-
> Press and hold the `control` key (or the `command` key on MacOS) for selecting multiple options.
20+
Note that we're able to omit the `value` attribute on the `<option>`, since the value is identical to the element's contents.
21+
22+
> Press and hold the `control` key (or the `command` key on MacOS) to select multiple options.

content/tutorial/01-svelte/06-bindings/06-multiple-select-bindings/app-a/src/lib/App.svelte

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
11
<script>
22
let scoops = 1;
3-
let flavours = ['Mint choc chip'];
3+
let flavours = [];
44
5-
let menu = [
6-
'Cookies and cream',
7-
'Mint choc chip',
8-
'Raspberry ripple'
9-
];
10-
11-
function join(flavours) {
12-
if (flavours.length === 1) return flavours[0];
13-
return `${flavours.slice(0, -1).join(', ')} and ${flavours[flavours.length - 1]}`;
14-
}
5+
const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
156
</script>
167

178
<h2>Size</h2>
189

19-
<label>
20-
<input type=radio bind:group={scoops} value={1}>
21-
One scoop
22-
</label>
23-
24-
<label>
25-
<input type=radio bind:group={scoops} value={2}>
26-
Two scoops
27-
</label>
10+
{#each [1, 2, 3] as number}
11+
<label>
12+
<input
13+
type="radio"
14+
name="scoops"
15+
value={number}
16+
bind:group={scoops}
17+
/>
2818

29-
<label>
30-
<input type=radio bind:group={scoops} value={3}>
31-
Three scoops
32-
</label>
19+
{number} {number === 1 ? 'scoop' : 'scoops'}
20+
</label>
21+
{/each}
3322

3423
<h2>Flavours</h2>
3524

3625
<select multiple bind:value={flavours}>
37-
{#each menu as flavour}
38-
<option value={flavour}>
39-
{flavour}
40-
</option>
26+
{#each ['cookies and cream', 'mint choc chip', 'raspberry ripple'] as flavour}
27+
<option>{flavour}</option>
4128
{/each}
4229
</select>
4330

@@ -48,6 +35,6 @@
4835
{:else}
4936
<p>
5037
You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
51-
of {join(flavours)}
38+
of {formatter.format(flavours)}
5239
</p>
53-
{/if}
40+
{/if}

content/tutorial/common/src/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
background: var(--bg-1);
116116
}
117117

118-
select {
118+
select:not([multiple]) {
119119
background: var(--bg-2);
120120
}
121121

0 commit comments

Comments
 (0)