mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
fix: don't set selected option(s) if value is unbound or not passed (#8329)
fix: #5644 --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
This commit is contained in:
parent
b56dfe51a8
commit
60a205edb8
@ -858,8 +858,9 @@ export default class ElementWrapper extends Wrapper {
|
||||
}
|
||||
|
||||
block.chunks.mount.push(b`
|
||||
(${data}.multiple ? @select_options : @select_option)(${this.var}, ${data}.value);
|
||||
'value' in ${data} && (${data}.multiple ? @select_options : @select_option)(${this.var}, ${data}.value);
|
||||
`);
|
||||
|
||||
block.chunks.update.push(b`
|
||||
if (${block.renderer.dirty(Array.from(dependencies))} && 'value' in ${data}) (${data}.multiple ? @select_options : @select_option)(${this.var}, ${data}.value);
|
||||
`);
|
||||
|
@ -0,0 +1,12 @@
|
||||
export default {
|
||||
test({ assert, component, target }) {
|
||||
const options = target.querySelectorAll('option');
|
||||
|
||||
assert.equal(options[0].selected, true);
|
||||
assert.equal(options[1].selected, false);
|
||||
|
||||
component.value = ['2'];
|
||||
assert.equal(options[0].selected, false);
|
||||
assert.equal(options[1].selected, true);
|
||||
}
|
||||
};
|
@ -0,0 +1,8 @@
|
||||
<script>
|
||||
import Select from './select.svelte';
|
||||
|
||||
export let value = ['1'];
|
||||
export let other = {};
|
||||
</script>
|
||||
|
||||
<Select {value} {other} />
|
@ -0,0 +1,9 @@
|
||||
<script>
|
||||
export let value;
|
||||
export let other;
|
||||
</script>
|
||||
|
||||
<select multiple bind:value {...other}>
|
||||
<option value="1">option 1</option>
|
||||
<option value="2">option 2</option>
|
||||
</select>
|
13
test/runtime/samples/select-multiple-spread/_config.js
Normal file
13
test/runtime/samples/select-multiple-spread/_config.js
Normal file
@ -0,0 +1,13 @@
|
||||
export default {
|
||||
test({ assert, component, target }) {
|
||||
const options = target.querySelectorAll('option');
|
||||
|
||||
assert.equal(options[0].selected, true);
|
||||
assert.equal(options[1].selected, false);
|
||||
|
||||
// Shouldn't change the value because the value is not bound.
|
||||
component.value = ['2'];
|
||||
assert.equal(options[0].selected, true);
|
||||
assert.equal(options[1].selected, false);
|
||||
}
|
||||
};
|
7
test/runtime/samples/select-multiple-spread/main.svelte
Normal file
7
test/runtime/samples/select-multiple-spread/main.svelte
Normal file
@ -0,0 +1,7 @@
|
||||
<script>
|
||||
import Select from './select.svelte';
|
||||
|
||||
export let attrs = { value: ['1'] };
|
||||
</script>
|
||||
|
||||
<Select {attrs} />
|
@ -0,0 +1,8 @@
|
||||
<script>
|
||||
export let attrs;
|
||||
</script>
|
||||
|
||||
<select multiple {...attrs}>
|
||||
<option value="1">option 1</option>
|
||||
<option value="2">option 2</option>
|
||||
</select>
|
Loading…
Reference in New Issue
Block a user