views/mdc/assets/js/components/selects.js in voom-presenters-0.1.13 vs views/mdc/assets/js/components/selects.js in voom-presenters-0.2.0

- old
+ new

@@ -1,45 +1,46 @@ import {MDCSelect} from '@material/select'; -import {VBaseComponent} from './base-component'; +import {VBaseComponent, hookupComponents} from './base-component'; import {eventHandlerMixin} from './mixins/event-handler'; export function initSelects() { console.log('\tSelects'); - var components = document.querySelectorAll('.mdc-select'); - for (var i = 0; i < components.length; i++) { - var component = components[i]; - if (!component.vComponent) { - let vSelect = new VSelect(component, MDCSelect.attachTo(component)); - component.vComponent = vSelect; - var selectInput = component.querySelector('select'); - selectInput.vComponent = vSelect; - } - } + hookupComponents('.v-select', VSelect, MDCSelect); } export class VSelect extends eventHandlerMixin(VBaseComponent) { constructor(element, mdcComponent) { - super(element); + super(element, mdcComponent); this.select = element.querySelector('select'); - this.mdcComponent = mdcComponent; + this.select.vComponent = this; } - prepareSubmit(form, params) { - // On actual post/submit the form is passed and we are not expected to return our value - if (!form) { - params.push([this.select.name, this.select.value]); - } + prepareSubmit(params) { + params.push([this.name(), this.value()]); } - validate() { - return true; + name() { + return this.select.name; } - name(){ - + value() { + return this.select.options.length === 0 ? null : this.select.options[this.select.selectedIndex].value; } - value(){ + clear() { + let before = this.select.selectedIndex; + this.select.selectedIndex = 0; + if (before !== 0) { + var event = new InputEvent('input', { + view: window, + bubbles: true, + cancelable: true + }); + this.select .dispatchEvent(event); + } + } + setValue(value) { + this.select.value = value; } } \ No newline at end of file