Skip to content

Commit 0ee5c83

Browse files
authored
docs: add shared state example for bind:group
1 parent ed26c3f commit 0ee5c83

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

documentation/docs/03-template-syntax/11-bind.md

+46-7
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,58 @@ Inputs that work together can use `bind:group`.
130130
</script>
131131
132132
<!-- grouped radio inputs are mutually exclusive -->
133-
<input type="radio" bind:group={tortilla} value="Plain" />
134-
<input type="radio" bind:group={tortilla} value="Whole wheat" />
135-
<input type="radio" bind:group={tortilla} value="Spinach" />
133+
<label><input type="radio" bind:group={tortilla} value="Plain" />Plain</label>
134+
<label><input type="radio" bind:group={tortilla} value="Whole wheat" />Whole wheat</label>
135+
<label><input type="radio" bind:group={tortilla} value="Spinach" />Spinach</label>
136136
137137
<!-- grouped checkbox inputs populate an array -->
138-
<input type="checkbox" bind:group={fillings} value="Rice" />
139-
<input type="checkbox" bind:group={fillings} value="Beans" />
140-
<input type="checkbox" bind:group={fillings} value="Cheese" />
141-
<input type="checkbox" bind:group={fillings} value="Guac (extra)" />
138+
<label><input type="checkbox" bind:group={fillings} value="Rice" /> Rice</label>
139+
<label><input type="checkbox" bind:group={fillings} value="Beans" />Beans</label>
140+
<label><input type="checkbox" bind:group={fillings} value="Cheese" /> Cheese</label>
141+
<label><input type="checkbox" bind:group={fillings} value="Guac (extra)" />Guac</label>
142142
```
143143

144144
> [!NOTE] `bind:group` only works if the inputs are in the same Svelte component.
145145
146+
`bind:group` can be used with component props with help of [bind-property](#bind:property-for-components):
147+
148+
```javascript
149+
// sharedState.svelte.js
150+
export const tortilla = $state({selectedValue: ""});
151+
export const fillings = $state({selectedValues: []});
152+
```
153+
154+
```svelte
155+
<!-- App.svelte -->
156+
<script>
157+
import { tortilla, fillings } from './sharedState.svelte.js';
158+
import Selection from './Selection.svelte';
159+
</script>
160+
161+
<Selection bind:tortilla={tortilla.selectedValue} bind:fillings={fillings.selectedValues} />
162+
```
163+
164+
```svelte
165+
<!-- Selection.svelte -->
166+
<script>
167+
let { tortilla = $bindable(), fillings = $bindable() } = $props();
168+
</script>
169+
170+
<!-- grouped radio inputs are mutually exclusive -->
171+
<label><input type="radio" bind:group={tortilla} value="Plain" />Plain</label>
172+
<label><input type="radio" bind:group={tortilla} value="Whole wheat" />Whole wheat</label>
173+
<label><input type="radio" bind:group={tortilla} value="Spinach" />Spinach</label>
174+
175+
<!-- grouped checkbox inputs populate an array -->
176+
<label><input type="checkbox" bind:group={fillings} value="Rice" /> Rice</label>
177+
<label><input type="checkbox" bind:group={fillings} value="Beans" />Beans</label>
178+
<label><input type="checkbox" bind:group={fillings} value="Cheese" /> Cheese</label>
179+
<label><input type="checkbox" bind:group={fillings} value="Guac (extra)" />Guac</label>
180+
```
181+
182+
> [!NOTE] `bind:group` only works if the inputs are in the same Svelte component, you can't pass down a single $state to multiple components (with different values).
183+
184+
146185
## `<input bind:files>`
147186

148187
On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList). When you want to update the files programmatically, you always need to use a `FileList` object. Currently `FileList` objects cannot be constructed directly, so you need to create a new [`DataTransfer`](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer) object and get `files` from there.

0 commit comments

Comments
 (0)