UNPKG

@tarojs/components

Version:
60 lines (56 loc) 1.78 kB
import { r as registerInstance, c as createEvent, h, g as getElement } from './index-980f930f.js'; const indexCss = "taro-switch-core{display:inline-block;width:52px;height:32px}taro-switch-core .weui-switch{display:block;width:100%;height:100%}"; const Switch = class { constructor(hostRef) { registerInstance(this, hostRef); this.onChange = createEvent(this, "change", 7); this.switchChange = e => { e.stopPropagation(); const value = e.target.checked; this.isChecked = value; this.onChange.emit({ value }); }; this.type = 'switch'; this.checked = false; this.color = '#04BE02'; this.name = undefined; this.disabled = false; this.nativeProps = {}; this.isChecked = undefined; this.isWillLoadCalled = false; } function(newVal, oldVal) { if (!this.isWillLoadCalled) return; if (newVal !== oldVal) this.isChecked = newVal; } componentWillLoad() { this.isWillLoadCalled = true; this.isChecked = this.checked; } componentDidLoad() { Object.defineProperty(this.el, 'value', { get: () => this.isChecked, configurable: true }); } render() { const { type, color, isChecked, name, disabled, nativeProps } = this; const style = isChecked ? { borderColor: color || '04BE02', backgroundColor: color || '04BE02' } : {}; return (h("input", Object.assign({ type: 'checkbox', class: `weui-${type}`, style: style, checked: isChecked, name: name, disabled: disabled, onChange: this.switchChange }, nativeProps))); } get el() { return getElement(this); } static get watchers() { return { "checked": ["function"] }; } }; Switch.style = indexCss; export { Switch as taro_switch_core };