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

优化按钮组件的焦点获取;完成input组件的开发

old
宇天 2019-07-24 21:20:20 +08:00
parent 1003ec1e0d
commit b6a8996c69
4 changed files with 262 additions and 15 deletions

View File

@ -66,7 +66,7 @@ function mkWCFile({ style, html, js }) {
let name = '' let name = ''
let props = '' let props = ''
js = js.replace(/props = (\{[\w\W]*?\})/, function(s, m) { js = js.replace(/props = (\{[\w\W]*?\n\s{2}?\})/, function(s, m) {
props = m props = m
var attr = new Function( var attr = new Function(
`var props = ${m}, attr = []; for(var i in props){attr.push(i)}; return attr` `var props = ${m}, attr = []; for(var i in props){attr.push(i)}; return attr`

View File

@ -75,7 +75,7 @@ function mkWCFile({ style, html, js }) {
let name, props let name, props
js = js.replace(/props = (\{[\w\W]*?\})/, function(s, m) { js = js.replace(/props = (\{[\w\W]*?\n\s{2}?\})/, function(s, m) {
props = m props = m
var attr = new Function( var attr = new Function(
`var props = ${m}, attr = []; for(var i in props){attr.push(i)}; return attr` `var props = ${m}, attr = []; for(var i in props){attr.push(i)}; return attr`

View File

@ -239,6 +239,7 @@
<script> <script>
import '../icon/index' import '../icon/index'
const IS_FIREFOX = !!window.sidebar
export default class Button { export default class Button {
props = { props = {
@ -335,9 +336,13 @@ export default class Button {
break break
case 'autofocus': case 'autofocus':
this.__BTN__.setAttribute('autofocus', '')
// 辣鸡火狐, 要触发一下focus, 才能聚焦
if (IS_FIREFOX) {
setTimeout(_ => { setTimeout(_ => {
this.__BTN__.focus() this.__BTN__.focus()
}, 10) }, 10)
}
break break
case 'loading': case 'loading':

View File

@ -1,21 +1,28 @@
<template> <template>
<label> <div class="label">
<slot class="prepend" name="prepend"></slot> <slot class="prepend" name="prepend"></slot>
${input} ${input}
<wc-icon class="icon"></wc-icon> <wc-icon class="icon"></wc-icon>
<slot class="append" name="append"></slot> <slot class="append" name="append"></slot>
</label>
<ul class="suggestion"></ul>
</div>
</template> </template>
<style lang="scss"> <style lang="scss">
ul,
li {
list-style: none;
}
:host { :host {
display: inline-block; display: inline-block;
user-select: none; user-select: none;
-moz-user-select: none;
color: nth($cd, 2); color: nth($cd, 2);
border-radius: 4px; border-radius: 4px;
} }
label { .label {
position: relative; position: relative;
display: flex; display: flex;
justify-content: center; justify-content: center;
@ -31,7 +38,7 @@ label {
input, input,
textarea { textarea {
width: 100%; min-width: 0;
padding: 0 5px; padding: 0 5px;
border: 0; border: 0;
border-radius: inherit; border-radius: inherit;
@ -41,8 +48,13 @@ label {
outline: none; outline: none;
box-shadow: none; box-shadow: none;
cursor: inherit; cursor: inherit;
&::placeholder {
color: nth($cgr, 1);
}
} }
textarea { textarea {
width: 100%;
height: 100%; height: 100%;
padding: 5px; padding: 5px;
resize: none; resize: none;
@ -77,8 +89,54 @@ label {
--size: 20px; --size: 20px;
} }
} }
.suggestion {
display: none;
position: absolute;
z-index: 10240;
left: 0;
top: 50px;
width: 100%;
height: auto;
min-height: 46px;
padding: 8px 0;
border-radius: 4px;
background: #fff;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.2);
&::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: block;
}
li {
overflow: hidden;
width: 100%;
height: 30px;
line-height: 30px;
padding: 0 8px;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
&:hover {
background: nth($cp, 1);
}
}
}
/* --- */ /* --- */
:host([disabled]) label { :host([disabled]) .label {
background: nth($cp, 1); background: nth($cp, 1);
cursor: not-allowed; cursor: not-allowed;
opacity: 0.6; opacity: 0.6;
@ -86,21 +144,74 @@ label {
:host(:focus-within) { :host(:focus-within) {
box-shadow: 0 0 3px nth($ct, 1); box-shadow: 0 0 3px nth($ct, 1);
} }
:host(:focus-within[readonly]) {
box-shadow: 0 0 3px nth($co, 1);
}
:host([type='textarea']) { :host([type='textarea']) {
display: flex; display: flex;
height: 80px; height: 80px;
label { .label {
overflow: auto;
width: 100%; width: 100%;
height: 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: 20px;
font-size: 12px;
}
.icon {
--size: 16px;
}
.prepend,
.append {
height: 18px;
}
} }
</style> </style>
<script> <script>
import '../icon/index' import '../icon/index'
const IS_FIREFOX = !!window.sidebar
const TYPES = ['text', 'textarea', 'password'] const TYPES = ['text', 'textarea', 'password']
const INPUTS = { const INPUTS = {
text: '<input spellcheck="false">', text: '<input spellcheck="false">',
@ -115,8 +226,10 @@ export default class Input {
label: '', label: '',
placeholder: '', placeholder: '',
autofocus: false, autofocus: false,
readonly: false,
disabled: false disabled: false
} }
constructor() { constructor() {
var type = this.getAttribute('type') var type = this.getAttribute('type')
var input = '' var input = ''
@ -134,6 +247,28 @@ export default class Input {
this.__INPUT__ = this.__OUTER__.children[1] this.__INPUT__ = this.__OUTER__.children[1]
this.__ICO__ = this.__OUTER__.children[2] this.__ICO__ = this.__OUTER__.children[2]
this.__APPEND__ = this.__OUTER__.children[3] 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() { get disabled() {
@ -176,6 +311,21 @@ export default class Input {
} }
} }
_parseSuggestion() {
var { list } = this.props
if (list && list.length) {
var html = list
.map((it, i) => `<li data-idx="${i}">${it.value}</li>`)
.join('')
this.__LIST__.innerHTML = html
this.__LIST__.classList.toggle('show', true)
} else {
this.__LIST__.classList.toggle('show', false)
}
}
_moveSelect() {}
mounted() { mounted() {
var prepend = this.__PREPEND__.assignedNodes() var prepend = this.__PREPEND__.assignedNodes()
var append = this.__APPEND__.assignedNodes() var append = this.__APPEND__.assignedNodes()
@ -194,6 +344,93 @@ export default class Input {
if (append.length && this.props.type !== 'textarea') { if (append.length && this.props.type !== 'textarea') {
this.__OUTER__.setAttribute('append', '') this.__OUTER__.setAttribute('append', '')
} }
var { type } = this.props
this._handleSubmit = ev => {
if (this.disabled || this.readonly) {
ev.cancelBubble = true
return
}
// up: 38, down: 40
log(ev.keyCode)
if (ev.keyCode === 38 || ev.keyCode === 40) {
ev.preventDefault()
return
}
// 回车触发submit事件
// textarea 要按Ctrl Or Cmd键, 才会触发
if (
ev.keyCode === 13 &&
(type === 'text' || (type === 'textarea' && (ev.ctrlKey || ev.metaKey)))
) {
this.dispatchEvent(
new CustomEvent('submit', {
detail: this.value
})
)
}
}
this._handleChange = ev => {
this.dispatchEvent(
new CustomEvent('fetch-suggestions', {
detail: {
value: this.value,
send: list => {
log('----', list)
this.props.list = list
this._parseSuggestion()
}
}
})
)
}
this.__INPUT__.addEventListener('keydown', this._handleSubmit, false)
// 非textarea, 可做输入建议功能
if (type === 'text') {
this.__INPUT__.addEventListener('input', this._handleChange, false)
this.__INPUT__.addEventListener(
'focus',
this._parseSuggestion.bind(this),
false
)
this.__INPUT__.addEventListener(
'blur',
ev => {
setTimeout(() => {
this.__LIST__.classList.remove('show')
}, 50)
},
false
)
this.__LIST__.addEventListener(
'click',
ev => {
this.__LIST__.classList.remove('show')
if (ev.target.tagName === 'LI') {
var idx = ev.target.dataset.idx
var item = this.props.list[idx]
this.value = item.value
this.dispatchEvent(
new CustomEvent('select', {
detail: item
})
)
}
},
false
)
}
}
unmount() {
this.__INPUT__.removeEventListener('keydown', this._handleSubmit)
this.__INPUT__.removeEventListener('input', this._handleChange)
} }
watch(name, old, val) { watch(name, old, val) {
@ -212,9 +449,13 @@ export default class Input {
break break
case 'autofocus': case 'autofocus':
this.__INPUT__.setAttribute('autofocus', '')
// 辣鸡火狐, 要触发一下focus, 才能聚焦
if (IS_FIREFOX) {
setTimeout(_ => { setTimeout(_ => {
this.__INPUT__.focus() this.__INPUT__.focus()
}, 10) }, 10)
}
break break
// label和placeholder 功能相同 // label和placeholder 功能相同
@ -231,6 +472,7 @@ export default class Input {
} }
break break
case 'readonly':
case 'disabled': case 'disabled':
if (val === '') { if (val === '') {
this[name] = true this[name] = true