2018-03-30 16:56:30 +00:00
|
|
|
const BaseControl = require('./base');
|
|
|
|
const Awesomplete = require('awesomplete');
|
|
|
|
|
|
|
|
class AutocompleteControl extends BaseControl {
|
|
|
|
make() {
|
|
|
|
super.make();
|
|
|
|
this.input.setAttribute('type', 'text');
|
|
|
|
this.setupAwesomplete();
|
|
|
|
}
|
|
|
|
|
2018-04-02 17:07:22 +00:00
|
|
|
async setupAwesomplete() {
|
2018-03-30 16:56:30 +00:00
|
|
|
this.awesomplete = new Awesomplete(this.input, {
|
|
|
|
minChars: 0,
|
|
|
|
maxItems: 99
|
|
|
|
});
|
|
|
|
|
2018-04-02 17:07:22 +00:00
|
|
|
this.list = await this.getList();
|
|
|
|
|
2018-03-30 16:56:30 +00:00
|
|
|
// rebuild the list on input
|
2018-04-02 17:07:22 +00:00
|
|
|
this.input.addEventListener('input', (event) => {
|
|
|
|
this.awesomplete.list = this.list;
|
2018-03-30 16:56:30 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-02 17:07:22 +00:00
|
|
|
|
|
|
|
validate(value) {
|
|
|
|
if (this.list.includes(value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-30 16:56:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = AutocompleteControl;
|