This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0
wcui/src/form/passwd.wc

467 lines
8.7 KiB
Plaintext

<template>
<div class="label">
<slot class="prepend" name="prepend"></slot>
<input spellcheck="false" type="password" />
<wc-icon class="icon" is="eye"></wc-icon>
<slot class="append" name="append"></slot>
</div>
</template>
<style lang="scss">
ul,
li {
list-style: none;
}
:host {
overflow: hidden;
display: inline-flex;
min-width: 128px;
user-select: none;
-moz-user-select: none;
color: var(--color-dark-1);
border-radius: 3px;
cursor: text;
transition: box-shadow 0.15s linear;
}
.label {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
height: 36px;
font-size: 14px;
border: 1px solid var(--color-grey-2);
border-radius: inherit;
background: var(--bg-color, #fff);
color: inherit;
cursor: inherit;
input {
flex: 1;
min-width: 36px;
width: 0;
height: 100%;
padding: 0 8px;
border: 0;
border-radius: inherit;
color: inherit;
font: inherit;
background: none;
outline: none;
box-shadow: none;
cursor: inherit;
&::placeholder {
color: var(--color-grey-1);
}
}
.prepend,
.append {
display: none;
justify-content: center;
align-items: center;
width: auto;
height: 34px;
padding: 0 10px;
line-height: 1;
white-space: nowrap;
}
.prepend {
border-right: 1px solid var(--color-grey-a);
border-radius: 6px 0 0 6px;
}
.append {
border-left: 1px solid var(--color-grey-a);
border-radius: 0 6px 6px 0;
}
&[prepend] .prepend,
&[append] .append {
display: flex;
}
&[prepend] input,
&[append] input {
min-width: 64px;
}
/* ----- */
.icon {
--size: 18px;
margin: 0 8px 0 4px;
color: var(--color-grey-2);
cursor: pointer;
}
}
:host([size='large']) {
min-width: 234px;
.label {
height: 52px;
font-size: 18px;
input {
padding: 0 16px;
}
}
.prepend,
.append {
height: 48px;
padding: 0 16px;
}
.icon {
--size: 24px;
margin: 0 20px 0 4px;
}
}
:host([size='medium']) {
min-width: 160px;
.label {
height: 44px;
input {
padding: 0 10px;
}
}
.prepend,
.append {
height: 40px;
}
}
:host([size='small']) {
min-width: 96px;
.label {
height: 32px;
input {
padding: 0 6px;
}
}
.prepend,
.append {
height: 28px;
}
}
:host([size='mini']) {
min-width: 72px;
.label {
height: 26px;
font-size: 12px;
}
.icon {
--size: 14px;
}
.prepend,
.append {
height: 22px;
}
}
/* 额外样式 */
:host([round]) {
border-radius: 26px;
.label input {
padding: 0 26px;
}
.label[prepend] input,
.label[append] input {
padding: 0 5px;
}
.label[prepend] input {
margin-left: 0;
}
.label[append] input {
margin-right: 0;
}
.prepend {
border-radius: 26px 0 0 26px;
}
.append {
border-radius: 0 26px 26px 0;
}
}
/* ----- 类型(颜色) ----- */
:host(:focus-within) {
box-shadow: 0 0 0 2px var(--color-plain-a);
}
:host([type='primary']:focus-within) {
box-shadow: 0 0 0 2px var(--color-teal-a);
}
:host([type='info']:focus-within) {
box-shadow: 0 0 0 2px var(--color-blue-a);
}
:host([type='success']:focus-within) {
box-shadow: 0 0 0 2px var(--color-green-a);
}
:host([type='danger']:focus-within) {
box-shadow: 0 0 0 2px var(--color-red-a);
}
:host([type='warning']:focus-within) {
box-shadow: 0 0 0 2px var(--color-orange-a);
}
:host([type='primary']) .label {
border-color: var(--color-teal-2);
.prepend,
.append {
border-color: var(--color-teal-a);
}
}
:host([type='info']) .label {
border-color: var(--color-blue-2);
.prepend,
.append {
border-color: var(--color-blue-a);
}
}
:host([type='success']) .label {
border-color: var(--color-green-2);
.prepend,
.append {
border-color: var(--color-green-a);
}
}
:host([type='danger']) .label {
border-color: var(--color-red-2);
.prepend,
.append {
border-color: var(--color-red-a);
}
}
:host([type='warning']) .label {
border-color: var(--color-orange-2);
.prepend,
.append {
border-color: var(--color-orange-a);
}
}
/* --- */
:host([disabled]) {
cursor: not-allowed;
.label {
border-color: var(--color-grey-1);
background: var(--color-plain-1);
opacity: 0.6;
}
}
:host([readonly]) {
cursor: default;
}
:host([no-border]),
:host(:focus-within[no-border]) {
.label {
border: 0;
&[prepend] input {
padding-left: 2px;
}
&[append] input {
padding-right: 2px;
}
}
.prepend,
.append {
border: 0;
}
}
</style>
<script>
import '../icon/index'
import $ from '../utils'
export default class Passwd {
props = {
value: '',
placeholder: '',
maxlength: 0,
minlength: 0,
autofocus: false,
readonly: false,
disabled: false,
lazy: 0 // 输入建议并发拦截时间, 单位毫秒
}
__init__() {
/* render */
this.__OUTER__ = this.root.children[1]
this.__PREPEND__ = this.__OUTER__.children[0]
this.__INPUT__ = this.__OUTER__.children[1]
this.__EYE__ = this.__OUTER__.children[2]
this.__APPEND__ = this.__OUTER__.children[3]
}
get readOnly() {
return this.props.readonly
}
set readOnly(val) {
var type = typeof val
if (val === this.props.readonly) {
return
}
if ((type === 'boolean' && val) || type !== 'boolean') {
this.props.readonly = true
this.setAttribute('readonly', '')
this.__INPUT__.setAttribute('readonly', '')
} else {
this.props.readonly = false
this.removeAttribute('readonly')
this.__INPUT__.removeAttribute('readonly')
}
}
get disabled() {
return this.props.disabled
}
set disabled(val) {
var type = typeof val
if (val === this.props.disabled) {
return
}
if ((type === 'boolean' && val) || type !== 'boolean') {
this.props.disabled = true
this.setAttribute('disabled', '')
this.__INPUT__.setAttribute('disabled', '')
} else {
this.props.disabled = false
this.removeAttribute('disabled')
this.__INPUT__.removeAttribute('disabled')
}
}
get value() {
return this.__INPUT__.value
}
set value(val) {
this.__INPUT__.value = val
}
mounted() {
var prepend = this.__PREPEND__.assignedNodes()
var append = this.__APPEND__.assignedNodes()
this.stamp = 0
// 相同插槽, 只允许1个
while (prepend.length > 1) {
this.removeChild(prepend.pop())
}
while (append.length > 1) {
this.removeChild(append.pop())
}
if (prepend.length) {
this.__OUTER__.setAttribute('prepend', '')
}
if (append.length) {
this.__OUTER__.setAttribute('append', '')
}
// 键盘事件
this._handleSubmit = $.catch(this.__INPUT__, 'keydown', ev => {
let { minlength, lazy } = this.props
let val = this.value
let now = Date.now()
if (this.disabled || this.readOnly) {
return
}
// 回车触发submit事件
if (ev.keyCode === 13) {
// 并发拦截
if (lazy && now - this.stamp < lazy) {
return
}
// 长度不够,拦截submit
if (minlength && minlength > 0) {
if (val.length < minlength) {
return
}
}
this.stamp = now
this.dispatchEvent(new CustomEvent('submit', { detail: this.value }))
}
})
this._eyeFn = $.bind(this.__EYE__, 'click', ev => {
if (this.__INPUT__.type === 'password') {
this.__INPUT__.type = 'text'
this.__EYE__.is = 'eye-off'
} else {
this.__INPUT__.type = 'password'
this.__EYE__.is = 'eye'
}
})
}
unmount() {
$.unbind(this.__INPUT__, 'keydown', this._handleSubmit)
$.unbind(this.__EYE__, 'click', this._eyeFn)
}
watch() {
switch (name) {
case 'autofocus':
this.__INPUT__.setAttribute('autofocus', '')
// 辣鸡火狐, 要触发一下focus, 才能聚焦
setTimeout(_ => {
this.__INPUT__.focus()
}, 10)
break
case 'placeholder':
this.__INPUT__.setAttribute('placeholder', val)
break
case 'value':
this.value = val
break
case 'maxlength':
if (val === null) {
this.__INPUT__.removeAttribute(name)
} else {
let n = +val
if (n > 0) {
this.__INPUT__.setAttribute(name, n)
} else {
this.removeAttribute(name)
}
}
break
case 'lazy':
this.props.lazy = val >> 0
break
case 'readonly':
case 'disabled':
var k = name
if (k === 'readonly') {
k = 'readOnly'
}
this[k] = val !== null
break
}
}
}
</script>
wcui是一套基于`Web Components`的UI组件库, 宗旨是追求简单、实用、不花哨。
JavaScript 95.2%
CSS 4.8%