2017-10-13 14:43:58 +02:00
|
|
|
import WrappedElement from './wrapped-element';
|
2017-12-18 13:06:38 +01:00
|
|
|
import templates from './../templates';
|
2017-10-13 14:43:58 +02:00
|
|
|
|
|
|
|
export default class WrappedSelect extends WrappedElement {
|
2018-05-21 18:01:03 +02:00
|
|
|
constructor({ element, classNames }) {
|
|
|
|
super({ element, classNames });
|
2017-10-13 14:43:58 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 13:54:45 +02:00
|
|
|
get placeholderOption() {
|
2017-10-18 09:43:56 +02:00
|
|
|
return this.element.querySelector('option[placeholder]');
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:54:45 +02:00
|
|
|
get optionGroups() {
|
|
|
|
return Array.from(this.element.getElementsByTagName('OPTGROUP'));
|
2017-10-18 09:43:56 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 13:54:45 +02:00
|
|
|
get options() {
|
|
|
|
return Array.from(this.element.options);
|
2017-10-18 09:43:56 +02:00
|
|
|
}
|
2017-12-18 13:06:38 +01:00
|
|
|
|
2018-04-24 13:54:45 +02:00
|
|
|
set options(options) {
|
2017-12-18 13:06:38 +01:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
const addOptionToFragment = (data) => {
|
|
|
|
// Create a standard select option
|
|
|
|
const template = templates.option(data);
|
|
|
|
// Append it to fragment
|
|
|
|
fragment.appendChild(template);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add each list item to list
|
|
|
|
options.forEach(optionData => addOptionToFragment(optionData));
|
|
|
|
|
|
|
|
this.appendDocFragment(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
appendDocFragment(fragment) {
|
|
|
|
this.element.innerHTML = '';
|
|
|
|
this.element.appendChild(fragment);
|
|
|
|
}
|
2017-10-13 14:43:58 +02:00
|
|
|
}
|