/** * * @authors yutent (yutent.io@gmail.com) * @date 2020-12-08 11:30:52 * @version v1.0.0 * */ import $ from "../utils.js" export default class RadioItem extends HTMLElement { static get observedAttributes() { return ["value","checked","readonly","disabled"] } props = { value: '', checked: false, readonly: false, disabled: false } constructor() { super(); Object.defineProperty(this, 'root', { value: this.attachShadow({ mode: 'open' }), writable: true, enumerable: false, configurable: true }) this.root.innerHTML = ` ` this.__SWITCH__ = this.root.lastElementChild } get value() { return this.props.value } set value(val) { this.props.value = val } get checked() { return this.props.checked } set checked(val) { this.props.checked = !!val this.__SWITCH__.classList.toggle('checked', this.props.checked) } get readOnly() { return this.props.readonly } set readOnly(val) { var type = typeof val if (val === this.props.readonly) { return } if ((type === 'boolean' && val) || type !== 'boolean') { this.props.readonly = true this.setAttribute('readonly', '') } else { this.props.readonly = false this.removeAttribute('readonly') } } get disabled() { return this.props.disabled } set disabled(val) { var type = typeof val if (val === this.props.disabled) { return } if ((type === 'boolean' && val) || type !== 'boolean') { this.props.disabled = true this.setAttribute('disabled', '') } else { this.props.disabled = false this.removeAttribute('disabled') } } connectedCallback() { if (this.value === this.parentNode.value) { this.checked = true } this._handleClick = $.catch(this, 'click', ev => { if (this.disabled || this.readOnly || this.checked) { return } this.parentNode.dispatchEvent( new CustomEvent('child-picked', { detail: this.value }) ) }) } disconnectedCallback() { $.unbind(this, 'click', this._handleClick) } attributeChangedCallback(name, old, val) { if (val === null || old === val) {return} switch (name) { case 'value': this.value = val break case 'checked': case 'readonly': case 'disabled': var k = name if (k === 'readonly') { k = 'readOnly' } this[k] = true break } } } if(!customElements.get('wc-radio-item')){ customElements.define('wc-radio-item', RadioItem) }