完成开关的重构

master
yutent 2023-11-20 18:08:00 +08:00
parent 420bf73c03
commit 44472fcf23
1 changed files with 78 additions and 113 deletions

View File

@ -8,54 +8,54 @@ import { nextTick, css, html, Component, classMap } from 'wkit'
class Switch extends Component { class Switch extends Component {
static props = { static props = {
value: { value: false,
type: Boolean,
default: false
},
inactiveText: '', inactiveText: '',
activeText: '', activeText: '',
inlineText: false, inlineText: false,
disabled: false, disabled: false,
readonly: false readOnly: false
} }
static styles = [ static styles = [
css` css`
:host { :host {
display: inline-flex; display: inline-flex;
align-items: center;
font-size: 14px;
cursor: pointer; cursor: pointer;
label { label {
display: flex; display: flex;
justify-content: center;
align-items: center; align-items: center;
min-width: 32px; height: 32px;
padding: 0 8px 0 2px; padding-right: 8px;
line-height: 1; line-height: 1;
-moz-user-select: none; font-size: 14px;
user-select: none;
white-space: nowrap; white-space: nowrap;
color: var(--color-dark-1);
cursor: inherit; cursor: inherit;
outline: none; outline: none;
color: var(--color-dark-1); -webkit-user-select: none;
user-select: none;
&.open .switch {
flex-direction: row-reverse;
background: var(--color-teal-1);
}
} }
.dot { .switch {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; min-width: 32px;
min-width: 36px;
height: 18px; height: 18px;
padding: 0 4px; padding: 0 2px;
margin-right: 5px; margin-right: 5px;
line-height: 14px;
border-radius: 16px; border-radius: 16px;
font-family: Tahoma, Verdana, Helvetica, Arial, sans-serif;
font-size: 12px;
background: var(--color-plain-3); background: var(--color-plain-3);
transition: box-shadow 0.2s ease, background 0.2s ease; transition: box-shadow 0.2s ease, background 0.2s ease;
}
&::before { .dot {
display: block; display: block;
width: 14px; width: 14px;
height: 14px; height: 14px;
@ -63,79 +63,55 @@ class Switch extends Component {
background: #fff; background: #fff;
content: ''; content: '';
} }
.text {
&::after { padding: 0 3px;
display: flex; background: none;
padding: 0 2px;
font-size: 12px;
content: attr(st);
color: #fff; color: #fff;
} }
}
&.open { :host(:focus-within) .switch {
flex-direction: row-reverse;
background: var(--color-teal-1);
}
}
}
`,
css`
:host(:focus-within) .dot {
box-shadow: 0 0 0 2px var(--color-plain-a); box-shadow: 0 0 0 2px var(--color-plain-a);
} }
`, `,
// 尺寸 // 尺寸
css` css`
@use 'sass:map'; @use 'sass:map';
@use 'sass:math'; @use 'sass:math';
$sizes: ( $sizes: (
s: (
hd: 14px,
h: 20px,
f: 10px
),
m: ( m: (
w: 72px, hd: 16px,
h: 24px, h: 24px,
f: 12px f: 12px
), ),
// l: ( xl: (
// w: 108px, hd: 20px,
// h: 32px,
// f: 14px
// ),
xl:
(
w: 132px,
h: 36px, h: 36px,
f: 14px f: 14px
),
xxl: (
w: 160px,
h: 44px,
f: 14px
) )
); );
@function double($n) {
$m: math.round($n);
@if $m % 2 == 0px {
@return $m;
}
@return $m + 1;
}
@loop $s, $v in $sizes { @loop $s, $v in $sizes {
:host([size='#{$s}']) { :host([size='#{$s}']) {
label {
height: map.get($v, 'h'); height: map.get($v, 'h');
font-size: map.get($v, 'f'); font-size: math.max(map.get($v, 'f'), 12px);
.dot {
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')};
} }
.switch {
min-width: map.get($v, 'hd') * 2 - 4;
height: map.get($v, 'hd');
padding: 0 #{math.div((map.get($v, 'hd') - map.get($v, 'f')), 2)};
}
.dot {
width: map.get($v, 'f');
height: map.get($v, 'f');
} }
} }
} }
@ -143,7 +119,6 @@ class Switch extends Component {
// 配色 // 配色
css` css`
$colors: ( $colors: (
primary: 'teal',
info: 'blue', info: 'blue',
success: 'green', success: 'green',
warning: 'orange', warning: 'orange',
@ -152,11 +127,11 @@ class Switch extends Component {
@loop $t, $c in $colors { @loop $t, $c in $colors {
:host([type='#{$t}']) { :host([type='#{$t}']) {
.dot.open { .open .switch {
background: var(--color-#{$c}-1); background: var(--color-#{$c}-1);
} }
&:host(:focus-within) .dot { &:host(:focus-within) .switch {
box-shadow: 0 0 0 2px var(--color-#{$c}-a); box-shadow: 0 0 0 2px var(--color-#{$c}-a);
} }
} }
@ -175,7 +150,11 @@ class Switch extends Component {
` `
] ]
toggleCheck(ev) { get #text() {
return this.value ? this.activeText : this.inactiveText
}
#toggleCheck(ev) {
if (this.disabled || this.readOnly) { if (this.disabled || this.readOnly) {
return return
} }
@ -183,45 +162,31 @@ class Switch extends Component {
ev.stopPropagation() ev.stopPropagation()
this.value = !this.value this.value = !this.value
let data = { let data = { value: this.value }
value: this.value
}
this.$emit('input') this.$emit('input', data)
this.$emit('change', data) this.$emit('change', data)
} }
handleClick(ev) { #click(ev) {
if (ev.type === 'click' || ev.keyCode === 32) { if (ev.type === 'click' || ev.keyCode === 32) {
ev.preventDefault() ev.preventDefault()
this.toggleCheck(ev) this.#toggleCheck(ev)
} }
} }
mounted() {}
render() { render() {
let classes = classMap({ dot: true, open: this.value })
return html` <label return html` <label
class=${classMap({ open: this.value })}
tabindex=${this.disabled || this.readOnly ? 'none' : 0} tabindex=${this.disabled || this.readOnly ? 'none' : 0}
@click=${this.handleClick} @click=${this.#click}
@keydown=${this.handleClick} @keydown=${this.#click}
>
<span
class=${classes}
st=${this.inlineText
? this.value
? this.activeText
: this.inactiveText
: ''}
></span>
<slot
>${!this.inlineText
? this.value
? this.activeText
: this.inactiveText
: ''}</slot
> >
<span class="switch">
<i class="dot"></i>
<mark class="text">${this.inlineText ? this.#text : ''}</mark>
</span>
<slot>${this.inlineText ? '' : this.#text}</slot>
</label>` </label>`
} }
} }