mirror of https://github.com/sveltejs/svelte
feat: take form resets into account for two way bindings (#10617)
* feat: take form resets into account for two way bindings When resetting a form, the value of the inputs within it get out of sync with the bound value of those inputs. This PR introduces a reset listener on the parent form to reset the value in that case closes #2659 * slightly different approach * tweaks, test * this is a breaking change, strictly speaking * bind:files * use capture phase * tweak wording * use promise, explainpull/10879/head
parent
416bc85d9c
commit
3eef1cb8cf
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
feat: take form resets into account for two way bindings
|
@ -0,0 +1,19 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target }) {
|
||||
const p = target.querySelector('p');
|
||||
|
||||
assert.htmlEqual(
|
||||
p?.innerHTML || '',
|
||||
`{"text":"text","checkbox":true,"radio_group":"a","checkbox_group":["a"],"select":"b","textarea":"textarea"}`
|
||||
);
|
||||
|
||||
await target.querySelector('button')?.click();
|
||||
await Promise.resolve();
|
||||
assert.htmlEqual(
|
||||
p?.innerHTML || '',
|
||||
`{"text":"","checkbox":false,"radio_group":null,"checkbox_group":[],"select":"a","textarea":""}`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,31 @@
|
||||
<script>
|
||||
let text = $state('text');
|
||||
let checkbox = $state(true);
|
||||
let radio_group = $state('a');
|
||||
let checkbox_group = $state(['a']);
|
||||
let select = $state('b');
|
||||
let textarea = $state('textarea');
|
||||
</script>
|
||||
|
||||
<p>{JSON.stringify({ text, checkbox, radio_group, checkbox_group, select, textarea })}</p>
|
||||
|
||||
<form>
|
||||
<input bind:value={text} />
|
||||
|
||||
<input type="checkbox" bind:checked={checkbox} />
|
||||
|
||||
<input type="radio" name="radio" value="a" bind:group={radio_group} />
|
||||
<input type="radio" name="radio" value="b" bind:group={radio_group} />
|
||||
|
||||
<input type="checkbox" name="checkbox" value="a" bind:group={checkbox_group} />
|
||||
<input type="checkbox" name="checkbox" value="b" bind:group={checkbox_group} />
|
||||
|
||||
<select bind:value={select}>
|
||||
<option value="a">a</option>
|
||||
<option value="b">b</option>
|
||||
</select>
|
||||
|
||||
<textarea bind:value={textarea}></textarea>
|
||||
|
||||
<button type="button" onclick={(e) => e.target.form.reset()}>Reset</button>
|
||||
</form>
|
Loading…
Reference in new issue