优化star组件

master
yutent 2023-11-21 12:16:18 +08:00
parent 88634682f7
commit d2f1c0a3c3
1 changed files with 19 additions and 11 deletions

View File

@ -9,7 +9,7 @@ import '../icon/index.js'
class Star extends Component { class Star extends Component {
static props = { static props = {
value: 0, value: 'num!0',
text: [], text: [],
allowHalf: false, allowHalf: false,
showValue: false, showValue: false,
@ -23,9 +23,10 @@ class Star extends Component {
display: flex; display: flex;
font-size: 14px; font-size: 14px;
cursor: pointer; cursor: pointer;
-webkit-user-select: none;
user-select: none; user-select: none;
} }
label { main {
display: flex; display: flex;
align-items: center; align-items: center;
line-height: 1; line-height: 1;
@ -44,7 +45,7 @@ class Star extends Component {
&:nth-child(#{$i}) { &:nth-child(#{$i}) {
&[name='star-full'], &[name='star-full'],
&[name='star-half'] { &[name='star-half'] {
color: var(--star-active-#{$i}, var(--color-teal-1)); color: var(--wc-star-active-#{$i}, var(--color-teal-1));
} }
} }
} }
@ -82,6 +83,13 @@ class Star extends Component {
font-size: map.get($v, 'f'); font-size: map.get($v, 'f');
} }
} }
`,
// 状态
css`
:host([disabled]) {
cursor: not-allowed;
opacity: 0.6;
}
` `
] ]
@ -134,7 +142,7 @@ class Star extends Component {
this.#value = { i, f } this.#value = { i, f }
} }
handleMove(ev) { #handleMove(ev) {
if (this.disabled) { if (this.disabled) {
return return
} }
@ -145,14 +153,14 @@ class Star extends Component {
} }
} }
handleLeave() { #handleLeave() {
if (this.disabled) { if (this.disabled) {
return return
} }
this.#updateDraw(-1) this.#updateDraw(-1)
} }
handleClick(ev) { #handleClick(ev) {
let tmp = this.#value let tmp = this.#value
if (this.disabled) { if (this.disabled) {
return return
@ -184,11 +192,11 @@ class Star extends Component {
val = val || '' val = val || ''
} }
return html` return html`
<label <main
ref="box" ref="box"
@mousemove=${this.handleMove} @mousemove=${this.#handleMove}
@mouseleave=${this.handleLeave} @mouseleave=${this.#handleLeave}
@click=${this.handleClick} @click=${this.#handleClick}
> >
<wc-icon data-idx="0" name="star"></wc-icon> <wc-icon data-idx="0" name="star"></wc-icon>
<wc-icon data-idx="1" name="star"></wc-icon> <wc-icon data-idx="1" name="star"></wc-icon>
@ -196,7 +204,7 @@ class Star extends Component {
<wc-icon data-idx="3" name="star"></wc-icon> <wc-icon data-idx="3" name="star"></wc-icon>
<wc-icon data-idx="4" name="star"></wc-icon> <wc-icon data-idx="4" name="star"></wc-icon>
<span class="text">${this.showValue ? val : ''}</span> <span class="text">${this.showValue ? val : ''}</span>
</label> </main>
` `
} }
} }