Skip to content

Commit 172a9db

Browse files
committed
docs: added example of the 'multiple' option to listbox
1 parent 6283794 commit 172a9db

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/lib/components/listbox/Listbox.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
horizontal?: boolean;
6767
/** The selected value */
6868
value?: StateDefinition["value"];
69-
/** Wether the `Listbox` should allow mutliple selections */
69+
/** Whether the `Listbox` should allow mutliple selections */
7070
multiple?: boolean;
7171
};
7272
</script>

src/routes/docs/listbox.svx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,44 @@ You can use these states to conditionally apply whatever active/focus styles you
9999
</Listbox>
100100
```
101101

102+
## Multiple Values
103+
104+
To allow selecting multiple values in your listbox, use the `multiple` prop and pass an array to `value` instead of a single option.
105+
106+
```svelte
107+
<script>
108+
import {
109+
Listbox,
110+
ListboxButton,
111+
ListboxLabel,
112+
ListboxOptions,
113+
ListboxOption,
114+
} from "@rgossiaux/svelte-headlessui";
115+
116+
const people = [
117+
{ id: 1, name: "Durward Reynolds" },
118+
{ id: 2, name: "Kenton Towne" },
119+
{ id: 3, name: "Therese Wunsch" },
120+
{ id: 4, name: "Benedict Kessler" },
121+
{ id: 5, name: "Katelyn Rohan" },
122+
];
123+
124+
let selectedPeople = [people[0], people[1]];
125+
</script>
126+
127+
<Listbox value={selectedPeople} on:change={(e) => (selectedPeople = e.detail)} multiple>
128+
<ListboxLabel>Assignee:</ListboxLabel>
129+
<ListboxButton>{selectedPeople.map((person) => person.name).join(', ')}</ListboxButton>
130+
<ListboxOptions>
131+
{#each people as person (person.id)}
132+
<ListboxOption value={person}>
133+
{person.name}
134+
</ListboxOption>
135+
{/each}
136+
</ListboxOptions>
137+
</Listbox>
138+
```
139+
102140
## Using a custom label
103141

104142
By default, the `Listbox` will use the `<ListboxButton>` contents as the label for screenreaders. If you'd like more control over what is announced to assistive technologies, use the `ListboxLabel` component:

0 commit comments

Comments
 (0)