Merge branch 'sveltejs:main' into docs-track-state-async-callbacks

pull/18573/head
Bored.gyl 2 months ago committed by GitHub
commit 4cc4611f1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: preserve select selection when spread attributes omit value

@ -56,8 +56,10 @@ export function select_option(select, value, mounting = false) {
*/ */
export function init_select(select) { export function init_select(select) {
var observer = new MutationObserver(() => { var observer = new MutationObserver(() => {
if ('__value' in select) {
// @ts-ignore // @ts-ignore
select_option(select, select.__value); select_option(select, select.__value);
}
// Deliberately don't update the potential binding value, // Deliberately don't update the potential binding value,
// the model should be preserved unless explicitly changed // the model should be preserved unless explicitly changed
}); });

@ -0,0 +1,25 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';
export default test({
mode: ['client', 'hydrate'],
async test({ assert, component, target }) {
const select = target.querySelector('select');
ok(select);
assert.equal(select.selectedIndex, 0);
component.toggle();
flushSync();
await Promise.resolve();
assert.equal(select.selectedIndex, 0);
component.toggle();
flushSync();
await Promise.resolve();
assert.equal(select.selectedIndex, 0);
}
});

@ -0,0 +1,16 @@
<script>
let show_extra = false;
const attributes = { 'aria-label': 'choice' };
export function toggle() {
show_extra = !show_extra;
}
</script>
<select {...attributes}>
<option value="">Choose an option</option>
<option value="first">First</option>
{#if show_extra}
<option value="extra">Extra</option>
{/if}
</select>
Loading…
Cancel
Save