完成switch组件

master
yutent 2023-03-30 13:59:44 +08:00
parent ea92ce9e59
commit 799b0f82f3
2 changed files with 75 additions and 31 deletions

View File

@ -34,7 +34,7 @@
- [x] `wc-radio`表单组件-单选框 - [x] `wc-radio`表单组件-单选框
- [ ] `wc-select`表单组件-下拉选择 - [ ] `wc-select`表单组件-下拉选择
- [ ] `wc-cascadar`表单组件-多级联动 - [ ] `wc-cascadar`表单组件-多级联动
- [ ] `wc-switch`表单组件-开关 - [x] `wc-switch`表单组件-开关
- [x] `wc-icon`图标组件 - [x] `wc-icon`图标组件
- [ ] `wc-layer` 弹层组件 - [ ] `wc-layer` 弹层组件
- [x] `wc-markd`markdown 组件 - [x] `wc-markd`markdown 组件

View File

@ -4,15 +4,19 @@
* @date 2023/03/21 16:14:10 * @date 2023/03/21 16:14:10
*/ */
import { nextTick, css, html, Component } from '@bd/core' import { nextTick, css, html, Component, classMap } from '@bd/core'
class Switch extends Component { class Switch extends Component {
static props = { static props = {
size: 'l',
value: { value: {
type: String, type: Boolean,
default: '', default: false,
attribute: false attribute: false
}, },
'inactive-text': '',
'active-text': '',
'inline-text': false,
disabled: false, disabled: false,
readonly: false readonly: false
} }
@ -42,15 +46,38 @@ class Switch extends Component {
.dot { .dot {
display: flex; display: flex;
justify-content: center;
align-items: center; align-items: center;
width: 36px; justify-content: space-between;
min-width: 36px;
height: 18px; height: 18px;
padding: 3px; padding: 0 4px;
margin-right: 5px; margin-right: 5px;
line-height: 14px;
border-radius: 16px; border-radius: 16px;
background: var(--color-dark-1); background: var(--color-plain-3);
transition: box-shadow 0.15s linear; 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);
}
} }
} }
`, `,
@ -63,6 +90,7 @@ class Switch extends Component {
// 尺寸 // 尺寸
css` css`
@use 'sass:map'; @use 'sass:map';
@use 'sass:math';
$sizes: ( $sizes: (
m: ( m: (
w: 72px, w: 72px,
@ -91,14 +119,29 @@ class Switch extends Component {
) )
); );
@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}']) {
height: map.get($v, 'h'); height: map.get($v, 'h');
font-size: map.get($v, 'f'); font-size: map.get($v, 'f');
.dot { .dot {
width: #{map.get($v, 'f') * 2.5}; min-width: #{map.get($v, 'f') * 2.5};
height: #{map.get($v, 'f') * 1.25}; 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')};
}
} }
} }
} }
@ -117,11 +160,7 @@ class Switch extends Component {
@loop $t, $c in $colors { @loop $t, $c in $colors {
:host([type='#{$t}']) { :host([type='#{$t}']) {
label { .dot.open {
color: var(--color-#{$c}-1);
}
.dot {
background: var(--color-#{$c}-1); background: var(--color-#{$c}-1);
} }
@ -151,18 +190,13 @@ class Switch extends Component {
ev.stopPropagation() ev.stopPropagation()
this.checked = !this.checked this.value = !this.value
let data = { let data = {
value: this.value, value: this.value
checked: this.checked
} }
if (this.inGroup) {
this.parentNode.$emit('child-change', data)
} else {
this.$emit('change', data) this.$emit('change', data)
} }
}
handleClick(ev) { handleClick(ev) {
if (ev.type === 'click' || ev.keyCode === 32) { if (ev.type === 'click' || ev.keyCode === 32) {
@ -170,20 +204,30 @@ class Switch extends Component {
} }
} }
mounted() { mounted() {}
if (this.parentNode?.tagName === 'WC-CHECKBOX-GROUP') {
this.inGroup = true
}
}
render() { render() {
let classes = classMap({ dot: true, open: this.value })
return html` <label return html` <label
tabindex=${this.disabled ? 'none' : 0} tabindex=${this.disabled ? 'none' : 0}
@click=${this.handleClick} @click=${this.handleClick}
@keydown=${this.handleClick} @keydown=${this.handleClick}
> >
<span class="dot"></span> <span
<slot></slot> class=${classes}
st=${this['inline-text']
? this.value
? this['active-text']
: this['inactive-text']
: ''}
></span>
<slot
>${!this['inline-text']
? this.value
? this['active-text']
: this['inactive-text']
: ''}</slot
>
</label>` </label>`
} }
} }