重构passwd组件

master
yutent 2023-11-29 14:17:23 +08:00
parent 7654fc8da7
commit a10f3ccd63
2 changed files with 44 additions and 56 deletions

View File

@ -78,4 +78,9 @@ code, pre, samp {font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;
--color-dark-1: #64748B; --color-dark-1: #64748B;
--color-dark-2: #475569; --color-dark-2: #475569;
--color-dark-3: #2c3441; --color-dark-3: #2c3441;
--color-drag-background: #fdf6ec;
--color-readonly-background: #f7f8fb;
--color-disabled-background: #fef0f0;
} }

View File

@ -22,15 +22,15 @@ class Passwd extends Component {
overflow: hidden; overflow: hidden;
display: inline-flex; display: inline-flex;
min-width: 188px; min-width: 188px;
user-select: none;
-moz-user-select: none;
color: var(--color-dark-1); color: var(--color-dark-1);
border-radius: 3px; border-radius: 3px;
cursor: text; cursor: text;
transition: box-shadow 0.15s linear; transition: box-shadow 0.15s linear;
-webkit-user-select: none;
user-select: none;
} }
.label { .container {
flex: 1; flex: 1;
display: flex; display: flex;
justify-content: center; justify-content: center;
@ -52,7 +52,8 @@ class Passwd extends Component {
border: 0; border: 0;
border-radius: inherit; border-radius: inherit;
color: inherit; color: inherit;
font: inherit; font-family: inherit;
font-size: inherit;
background: none; background: none;
outline: none; outline: none;
box-shadow: none; box-shadow: none;
@ -63,7 +64,7 @@ class Passwd extends Component {
} }
} }
} }
:host([round]) .label input { :host([round]) .container input {
padding: 0 6px 0 20px; padding: 0 6px 0 20px;
} }
:host(:focus-within) { :host(:focus-within) {
@ -71,7 +72,7 @@ class Passwd extends Component {
} }
`, `,
css` css`
.label { .container {
.icon { .icon {
--wc-icon-size: 16px; --wc-icon-size: 16px;
margin: 0 8px 0 4px; margin: 0 8px 0 4px;
@ -82,39 +83,10 @@ class Passwd extends Component {
`, `,
// 尺寸 // 尺寸
css` css`
@use 'sass:map'; :host([size='small']) {
$sizes: ( min-width: 128px;
m: ( .container {
w: 128px, height: 24px;
h: 24px,
f: 12px
),
// l default
xl:
(
w: 224px,
h: 36px,
f: 14px
),
xxl: (
w: 288px,
h: 44px,
f: 14px
)
);
@loop $s, $v in $sizes {
:host([size='#{$s}']) {
min-width: map.get($v, 'w');
.label {
height: map.get($v, 'h');
}
@if $s == 'xxl' {
.icon {
--wc-icon-size: 18px;
}
}
} }
} }
`, `,
@ -122,56 +94,67 @@ class Passwd extends Component {
:host([disabled]), :host([disabled]),
:host([readonly]) { :host([readonly]) {
cursor: default; cursor: default;
.icon { .icon {
cursor: default; cursor: inherit;
}
.container {
background: var(--color-readonly-background);
opacity: 0.6;
} }
} }
:host([disabled]) { :host([disabled]) {
cursor: not-allowed; cursor: not-allowed;
.label { .container {
border-color: var(--color-grey-1); background: var(--color-disabled-background);
background: var(--color-plain-1);
opacity: 0.6;
} }
} }
` `
] ]
#type = 'password' #type = 'password'
#stamp = 0
mounted() { mounted() {
if (this.autofocus) { if (this.autofocus) {
nextTick(_ => this.$refs.input.focus()) this.$refs.input.focus()
} }
} }
iconClick(ev) { #toggleView(ev) {
if (this.readOnly || this.disabled) { if (this.readOnly || this.disabled) {
return return
} }
this.#type = this.#type === 'password' ? '' : 'password' this.#type = this.#type === 'password' ? 'text' : 'password'
this.$requestUpdate() this.$requestUpdate()
} }
handleInput(ev) { #fetch(ev) {
let now = Date.now()
let { lazy } = this
this.value = ev.target.value this.value = ev.target.value
// 并发拦截
if (lazy && now - this.#stamp < lazy) {
return
} }
handleChange() { this.#stamp = now
this.$emit('change') this.$emit(ev.type)
} }
render() { render() {
return html` return html`
<div class="label"> <main class="container">
<slot class="prepend" name="prepend"></slot> <slot class="prepend" name="prepend"></slot>
<input <input
spellcheck="false" spellcheck="false"
ref="input" ref="input"
placeholder=${this.placeholder} placeholder=${this.placeholder}
@input=${this.handleInput} @input.stop=${this.#fetch}
@change=${this.handleChange} @change=${this.#fetch}
:readOnly=${this.readOnly} :readOnly=${this.readOnly}
:disabled=${this.disabled} :disabled=${this.disabled}
:type=${this.#type} :type=${this.#type}
@ -179,10 +162,10 @@ class Passwd extends Component {
/> />
<wc-icon <wc-icon
class="icon" class="icon"
@click=${this.iconClick} @click=${this.#toggleView}
:name=${this.#type === 'password' ? 'eye' : 'eye-off'} :name=${this.#type === 'password' ? 'eye' : 'eye-off'}
></wc-icon> ></wc-icon>
</div> </main>
` `
} }
} }