mirror of https://github.com/sveltejs/svelte
fix: preserve `<select>` state while focused (#16958)
parent
9b5fb3f430
commit
ee093e4c86
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: preserve `<select>` state while focused
|
||||||
@ -0,0 +1,82 @@
|
|||||||
|
import { tick } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
async test({ assert, target }) {
|
||||||
|
const [shift] = target.querySelectorAll('button');
|
||||||
|
shift.click();
|
||||||
|
await tick();
|
||||||
|
|
||||||
|
const [select] = target.querySelectorAll('select');
|
||||||
|
|
||||||
|
select.focus();
|
||||||
|
select.value = 'three';
|
||||||
|
select.dispatchEvent(new InputEvent('change', { bubbles: true }));
|
||||||
|
await tick();
|
||||||
|
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>shift</button>
|
||||||
|
<select>
|
||||||
|
<option>one</option>
|
||||||
|
<option>two</option>
|
||||||
|
<option>three</option>
|
||||||
|
</select>
|
||||||
|
<p>two</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
assert.equal(select.value, 'three');
|
||||||
|
|
||||||
|
select.focus();
|
||||||
|
select.value = 'one';
|
||||||
|
select.dispatchEvent(new InputEvent('change', { bubbles: true }));
|
||||||
|
await tick();
|
||||||
|
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>shift</button>
|
||||||
|
<select>
|
||||||
|
<option>one</option>
|
||||||
|
<option>two</option>
|
||||||
|
<option>three</option>
|
||||||
|
</select>
|
||||||
|
<p>two</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
assert.equal(select.value, 'one');
|
||||||
|
|
||||||
|
shift.click();
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>shift</button>
|
||||||
|
<select>
|
||||||
|
<option>one</option>
|
||||||
|
<option>two</option>
|
||||||
|
<option>three</option>
|
||||||
|
</select>
|
||||||
|
<p>three</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
assert.equal(select.value, 'one');
|
||||||
|
|
||||||
|
shift.click();
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`
|
||||||
|
<button>shift</button>
|
||||||
|
<select>
|
||||||
|
<option>one</option>
|
||||||
|
<option>two</option>
|
||||||
|
<option>three</option>
|
||||||
|
</select>
|
||||||
|
<p>one</p>
|
||||||
|
`
|
||||||
|
);
|
||||||
|
assert.equal(select.value, 'one');
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
let selected = $state('two');
|
||||||
|
|
||||||
|
let resolvers = [];
|
||||||
|
let select;
|
||||||
|
|
||||||
|
function push(value) {
|
||||||
|
const { promise, resolve } = Promise.withResolvers();
|
||||||
|
resolvers.push(() => resolve(value));
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={() => {
|
||||||
|
select.focus();
|
||||||
|
resolvers.shift()?.();
|
||||||
|
}}>shift</button>
|
||||||
|
|
||||||
|
<svelte:boundary>
|
||||||
|
<select bind:this={select} bind:value={selected}>
|
||||||
|
<option>one</option>
|
||||||
|
<option>two</option>
|
||||||
|
<option>three</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<p>{await push(selected)}</p>
|
||||||
|
|
||||||
|
{#snippet pending()}
|
||||||
|
<p>loading...</p>
|
||||||
|
{/snippet}
|
||||||
|
</svelte:boundary>
|
||||||
Loading…
Reference in new issue