Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(4995): dynamically set autofocus #5070

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/compile/render_dom/wrappers/Element/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class AttributeWrapper {
}

// 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;
}

Expand Down Expand Up @@ -350,4 +350,4 @@ const boolean_attribute = new Set([
'required',
'reversed',
'selected'
]);
]);
20 changes: 18 additions & 2 deletions test/runtime/samples/autofocus/_config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
export default {
html: '',

test({ assert, component, target, window }) {
async test({ assert, component, target, window }) {
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');
}
};
18 changes: 13 additions & 5 deletions test/runtime/samples/autofocus/main.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<script>
export let visible = false;
let input;
let autofocus = true;
let active = "text";
</script>

{#if visible}
<input bind:this={input} autofocus>
{/if}
<input title="input" bind:value={active} />

{#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}