ui/src/form/switch.js

230 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-03-22 15:38:31 +08:00
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2023/03/21 16:14:10
*/
2023-05-25 17:00:17 +08:00
import { nextTick, css, html, Component, classMap } from 'wkit'
2023-03-22 15:38:31 +08:00
class Switch extends Component {
static props = {
value: {
2023-03-30 13:59:44 +08:00
type: Boolean,
default: false
2023-03-22 15:38:31 +08:00
},
2023-05-24 11:54:26 +08:00
inactiveText: '',
activeText: '',
inlineText: false,
2023-03-22 15:38:31 +08:00
disabled: false,
readonly: false
}
static styles = [
css`
:host {
display: inline-flex;
align-items: center;
font-size: 14px;
cursor: pointer;
label {
display: flex;
justify-content: center;
align-items: center;
min-width: 32px;
padding-right: 16px;
line-height: 1;
-moz-user-select: none;
user-select: none;
white-space: nowrap;
cursor: inherit;
outline: none;
color: var(--color-dark-1);
}
.dot {
display: flex;
align-items: center;
2023-03-30 13:59:44 +08:00
justify-content: space-between;
min-width: 36px;
2023-03-29 19:07:29 +08:00
height: 18px;
2023-03-30 13:59:44 +08:00
padding: 0 4px;
2023-03-29 19:07:29 +08:00
margin-right: 5px;
2023-03-30 13:59:44 +08:00
line-height: 14px;
2023-03-29 19:07:29 +08:00
border-radius: 16px;
2023-03-30 13:59:44 +08:00
background: var(--color-plain-3);
transition: box-shadow 0.2s ease, background 0.2s ease;
&::before {
display: block;
width: 14px;
height: 14px;
border-radius: 50%;
background: #fff;
content: '';
}
&::after {
display: flex;
padding: 0 2px;
font-size: 12px;
content: attr(st);
color: #fff;
}
&.open {
flex-direction: row-reverse;
background: var(--color-teal-1);
}
2023-03-22 15:38:31 +08:00
}
}
`,
css`
:host(:focus-within) .dot {
box-shadow: 0 0 0 2px var(--color-plain-a);
}
`,
// 尺寸
css`
@use 'sass:map';
2023-03-30 13:59:44 +08:00
@use 'sass:math';
2023-03-22 15:38:31 +08:00
$sizes: (
m: (
w: 72px,
h: 24px,
f: 12px
),
2023-08-15 18:53:52 +08:00
// l: (
// w: 108px,
// h: 32px,
// f: 14px
// ),
xl:
(
w: 132px,
h: 36px,
f: 14px
),
2023-03-22 15:38:31 +08:00
xxl: (
w: 160px,
h: 44px,
f: 14px
)
);
2023-03-30 13:59:44 +08:00
@function double($n) {
$m: math.round($n);
@if $m % 2 == 0px {
@return $m;
}
@return $m + 1;
}
2023-03-22 15:38:31 +08:00
@loop $s, $v in $sizes {
:host([size='#{$s}']) {
height: map.get($v, 'h');
font-size: map.get($v, 'f');
.dot {
2023-03-30 13:59:44 +08:00
min-width: #{map.get($v, 'f') * 2.5};
height: #{double(map.get($v, 'f') * 1.25)};
line-height: #{map.get($v, 'f')};
&::before {
width: #{map.get($v, 'f')};
height: #{map.get($v, 'f')};
}
2023-03-22 15:38:31 +08:00
}
}
}
`,
// 配色
css`
$colors: (
primary: 'teal',
info: 'blue',
success: 'green',
warning: 'orange',
2023-08-15 18:53:52 +08:00
danger: 'red'
2023-03-22 15:38:31 +08:00
);
@loop $t, $c in $colors {
:host([type='#{$t}']) {
2023-03-30 13:59:44 +08:00
.dot.open {
2023-03-29 19:07:29 +08:00
background: var(--color-#{$c}-1);
2023-03-22 15:38:31 +08:00
}
&:host(:focus-within) .dot {
box-shadow: 0 0 0 2px var(--color-#{$c}-a);
}
}
}
`,
// 状态
css`
:host([readonly]),
:host([disabled]) {
cursor: not-allowed;
opacity: 0.6;
}
:host([readonly]) {
cursor: default;
}
`
]
toggleCheck(ev) {
if (this.disabled || this.readOnly) {
return
}
ev.stopPropagation()
2023-03-30 13:59:44 +08:00
this.value = !this.value
2023-03-22 15:38:31 +08:00
let data = {
2023-03-30 13:59:44 +08:00
value: this.value
2023-03-22 15:38:31 +08:00
}
this.$emit('input')
2023-03-30 13:59:44 +08:00
this.$emit('change', data)
2023-03-22 15:38:31 +08:00
}
handleClick(ev) {
if (ev.type === 'click' || ev.keyCode === 32) {
ev.preventDefault()
2023-03-22 15:38:31 +08:00
this.toggleCheck(ev)
}
}
2023-03-30 13:59:44 +08:00
mounted() {}
2023-03-22 15:38:31 +08:00
render() {
2023-03-30 13:59:44 +08:00
let classes = classMap({ dot: true, open: this.value })
2023-03-22 15:38:31 +08:00
return html` <label
tabindex=${this.disabled || this.readOnly ? 'none' : 0}
2023-03-22 15:38:31 +08:00
@click=${this.handleClick}
@keydown=${this.handleClick}
>
2023-03-30 13:59:44 +08:00
<span
class=${classes}
2023-05-24 11:54:26 +08:00
st=${this.inlineText
2023-03-30 13:59:44 +08:00
? this.value
2023-05-24 11:54:26 +08:00
? this.activeText
: this.inactiveText
2023-03-30 13:59:44 +08:00
: ''}
></span>
<slot
2023-05-24 11:54:26 +08:00
>${!this.inlineText
2023-03-30 13:59:44 +08:00
? this.value
2023-05-24 11:54:26 +08:00
? this.activeText
: this.inactiveText
2023-03-30 13:59:44 +08:00
: ''}</slot
>
2023-03-22 15:38:31 +08:00
</label>`
}
}
Switch.reg('switch')