Last active
November 15, 2016 16:36
-
-
Save swalters/8d8772d20ee375df61277d5b048e6eb2 to your computer and use it in GitHub Desktop.
Aurelia auto-focus attribute that I use with Aurelia Materialize bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {inject, customAttribute, TaskQueue} from 'aurelia-framework' | |
@customAttribute('auto-focus') | |
@inject(Element,TaskQueue) | |
export class AutoFocus { | |
constructor(private element:Element, private taskQueue:TaskQueue) { | |
} | |
attached(){ | |
this.taskQueue.queueTask(() => { | |
var inputs = this.element.getElementsByTagName('input'); | |
if(inputs.length > 0){ | |
let input:HTMLInputElement = inputs[0] as HTMLInputElement; | |
input.focus(); | |
let label = input.nextElementSibling as HTMLLabelElement; | |
if (label.nodeName === "LABEL") { | |
this.taskQueue.queueTask(() => { label.classList.add("active"); }); | |
} | |
} | |
else { | |
console.warn('No input element found for auto-focus'); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When a focused input has a label it does not move on text entry.
I had to add another task to set the class on it