fix(4995): dynamiclly set autofocus

pull/5070/head
Bassam Ismail 5 years ago
parent 0520a10dc0
commit be67e1226a

@ -164,7 +164,7 @@ export default class AttributeWrapper {
} }
// special case autofocus. has to be handled in a bit of a weird way // special case autofocus. has to be handled in a bit of a weird way
if (this.node.is_true && name === 'autofocus') { if (name === 'autofocus') {
block.autofocus = element.var; block.autofocus = element.var;
} }
@ -350,4 +350,4 @@ const boolean_attribute = new Set([
'required', 'required',
'reversed', 'reversed',
'selected' 'selected'
]); ]);

@ -1,8 +1,24 @@
export default { export default {
html: '', html: '',
test({ assert, component, target, window }) { async test({ assert, component, target, window }) {
component.visible = true; component.visible = true;
assert.equal(target.querySelector('input'), window.document.activeElement); const input = target.querySelector('input');
async function dispatchEvent(value) {
input.value = value;
const inputEvent = new window.InputEvent("input");
await input.dispatchEvent(inputEvent);
}
assert.equal(window.document.activeElement.getAttribute('title'), 'text');
await dispatchEvent('dynamic');
assert.equal(window.document.activeElement.getAttribute('title'), 'dynamic');
await dispatchEvent('bound');
assert.equal(window.document.activeElement.getAttribute('title'), 'bound');
await dispatchEvent('fn');
assert.equal(window.document.activeElement.getAttribute('title'), 'fn');
} }
}; };

@ -1,8 +1,16 @@
<script> <script>
export let visible = false; let autofocus = true;
let input; let active = "text";
</script> </script>
{#if visible} <input title="input" bind:value={active} />
<input bind:this={input} autofocus>
{/if} {#if active === "text"}
<input title={active} autofocus />
{:else if active === "dynamic"}
<input title={active} {autofocus} />
{:else if active === "bound"}
<input title={active} autofocus={autofocus} />
{:else if active === "fn"}
<input title={active} autofocus={() => true} />
{/if}

Loading…
Cancel
Save