This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0
wcui/src/form/checkbox.wc

240 lines
3.9 KiB
Plaintext
Raw Normal View History

2019-07-25 21:29:03 +08:00
<template>
<label>
<wc-icon class="dot" is="checkbox-off"></wc-icon>
<slot></slot>
</label>
</template>
<style lang="scss">
:host {
display: inline-block;
font-size: 14px;
label {
display: flex;
justify-content: center;
align-items: center;
min-width: 32px;
height: 32px;
padding: 0 5px;
user-select: none;
-moz-user-select: none;
cursor: inherit;
color: nth($cgr, 3);
}
.dot {
--size: 18px;
padding: 2px;
margin-right: 3px;
}
}
:host([disabled]) {
cursor: not-allowed;
opacity: 0.6;
}
:host([size='large']) {
label {
width: 58px;
height: 32px;
}
.dot {
--size: 26px;
}
}
:host([size='medium']) {
label {
width: 50px;
height: 28px;
}
.dot {
--size: 22px;
}
}
:host([size='mini']) {
label {
width: 22px;
height: 14px;
padding: 2px;
}
.dot {
--size: 10px;
}
}
:host([color='red']) label.checked {
color: nth($cr, 1);
.dot {
border-color: nth($cr, 1);
}
.dot::after {
background: nth($cr, 1);
}
}
:host([color='blue']) label.checked {
color: nth($cb, 1);
.dot {
border-color: nth($cb, 1);
}
.dot::after {
background: nth($cb, 1);
}
}
:host([color='green']) label.checked {
color: nth($cg, 1);
.dot {
border-color: nth($cg, 1);
}
.dot::after {
background: nth($cg, 1);
}
}
:host([color='teal']) label.checked {
color: nth($ct, 1);
.dot {
border-color: nth($ct, 1);
}
.dot::after {
background: nth($ct, 1);
}
}
:host([color='orange']) label.checked {
color: nth($co, 1);
.dot {
border-color: nth($co, 1);
}
.dot::after {
background: nth($co, 1);
}
}
:host([color='dark']) label.checked {
color: nth($cd, 1);
.dot {
border-color: nth($cd, 1);
}
.dot::after {
background: nth($cd, 1);
}
}
:host([color='purple']) label.checked {
color: nth($cpp, 1);
.dot {
border-color: nth($cpp, 1);
}
.dot::after {
background: nth($cpp, 1);
}
}
</style>
<script>
import '../icon/index'
export default class Checkbox {
props = {
label: '',
color: '',
value: [],
checked: false,
disabled: false
}
constructor() {
/* render */
this.__SWITCH__ = this.root.lastElementChild
this.__ICO__ = this.__SWITCH__.children[0]
}
get value() {
return this.props.value
}
set value(val) {
this.props.value = val
this.checked = this.props.value.includes(this.props.label)
}
get checked() {
return this.props.checked
}
set checked(val) {
this.props.checked = !!val
var { value, checked, label, color } = this.props
this.__SWITCH__.classList.toggle('checked', checked)
this.__ICO__.setAttribute('is', 'checkbox-' + (checked ? 'on' : 'off'))
var idx = value.indexOf(label)
if (checked) {
this.__ICO__.setAttribute('color', color)
if (idx < 0) {
value.push(label)
}
} else {
this.__ICO__.removeAttribute('color')
if (~idx) {
value.splice(idx, 1)
}
}
}
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')
}
}
mounted() {
this.addEventListener(
'click',
ev => {
if (!this.disabled) {
this.checked = !this.checked
this.dispatchEvent(new CustomEvent('input'))
}
},
false
)
}
watch(name, old, val) {
if (old === val) {
return
}
switch (name) {
case 'label':
case 'color':
this.props[name] = val
break
case 'checked':
case 'disabled':
if (val === '') {
this[name] = true
}
break
}
}
}
</script>