Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add shared state example for bind:group & add labels #14905

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions documentation/docs/03-template-syntax/11-bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,58 @@ Inputs that work together can use `bind:group`.
</script>

<!-- grouped radio inputs are mutually exclusive -->
<input type="radio" bind:group={tortilla} value="Plain" />
<input type="radio" bind:group={tortilla} value="Whole wheat" />
<input type="radio" bind:group={tortilla} value="Spinach" />
<label><input type="radio" bind:group={tortilla} value="Plain" />Plain</label>
<label><input type="radio" bind:group={tortilla} value="Whole wheat" />Whole wheat</label>
<label><input type="radio" bind:group={tortilla} value="Spinach" />Spinach</label>

<!-- grouped checkbox inputs populate an array -->
<input type="checkbox" bind:group={fillings} value="Rice" />
<input type="checkbox" bind:group={fillings} value="Beans" />
<input type="checkbox" bind:group={fillings} value="Cheese" />
<input type="checkbox" bind:group={fillings} value="Guac (extra)" />
<label><input type="checkbox" bind:group={fillings} value="Rice" /> Rice</label>
<label><input type="checkbox" bind:group={fillings} value="Beans" />Beans</label>
<label><input type="checkbox" bind:group={fillings} value="Cheese" /> Cheese</label>
<label><input type="checkbox" bind:group={fillings} value="Guac (extra)" />Guac</label>
```

> [!NOTE] `bind:group` only works if the inputs are in the same Svelte component.

`bind:group` can be used with component props with help of [bind-property](#bind:property-for-components):

```javascript
// sharedState.svelte.js
export const tortilla = $state({selectedValue: ""});
export const fillings = $state({selectedValues: []});
```

```svelte
<!-- App.svelte -->
<script>
import { tortilla, fillings } from './sharedState.svelte.js';
import Selection from './Selection.svelte';
</script>

<Selection bind:tortilla={tortilla.selectedValue} bind:fillings={fillings.selectedValues} />
```

```svelte
<!-- Selection.svelte -->
<script>
let { tortilla = $bindable(), fillings = $bindable() } = $props();
</script>

<!-- grouped radio inputs are mutually exclusive -->
<label><input type="radio" bind:group={tortilla} value="Plain" />Plain</label>
<label><input type="radio" bind:group={tortilla} value="Whole wheat" />Whole wheat</label>
<label><input type="radio" bind:group={tortilla} value="Spinach" />Spinach</label>

<!-- grouped checkbox inputs populate an array -->
<label><input type="checkbox" bind:group={fillings} value="Rice" /> Rice</label>
<label><input type="checkbox" bind:group={fillings} value="Beans" />Beans</label>
<label><input type="checkbox" bind:group={fillings} value="Cheese" /> Cheese</label>
<label><input type="checkbox" bind:group={fillings} value="Guac (extra)" />Guac</label>
```

> [!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).


## `<input bind:files>`

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.
Expand Down
Loading