UNPKG

@tarojs/components

Version:
134 lines (130 loc) 4.79 kB
import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-5e431bb8.js'; const Radio = class { constructor(hostRef) { registerInstance(this, hostRef); this.onChange = createEvent(this, "radiochange", 7); this.handleClick = (e) => { e.stopPropagation(); if (this.disabled) return; if (!this.checked) this.checked = true; }; this.name = undefined; this.value = ''; this.id = undefined; this.checked = false; this.disabled = false; this.nativeProps = {}; this.isWillLoadCalled = false; } watchChecked(newVal) { if (!this.isWillLoadCalled) return; newVal && this.onChange.emit({ value: this.value }); } watchId(newVal) { if (!this.isWillLoadCalled) return; if (newVal) this.inputEl.setAttribute('id', newVal); } componentDidRender() { this.id && this.el.removeAttribute('id'); } componentWillLoad() { this.isWillLoadCalled = true; } render() { const { checked, name, value, disabled, nativeProps } = this; return (h(Host, { class: 'weui-cells_checkbox', onClick: this.handleClick }, h("input", Object.assign({ ref: dom => { if (!dom) return; this.inputEl = dom; if (this.id) dom.setAttribute('id', this.id); }, type: 'radio', name: name, value: value, class: 'weui-check', checked: checked, disabled: disabled, onChange: e => e.stopPropagation() }, nativeProps)), h("i", { class: 'weui-icon-checked' }), h("slot", null))); } get el() { return getElement(this); } static get watchers() { return { "checked": ["watchChecked"], "id": ["watchId"] }; } }; var __classPrivateFieldSet = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _RadioGroup_value; const RadioGroup = class { constructor(hostRef) { registerInstance(this, hostRef); this.onChange = createEvent(this, "change", 7); this.uniqueName = Date.now().toString(36); _RadioGroup_value.set(this, void 0); this.name = undefined; } function(e) { e.stopPropagation(); if (e.target.tagName !== 'TARO-RADIO-CORE') return; const target = e.target; if (target.checked) { const childList = this.el.querySelectorAll('taro-radio-core'); childList.forEach(element => { if (element !== target) { element.checked = false; } }); __classPrivateFieldSet(this, _RadioGroup_value, e.detail.value, "f"); this.onChange.emit({ value: __classPrivateFieldGet(this, _RadioGroup_value, "f") }); } } componentDidLoad() { const childList = this.el.querySelectorAll('taro-radio-core'); childList.forEach((element) => { element.setAttribute('name', this.name || this.uniqueName); }); Object.defineProperty(this.el, 'value', { get: () => { if (!__classPrivateFieldGet(this, _RadioGroup_value, "f")) { const childList = this.el.querySelectorAll('taro-radio-core'); __classPrivateFieldSet(this, _RadioGroup_value, this.getValues(childList), "f"); } return __classPrivateFieldGet(this, _RadioGroup_value, "f"); }, configurable: true }); } getValues(childList) { let val = ''; Array.from(childList) .forEach(element => { const checkbox = element.querySelector('input'); if (checkbox === null || checkbox === void 0 ? void 0 : checkbox.checked) { val = checkbox.value || ''; } }); return val; } render() { return (h(Host, { class: 'weui-cells_radiogroup' })); } get el() { return getElement(this); } }; _RadioGroup_value = new WeakMap(); export { Radio as taro_radio_core, RadioGroup as taro_radio_group_core };