fix: update error message to clarify usage with radio inputs (#16874)

* fix: update  error message to clarify usage with radio inputs

* show an extra bind checked error when a radio type

* typo

* replace 'group' binding with , drive-by fix some other messages

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/16877/head
Yuki Ishii 2 days ago committed by GitHub
parent 31541c8849
commit 4f653ddcea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: update `bind:checked` error message to clarify usage with radio inputs

@ -95,7 +95,7 @@ Since 5.6.0, if an `<input>` has a `defaultValue` and is part of a form, it will
## `<input bind:checked>` ## `<input bind:checked>`
Checkbox and radio inputs can be bound with `bind:checked`: Checkbox inputs can be bound with `bind:checked`:
```svelte ```svelte
<label> <label>
@ -117,6 +117,8 @@ Since 5.6.0, if an `<input>` has a `defaultChecked` attribute and is part of a f
</form> </form>
``` ```
> [!NOTE] Use `bind:group` for radio inputs instead of `bind:checked`.
## `<input bind:indeterminate>` ## `<input bind:indeterminate>`
Checkboxes can be in an [indeterminate](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/indeterminate) state, independently of whether they are checked or unchecked: Checkboxes can be in an [indeterminate](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/indeterminate) state, independently of whether they are checked or unchecked:

@ -33,7 +33,7 @@ export function BindDirective(node, context) {
e.bind_invalid_target( e.bind_invalid_target(
node, node,
node.name, node.name,
property.valid_elements.map((valid_element) => `<${valid_element}>`).join(', ') property.valid_elements.map((valid_element) => `\`<${valid_element}>\``).join(', ')
); );
} }
@ -67,11 +67,15 @@ export function BindDirective(node, context) {
} }
} else { } else {
if (node.name === 'checked' && type?.value[0].data !== 'checkbox') { if (node.name === 'checked' && type?.value[0].data !== 'checkbox') {
e.bind_invalid_target(node, node.name, '<input type="checkbox">'); e.bind_invalid_target(
node,
node.name,
`\`<input type="checkbox">\`${type?.value[0].data === 'radio' ? ` — for \`<input type="radio">\`, use \`bind:group\`` : ''}`
);
} }
if (node.name === 'files' && type?.value[0].data !== 'file') { if (node.name === 'files' && type?.value[0].data !== 'file') {
e.bind_invalid_target(node, node.name, '<input type="file">'); e.bind_invalid_target(node, node.name, '`<input type="file">`');
} }
} }
} }
@ -94,7 +98,7 @@ export function BindDirective(node, context) {
e.bind_invalid_target( e.bind_invalid_target(
node, node,
node.name, node.name,
`non-<svg> elements. Use 'clientWidth' for <svg> instead` `non-\`<svg>\` elements. Use \`bind:clientWidth\` for \`<svg>\` instead`
); );
} }

@ -3,6 +3,6 @@ import { test } from '../../test';
export default test({ export default test({
error: { error: {
code: 'bind_invalid_target', code: 'bind_invalid_target',
message: '`bind:value` can only be used with <input>, <textarea>, <select>' message: '`bind:value` can only be used with `<input>`, `<textarea>`, `<select>`'
} }
}); });

@ -1,7 +1,7 @@
[ [
{ {
"code": "bind_invalid_target", "code": "bind_invalid_target",
"message": "`bind:offsetWidth` can only be used with non-<svg> elements. Use 'clientWidth' for <svg> instead", "message": "`bind:offsetWidth` can only be used with non-`<svg>` elements. Use `bind:clientWidth` for `<svg>` instead",
"start": { "start": {
"line": 5, "line": 5,
"column": 5 "column": 5

@ -1,7 +1,7 @@
[ [
{ {
"code": "bind_invalid_target", "code": "bind_invalid_target",
"message": "`bind:checked` can only be used with <input type=\"checkbox\">", "message": "`bind:checked` can only be used with `<input type=\"checkbox\">`",
"start": { "start": {
"line": 5, "line": 5,
"column": 7 "column": 7

@ -1,7 +1,7 @@
[ [
{ {
"code": "bind_invalid_target", "code": "bind_invalid_target",
"message": "`bind:open` can only be used with <details>", "message": "`bind:open` can only be used with `<details>`",
"start": { "start": {
"line": 5, "line": 5,
"column": 5 "column": 5

@ -1,7 +1,7 @@
[ [
{ {
"code": "bind_invalid_target", "code": "bind_invalid_target",
"message": "`bind:value` can only be used with <input>, <textarea>, <select>", "message": "`bind:value` can only be used with `<input>`, `<textarea>`, `<select>`",
"start": { "start": {
"line": 5, "line": 5,
"column": 5 "column": 5

@ -0,0 +1,14 @@
[
{
"code": "bind_invalid_target",
"message": "`bind:checked` can only be used with `<input type=\"checkbox\">` — for `<input type=\"radio\">`, use `bind:group`",
"start": {
"line": 5,
"column": 20
},
"end": {
"line": 5,
"column": 38
}
}
]

@ -0,0 +1,5 @@
<script>
let foo;
</script>
<input type="radio" bind:checked={foo}>
Loading…
Cancel
Save