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/input.wc

538 lines
11 KiB
Plaintext

<template>
<div class="label">
<slot class="prepend" name="prepend"></slot>
${input}
<wc-icon class="icon"></wc-icon>
<slot class="append" name="append"></slot>
<div class="suggestion">
<wc-scroll>
<ul class="list"></ul>
</wc-scroll>
</div>
</div>
</template>
<style lang="scss">
ul,
li {
list-style: none;
}
:host {
overflow: hidden;
display: inline-block;
user-select: none;
-moz-user-select: none;
color: nth($cd, 2);
border-radius: 4px;
}
.label {
display: flex;
justify-content: center;
align-items: center;
min-width: 64px;
height: 32px;
font-size: 14px;
border: 1px solid nth($cp, 3);
border-radius: inherit;
background: #fff;
color: inherit;
cursor: text;
input,
textarea {
flex: 1;
min-width: 0;
height: 100%;
padding: 0 5px;
border: 0;
border-radius: inherit;
color: inherit;
font-size: inherit;
background: none;
outline: none;
box-shadow: none;
cursor: inherit;
&::placeholder {
color: nth($cgr, 1);
}
}
textarea {
width: 100%;
padding: 5px;
resize: none;
}
.prepend,
.append {
display: none;
justify-content: center;
align-items: center;
width: auto;
height: 30px;
padding: 0 10px;
background: nth($cp, 1);
}
.prepend {
border-right: 1px solid nth($cp, 3);
border-radius: 4px 0 0 4px;
}
.append {
border-left: 1px solid nth($cp, 3);
border-radius: 0 4px 4px 0;
}
&[prepend] .prepend,
&[append] .append {
display: flex;
}
/* ----- */
.icon {
padding: 0 5px;
--size: 20px;
}
}
.suggestion {
display: none;
position: fixed;
z-index: 10240;
left: 0;
top: 0;
width: 200px;
height: auto;
max-height: 200px;
min-height: 46px;
padding: 8px 0;
border-radius: 4px;
background: #fff;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.2);
.list {
width: 100%;
}
&::after {
position: absolute;
left: 30px;
top: -4px;
width: 8px;
height: 8px;
background: #fff;
box-shadow: -1px -1px 2px rgba(0, 0, 0, 0.1);
transform: rotate(45deg);
content: '';
}
&.show {
display: flex;
}
li {
overflow: hidden;
width: 100%;
height: 30px;
line-height: 30px;
padding: 0 8px;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
&:hover,
&[focus] {
background: nth($cp, 1);
}
}
}
/* --- */
:host([disabled]) .label {
background: nth($cp, 1);
cursor: not-allowed;
opacity: 0.6;
}
:host(:focus-within) {
box-shadow: 0 0 3px nth($ct, 1);
}
:host(:focus-within[readonly]) {
box-shadow: 0 0 3px nth($co, 1);
}
:host([type='textarea']) {
display: flex;
height: 80px;
.label {
width: 100%;
height: 100%;
}
.icon,
.suggestion {
display: none;
}
}
/* 额外样式 */
:host([round]) {
border-radius: 21px;
.prepend {
border-radius: 21px 0 0 21px;
}
.append {
border-radius: 0 21px 21px 0;
}
}
:host([size='large']) {
.label {
height: 42px;
font-size: 16px;
}
.prepend,
.append {
height: 40px;
}
}
:host([size='medium']) {
.label {
height: 36px;
}
.prepend,
.append {
height: 34px;
}
}
:host([size='mini']) {
.label {
height: 24px;
font-size: 12px;
}
.icon {
--size: 16px;
}
.prepend,
.append {
height: 18px;
}
}
</style>
<script>
import '../scroll/index'
import '../icon/index'
import { nextTick, ebind, bind, unbind } from '../utils'
const IS_FIREFOX = !!window.sidebar
const TYPES = ['text', 'textarea', 'password']
const INPUTS = {
text: '<input spellcheck="false">',
textarea: '<textarea spellcheck="false"></textarea>'
}
export default class Input {
props = {
value: '',
icon: '',
type: 'text',
label: '',
placeholder: '',
mvidx: null, //下拉列表光标的索引ID
autofocus: false,
readonly: false,
disabled: false
}
constructor() {
var type = this.getAttribute('type')
var input = ''
if (type !== 'textarea') {
type = 'text'
}
input = INPUTS[type]
/* render */
this.props.type = type
this.__OUTER__ = this.root.children[1]
this.__PREPEND__ = this.__OUTER__.children[0]
this.__INPUT__ = this.__OUTER__.children[1]
this.__ICO__ = this.__OUTER__.children[2]
this.__APPEND__ = this.__OUTER__.children[3]
this.__LIST__ = this.__OUTER__.children[4]
}
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
}
get type() {
return this.__INPUT__.type
}
set type(val) {
if (val !== 'textarea') {
this.__INPUT__.type = val
}
}
// 移动光标选择下拉选项
_moveSelect(ev) {
var { list } = this.props
if (list && list.length) {
ev.preventDefault()
var step = ev.keyCode === 38 ? -1 : 1
var items = Array.from(
this.__LIST__.firstElementChild.firstElementChild.children
)
if (this.props.mvidx === null) {
this.props.mvidx = 0
} else {
this.props.mvidx += step
}
if (this.props.mvidx < 0) {
this.props.mvidx = 0
} else if (this.props.mvidx > items.length - 1) {
this.props.mvidx = items.length - 1
}
items.forEach((it, i) => {
if (i === this.props.mvidx) {
this.__LIST__.firstElementChild.scrollTop = it.offsetTop - 150
it.setAttribute('focus', '')
} else {
it.removeAttribute('focus')
}
})
}
}
// 触发列表选择
_fetchSelect(idx, ev) {
var item = this.props.list[idx]
this.value = item.value
this.dispatchEvent(
new CustomEvent('select', {
detail: item
})
)
this._handleChange(ev)
this.__LIST__.classList.remove('show')
this.props.mvidx = null
}
mounted() {
var prepend = this.__PREPEND__.assignedNodes()
var append = this.__APPEND__.assignedNodes()
// 相同插槽, 只允许1个
while (prepend.length > 1) {
this.removeChild(prepend.pop())
}
while (append.length > 1) {
this.removeChild(append.pop())
}
if (prepend.length && this.props.type !== 'textarea') {
this.__OUTER__.setAttribute('prepend', '')
}
if (append.length && this.props.type !== 'textarea') {
this.__OUTER__.setAttribute('append', '')
}
var { type } = this.props
// 键盘事件
this._handleSubmit = ebind(this.__INPUT__, 'keydown', ev => {
if (this.disabled || this.readonly) {
return
}
// up: 38, down: 40
if (ev.keyCode === 38 || ev.keyCode === 40) {
// 仅普通文本表单, 密码和多行文本框不做响应
if (this.type === 'text') {
return this._moveSelect(ev)
}
}
// 回车触发submit事件
// textarea 要按Ctrl Or Cmd键, 才会触发
if (ev.keyCode === 13) {
// 如果是输入建议存在,则第1次回车的时候, 不触发提交
if (this.type === 'text' && this.props.mvidx !== null) {
return this._fetchSelect(this.props.mvidx, ev)
}
if (
type === 'text' ||
(type === 'textarea' && (ev.ctrlKey || ev.metaKey))
) {
this.dispatchEvent(
new CustomEvent('submit', {
detail: this.value
})
)
}
}
})
this._handleWheel = ebind(this.__INPUT__, 'wheel')
// 非textarea, 可做输入建议功能
if (type === 'text') {
// 输入状态事件
this._handleChange = bind(this.__INPUT__, 'input', ev => {
ev.preventDefault()
this.dispatchEvent(
new CustomEvent('fetch-suggest', {
detail: {
value: this.value,
send: list => {
this.props.list = list
this._parseSuggestion()
}
}
})
)
})
// 渲染建议列表
this._parseSuggestion = bind(this.__INPUT__, 'click', ev => {
var { list } = this.props
let { x, y, width } = this.getBoundingClientRect()
if (list && list.length) {
var html = list
.map((it, i) => `<li data-idx="${i}">${it.value}</li>`)
.join('')
this.__LIST__.firstElementChild.firstElementChild.innerHTML = html
this.__LIST__.classList.toggle('show', true)
this.__LIST__.style.cssText = `left:${x}px;top:${y +
50}px;width:${width}px;`
} else {
this.__LIST__.classList.toggle('show', false)
}
})
this._bubbleFn = ebind(this, 'click')
this._inactiveFn = bind(document, 'click', ev => {
this.__LIST__.classList.remove('show')
})
// 选择建议
this._handleSelect = bind(this.__LIST__, 'click', ev => {
if (ev.target.tagName === 'LI') {
this._fetchSelect(ev.target.dataset.idx, ev)
this.dispatchEvent(new CustomEvent('input'))
}
})
}
}
unmount() {
unbind(this.__INPUT__, 'wheel', this._handleWheel)
unbind(this.__INPUT__, 'keydown', this._handleSubmit)
unbind(this.__INPUT__, 'input', this._handleChange)
unbind(this, 'click', this._bubbleFn)
unbind(document, 'click', this._inactiveFn)
unbind(this.__LIST__, 'click', this._handleSelect)
}
watch(name, old, val) {
if (old === val) {
return
}
switch (name) {
case 'icon':
this.props.icon = val
if (val) {
this.__ICO__.setAttribute('is', val)
} else {
this.removeAttribute('icon')
this.__ICO__.removeAttribute('is')
}
break
case 'autofocus':
this.__INPUT__.setAttribute('autofocus', '')
// 辣鸡火狐, 要触发一下focus, 才能聚焦
if (IS_FIREFOX) {
setTimeout(_ => {
this.__INPUT__.focus()
}, 10)
}
break
// label和placeholder 功能相同
case 'label':
case 'placeholder':
this.__INPUT__.setAttribute('placeholder', val)
break
case 'type':
if (~TYPES.indexOf(val)) {
this.type = val
} else {
this.type = 'text'
}
break
case 'value':
this.value = val
break
case 'readonly':
case 'disabled':
if (val === '') {
this[name] = true
}
break
}
}
}
</script>
wcui是一套基于`Web Components`的UI组件库, 宗旨是追求简单、实用、不花哨。
JavaScript 95.2%
CSS 4.8%