-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bind null option and input values consistently (#8328)
Null and undefined `value` bindings should always be set to an empty string. This allows native browser validation of `required` fields to work as expected with placeholder options. Placeholder options bound to null are necessary in forms where the field is conditionally required, and the bound value is posted to an API endpoint which requires it to be a nullable number or object rather than a string. fixes #8312
- Loading branch information
1 parent
d587175
commit 1728a89
Showing
4 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/runtime/samples/binding-select-null-placeholder/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const items = [ { id: 'a' }, { id: 'b' } ]; | ||
|
||
export default { | ||
props: { | ||
foo: null, | ||
items | ||
}, | ||
|
||
test({ assert, component, target }) { | ||
const select = target.querySelector( 'select' ); | ||
const options = target.querySelectorAll( 'option' ); | ||
|
||
assert.equal( options[0].selected, true ); | ||
assert.equal( options[0].disabled, true ); | ||
assert.equal( options[1].selected, false ); | ||
assert.equal( options[1].disabled, false ); | ||
|
||
// placeholder option value must be blank string for native required field validation | ||
assert.equal( options[0].value, '' ); | ||
assert.equal( select.checkValidity(), false ); | ||
|
||
component.foo = items[0]; | ||
|
||
assert.equal( options[0].selected, false ); | ||
assert.equal( options[1].selected, true ); | ||
assert.equal( select.checkValidity(), true ); | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
test/runtime/samples/binding-select-null-placeholder/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
export let foo; | ||
export let items; | ||
</script> | ||
|
||
<select bind:value={foo} required> | ||
<option value={null} disabled>Select an option</option> | ||
{#each items as item} | ||
<option value={item}>{item.id}</option> | ||
{/each} | ||
</select> |