完成单选组件的重构

master
yutent 2023-11-20 18:53:51 +08:00
parent 44472fcf23
commit 0ac39d443f
2 changed files with 34 additions and 43 deletions

View File

@ -61,47 +61,41 @@ class Radio extends Component {
class RadioItem extends Component { class RadioItem extends Component {
static props = { static props = {
value: { value: 'str!',
type: String,
default: '',
attribute: false
},
checked: false, checked: 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;
padding-right: 16px; padding-right: 16px;
line-height: 1; line-height: 1;
-moz-user-select: none;
user-select: none;
white-space: nowrap; white-space: nowrap;
align-items: center;
font-size: 14px;
cursor: inherit; cursor: inherit;
outline: none; outline: none;
color: var(--color-dark-1); color: var(--color-dark-1);
-webkit-user-select: none;
user-select: none;
} }
.dot { .dot {
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
width: 16px; width: 14px;
height: 16px; height: 14px;
margin-right: 4px; margin-right: 4px;
border: 1px solid var(--color-dark-1); border: 1px solid var(--color-grey-2);
border-radius: 50%; border-radius: 50%;
background: #fff; background: #fff;
transition: box-shadow 0.15s linear; transition: box-shadow 0.15s linear;
@ -132,38 +126,35 @@ class RadioItem extends Component {
`, `,
// 尺寸 // 尺寸
css` css`
@use 'sass:math';
@use 'sass:map'; @use 'sass:map';
$sizes: ( $sizes: (
s: (
h: 20px,
f: 12px
),
m: ( m: (
w: 72px,
h: 24px, h: 24px,
f: 12px f: 12px
), ),
l: (
w: 108px,
h: 32px,
f: 14px
),
xl: ( xl: (
w: 132px,
h: 36px, h: 36px,
f: 14px f: 16px
),
xxl: (
w: 160px,
h: 44px,
f: 14px
) )
); );
@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');
.dot { .dot {
width: #{map.get($v, 'f')}; width: map.get($v, 'f');
height: #{map.get($v, 'f')}; height: map.get($v, 'f');
&::after {
width: math.div(map.get($v, 'f'), 2);
height: math.div(map.get($v, 'f'), 2);
}
} }
} }
} }
@ -202,17 +193,17 @@ class RadioItem extends Component {
`, `,
// 状态 // 状态
css` css`
:host([readonly]),
:host([disabled]) { :host([disabled]) {
cursor: not-allowed;
opacity: 0.6; opacity: 0.6;
} }
:host([readonly]) { :host([disabled]) {
cursor: default; cursor: not-allowed;
} }
` `
] ]
toggleCheck(ev) { #toggleCheck(ev) {
if (this.disabled || this.readOnly || this.checked) { if (this.disabled || this.readOnly || this.checked) {
return return
} }
@ -228,9 +219,9 @@ class RadioItem extends Component {
} }
} }
handleClick(ev) { #click(ev) {
if (ev.type === 'click' || ev.keyCode === 32) { if (ev.type === 'click' || ev.keyCode === 32) {
this.toggleCheck(ev) this.#toggleCheck(ev)
} }
} }
@ -243,8 +234,8 @@ class RadioItem extends Component {
render() { render() {
return html` <label return html` <label
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="dot"></span> <span class="dot"></span>
<slot></slot> <slot></slot>

View File

@ -38,7 +38,7 @@ class Switch extends Component {
&.open .switch { &.open .switch {
flex-direction: row-reverse; flex-direction: row-reverse;
background: var(--color-teal-1); background: var(--color-grey-3);
} }
} }
@ -119,6 +119,7 @@ class Switch extends Component {
// 配色 // 配色
css` css`
$colors: ( $colors: (
primay: 'teal',
info: 'blue', info: 'blue',
success: 'green', success: 'green',
warning: 'orange', warning: 'orange',
@ -141,11 +142,10 @@ class Switch extends Component {
css` css`
:host([readonly]), :host([readonly]),
:host([disabled]) { :host([disabled]) {
cursor: not-allowed;
opacity: 0.6; opacity: 0.6;
} }
:host([readonly]) { :host([disabled]) {
cursor: default; cursor: not-allowed;
} }
` `
] ]