Browse Source

优化button

pull/9/head
tangjinzhou 7 years ago
parent
commit
d070be113b
  1. 90
      components/button/button.vue
  2. 8
      components/button/demo/loading.vue

90
components/button/button.vue

@ -1,15 +1,7 @@
<template>
<button :type="htmlType" :class="classes" :disabled="disabled"
@click="handleClick" @mouseout="mouseout" @mouseover="mouseover">
<Icon v-if="iconType" :type="iconType"></Icon>
<span v-if="this.$slots.default">
<slot></slot>
</span>
</button>
</template>
<script> <script>
import Icon from '../icon' import Icon from '../icon'
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar)
export default { export default {
name: 'Button', name: 'Button',
components: { Icon }, components: { Icon },
@ -40,7 +32,7 @@ export default {
return ['small', 'large', 'default'].includes(value) return ['small', 'large', 'default'].includes(value)
}, },
}, },
loading: Boolean, loading: [Boolean, Object],
disabled: Boolean, disabled: Boolean,
ghost: Boolean, ghost: Boolean,
}, },
@ -51,48 +43,100 @@ export default {
small: 'sm', small: 'sm',
}, },
clicked: false, clicked: false,
sLoading: !!this.loading,
} }
}, },
watch: {
loading: {
handler: function (val) {
clearTimeout(this.delayTimeout)
if (typeof val !== 'boolean' && val && val.delay) {
this.delayTimeout = setTimeout(() => { this.sLoading = !!val }, val.delay)
} else {
this.sLoading = !!val
}
},
deep: true,
},
},
computed: { computed: {
classes () { classes () {
const { prefixCls, type, shape, size, loading, ghost, clicked, sizeMap } = this const { prefixCls, type, shape, size, sLoading, ghost, clicked, sizeMap } = this
const sizeCls = sizeMap[size] || '' const sizeCls = sizeMap[size] || ''
return { return {
[`${prefixCls}`]: true, [`${prefixCls}`]: true,
[`${prefixCls}-${type}`]: type, [`${prefixCls}-${type}`]: type,
[`${prefixCls}-${shape}`]: shape, [`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-${sizeCls}`]: sizeCls, [`${prefixCls}-${sizeCls}`]: sizeCls,
[`${prefixCls}-loading`]: loading, [`${prefixCls}-loading`]: sLoading,
[`${prefixCls}-clicked`]: clicked, [`${prefixCls}-clicked`]: clicked,
[`${prefixCls}-background-ghost`]: ghost || type === 'ghost', [`${prefixCls}-background-ghost`]: ghost || type === 'ghost',
} }
}, },
iconType () { iconType () {
const { loading, icon } = this const { sLoading, icon } = this
return loading ? 'loading' : icon return sLoading ? 'loading' : icon
}, },
}, },
methods: { methods: {
handleClick (event) { handleClick (event) {
if (this.clicked) {
return
}
this.clicked = true this.clicked = true
clearTimeout(this.timeout) clearTimeout(this.timeout)
this.timeout = setTimeout(() => (this.clicked = false), 500) this.timeout = setTimeout(() => (this.clicked = false), 500)
this.$emit('click', event) this.$emit('click', event)
}, },
mouseover (event) { insertSpace (child, needInserted) {
this.$emit('mouseover', event) // Check the child if is undefined or null.
}, if (child == null) {
mouseout (event) { return child
this.$emit('mouseout', event) }
const SPACE = needInserted ? ' ' : ''
if (typeof child.text === 'string') {
let text = child.text.trim()
if (isTwoCNChar(text)) {
text = text.split('').join(SPACE)
}
return <span>{text}</span>
}
return child
}, },
}, },
render () {
const { htmlType, classes, disabled, handleClick, iconType, $slots, $attrs, _events } = this
const buttonProps = {
props: {
},
attrs: {
...$attrs,
type: htmlType,
disabled,
},
class: classes,
on: {
click: handleClick,
},
}
for (const [k, event] of Object.entries(_events)) {
if (!buttonProps.on[k]) {
buttonProps.on[k] = event
}
}
const needInserted = $slots.default && $slots.default.length === 1 && (!iconType || iconType === 'loading')
const kids = $slots.default && $slots.default.length === 1 ? this.insertSpace($slots.default[0], needInserted) : $slots.default
return (
<button {...buttonProps}>
{iconType ? <Icon type={iconType}></Icon> : null}
{kids}
</button>
)
},
beforeDestroy () { beforeDestroy () {
if (this.timeout) { if (this.timeout) {
clearTimeout(this.timeout) clearTimeout(this.timeout)
} }
if (this.delayTimeout) {
clearTimeout(this.delayTimeout)
}
}, },
} }
</script> </script>

8
components/button/demo/loading.vue

@ -7,11 +7,11 @@
Loading Loading
</AntButton> </AntButton>
<br /> <br />
<AntButton type="primary" :loading="loading" @click="enterLoading"> <AntButton type="primary" :loading="loading" @mouseenter="enterLoading">
Click me! mouseenter me!
</AntButton> </AntButton>
<AntButton type="primary" icon="poweroff" :loading="iconLoading" @click="enterIconLoading"> <AntButton type="primary" icon="poweroff" :loading="iconLoading" @click="enterIconLoading">
Click me! 延迟1s
</AntButton> </AntButton>
<br /> <br />
<AntButton shape="circle" loading /> <AntButton shape="circle" loading />
@ -32,7 +32,7 @@ export default {
this.loading = true this.loading = true
}, },
enterIconLoading () { enterIconLoading () {
this.iconLoading = true this.iconLoading = { delay: 1000 }
}, },
}, },
components: { components: {

Loading…
Cancel
Save