From a57e747cbb195636e6a65bae77d7d1e37f4ceb04 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:47:01 +0100 Subject: [PATCH 001/773] feat: default values for form elements (#14289) * tests * typings * implement for defaultValue/defaultChecked on inputs * docs (draft) * selected * fix test * remove * tweak * changeset * untrack reads, they could be inside an effect * Apply suggestions from code review Co-authored-by: Rich Harris * handle select reset case * handle reset case specifically: use different props/queries in that case * enhance test * fix --------- Co-authored-by: Rich Harris --- .changeset/tidy-zebras-begin.md | 5 + .../docs/03-template-syntax/11-bind.md | 43 +++- packages/svelte/elements.d.ts | 8 + .../client/visitors/RegularElement.js | 38 ++-- .../server/visitors/shared/element.js | 3 +- .../client/dom/elements/attributes.js | 19 ++ .../client/dom/elements/bindings/input.js | 43 ++-- .../client/dom/elements/bindings/select.js | 10 +- .../client/dom/elements/bindings/shared.js | 8 +- packages/svelte/src/internal/client/index.js | 3 +- packages/svelte/src/utils.js | 6 +- .../samples/form-default-value/_config.js | 209 ++++++++++++++++++ .../samples/form-default-value/main.svelte | 156 +++++++++++++ 13 files changed, 509 insertions(+), 42 deletions(-) create mode 100644 .changeset/tidy-zebras-begin.md create mode 100644 packages/svelte/tests/runtime-runes/samples/form-default-value/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/form-default-value/main.svelte diff --git a/.changeset/tidy-zebras-begin.md b/.changeset/tidy-zebras-begin.md new file mode 100644 index 000000000000..cefbf2acfde1 --- /dev/null +++ b/.changeset/tidy-zebras-begin.md @@ -0,0 +1,5 @@ +--- +'svelte': minor +--- + +feat: support `defaultValue/defaultChecked` for inputs diff --git a/documentation/docs/03-template-syntax/11-bind.md b/documentation/docs/03-template-syntax/11-bind.md index 7dd03a6b04d7..975135f82441 100644 --- a/documentation/docs/03-template-syntax/11-bind.md +++ b/documentation/docs/03-template-syntax/11-bind.md @@ -53,6 +53,22 @@ In the case of a numeric input (`type="number"` or `type="range"`), the value wi If the input is empty or invalid (in the case of `type="number"`), the value is `undefined`. +If an `` has a `defaultValue` and is part of a form, it will revert to that value instead of the empty string when the form is reset. Note that for the initial render the value of the binding takes precedence unless it is `null` or `undefined`. + +```svelte + + +
+ + +
+``` + +> [!NOTE] +> Use reset buttons sparingly, and ensure that users won't accidentally click them while trying to submit the form. + ## `` Checkbox and radio inputs can be bound with `bind:checked`: @@ -64,16 +80,29 @@ Checkbox and radio inputs can be bound with `bind:checked`: ``` +If an `` has a `defaultChecked` attribute and is part of a form, it will revert to that value instead of `false` when the form is reset. Note that for the initial render the value of the binding takes precedence unless it is `null` or `undefined`. + +```svelte + + +
+ + +
+``` + ## `` Inputs that work together can use `bind:group`. ```svelte @@ -146,6 +175,16 @@ When the value of an ` + + + +``` + ## `