Skip to content

Commit bc43171

Browse files
committed
docs (draft)
1 parent b44f949 commit bc43171

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ In the case of a numeric input (`type="number"` or `type="range"`), the value wi
5353

5454
If the input is empty or invalid (in the case of `type="number"`), the value is `undefined`.
5555

56+
You can give the input a default value by setting the `defaultValue` property. This way, when the input is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of the empty string. Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
57+
58+
```svelte
59+
<script>
60+
let value = $state('');
61+
</script>
62+
63+
<form>
64+
<input bind:value defaultValue="x">
65+
<input type="reset" value="Reset">
66+
</form>
67+
```
68+
5669
## `<input bind:checked>`
5770

5871
Checkbox and radio inputs can be bound with `bind:checked`:
@@ -64,16 +77,29 @@ Checkbox and radio inputs can be bound with `bind:checked`:
6477
</label>
6578
```
6679

80+
You can give the input a default value by setting the `defaultChecked` property. This way, when the input is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of `false`. Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
81+
82+
```svelte
83+
<script>
84+
let checked = $state(true);
85+
</script>
86+
87+
<form>
88+
<input type="checkbox" bind:checked defaultChecked={true}>
89+
<input type="reset" value="Reset">
90+
</form>
91+
```
92+
6793
## `<input bind:group>`
6894

6995
Inputs that work together can use `bind:group`.
7096

7197
```svelte
7298
<script>
73-
let tortilla = 'Plain';
99+
let tortilla = $state('Plain');
74100
75101
/** @type {Array<string>} */
76-
let fillings = [];
102+
let fillings = $state([]);
77103
</script>
78104
79105
<!-- grouped radio inputs are mutually exclusive -->
@@ -88,6 +114,20 @@ Inputs that work together can use `bind:group`.
88114
<input type="checkbox" bind:group={fillings} value="Guac (extra)" />
89115
```
90116

117+
You can give the group a default value by setting the `checked` property on the elements that should be selected initially. This way, when the input is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of the empty string (for radio groups) or the empty array (for checkbox groups). Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
118+
119+
```svelte
120+
<script>
121+
let tortilla = $state('Plain');
122+
</script>
123+
124+
<form>
125+
<input type="radio" bind:group={tortilla} value="Plain" checked />
126+
<input type="radio" bind:group={tortilla} value="Whole wheat" />
127+
<input type="reset" value="Reset">
128+
</form>
129+
```
130+
91131
> [!NOTE] `bind:group` only works if the inputs are in the same Svelte component.
92132
93133
## `<input bind:files>`
@@ -146,6 +186,16 @@ When the value of an `<option>` matches its text content, the attribute can be o
146186
</select>
147187
```
148188

189+
You can give the select a default value by setting the `selected` property on the elements that should be selected initially. This way, when the select is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of the empty string (for single-value selects) or the empty array (for mult-value selects). Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
190+
191+
```svelte
192+
<select bind:value={selected}>
193+
<option value={a}>a</option>
194+
<option value={b} selected>b</option>
195+
<option value={c}>c</option>
196+
</select>
197+
```
198+
149199
## `<audio>`
150200

151201
`<audio>` elements have their own set of bindings — five two-way ones...

0 commit comments

Comments
 (0)