ant-design-vue/components/button/button.vue

139 lines
3.5 KiB
Vue
Raw Normal View History

2017-10-26 15:18:08 +08:00
<script>
import Icon from '../icon'
2017-12-29 16:51:06 +08:00
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar)
2017-10-26 15:18:08 +08:00
export default {
name: 'Button',
components: { Icon },
props: {
prefixCls: {
default: 'ant-btn',
type: String,
},
type: {
validator (value) {
return ['primary', 'danger', 'dashed', 'ghost', 'default'].includes(value)
},
},
htmlType: {
default: 'button',
validator (value) {
return ['button', 'submit', 'reset'].includes(value)
},
},
icon: String,
shape: {
validator (value) {
return ['circle', 'circle-outline'].includes(value)
},
},
size: {
validator (value) {
return ['small', 'large', 'default'].includes(value)
},
},
2017-12-29 16:51:06 +08:00
loading: [Boolean, Object],
2017-10-26 15:18:08 +08:00
disabled: Boolean,
ghost: Boolean,
},
data () {
return {
sizeMap: {
large: 'lg',
small: 'sm',
},
clicked: false,
2017-12-29 16:51:06 +08:00
sLoading: !!this.loading,
2017-10-26 15:18:08 +08:00
}
},
2017-12-29 16:51:06 +08:00
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,
},
},
2017-10-26 15:18:08 +08:00
computed: {
classes () {
2017-12-29 16:51:06 +08:00
const { prefixCls, type, shape, size, sLoading, ghost, clicked, sizeMap } = this
2017-10-26 15:18:08 +08:00
const sizeCls = sizeMap[size] || ''
return {
[`${prefixCls}`]: true,
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-${sizeCls}`]: sizeCls,
2017-12-29 16:51:06 +08:00
[`${prefixCls}-loading`]: sLoading,
2017-10-26 15:18:08 +08:00
[`${prefixCls}-clicked`]: clicked,
[`${prefixCls}-background-ghost`]: ghost || type === 'ghost',
}
},
iconType () {
2017-12-29 16:51:06 +08:00
const { sLoading, icon } = this
return sLoading ? 'loading' : icon
2017-10-26 15:18:08 +08:00
},
},
methods: {
handleClick (event) {
this.clicked = true
clearTimeout(this.timeout)
this.timeout = setTimeout(() => (this.clicked = false), 500)
this.$emit('click', event)
},
2017-12-29 16:51:06 +08:00
insertSpace (child, needInserted) {
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
2017-10-26 15:18:08 +08:00
},
},
2017-12-29 16:51:06 +08:00
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>
)
},
2017-10-26 15:18:08 +08:00
beforeDestroy () {
if (this.timeout) {
clearTimeout(this.timeout)
}
2017-12-29 16:51:06 +08:00
if (this.delayTimeout) {
clearTimeout(this.delayTimeout)
}
2017-10-26 15:18:08 +08:00
},
}
</script>