Created
January 12, 2016 09:50
-
-
Save Manuel-S/289a5c5cb2fdfbfbabce to your computer and use it in GitHub Desktop.
Aurelia custom element for auto completion using awesomeplete
This file contains hidden or 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 {bindable, inject, inlineView} from 'aurelia-framework'; | |
| import * as Awe from 'awesomplete'; //eslint-disable-line | |
| /* USAGE: | |
| <auto-complete | |
| data.two-way="values" | |
| selected.bind="selectedValue" | |
| placeholder="Select a value" | |
| label="Autocomplete"> | |
| </auto-complete> | |
| */ | |
| export class AwesompleteFactory { | |
| create(input) { | |
| const awe = new Awesomplete(input); | |
| // monkey patch Awesomplete to add toggle function | |
| awe.toggle = function () { | |
| if (this.ul.childNodes.length === 0) { | |
| this.minChars = 0; | |
| this.evaluate(); | |
| } else if (this.ul.hasAttribute('hidden')) { | |
| this.evaluate(); | |
| this.open(); | |
| } else { | |
| this.evaluate(); | |
| this.close(); | |
| } | |
| }; | |
| return awe; | |
| } | |
| } | |
| @inject(AwesompleteFactory) | |
| @inlineView(`<template> | |
| <label for.bind="inputId">${label}</label> | |
| <input name.bind="inputId" | |
| id.bind="inputId" | |
| placeholder.bind="placeholder" | |
| selected.two-way="selectedFilter"> | |
| </template>`) | |
| export class AutoComplete { | |
| const SELECT_COMPLETE = 'awesomplete-selectcomplete'; | |
| @bindable data; | |
| input; | |
| @bindable inputId; | |
| @bindable label; | |
| @bindable placeholder; | |
| awe; | |
| constructor(awesompleteFactory) { | |
| this.awesompleteFactory = awesompleteFactory; | |
| } | |
| listener(e) { | |
| this.selected = e.target.value; | |
| e.target.value = null; | |
| } | |
| attached() { | |
| this.input = document.getElementById(this.inputId); | |
| this.awe = this.awesompleteFactory.create(this.input); | |
| this.awe.list = this.data; | |
| this.input.addEventListener(this.SELECT_COMPLETE, this.listener); | |
| } | |
| detached() { | |
| this.input.removeEventListener(this.SELECT_COMPLETE, this.listener); | |
| } | |
| toggle() { | |
| this.input.value = ''; | |
| this.awe.toggle(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi I'm using type script so I'm getting some errors. I want to fix those can you help me sort out these
Awesomplete(input) : from where this constructor comming from.