You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.7 KiB
106 lines
2.7 KiB
7 years ago
|
|
||
7 years ago
|
import Icon from '../icon'
|
||
7 years ago
|
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/
|
||
|
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar)
|
||
7 years ago
|
import buttonTypes from './buttonTypes'
|
||
7 years ago
|
const props = buttonTypes()
|
||
7 years ago
|
export default {
|
||
|
name: 'Button',
|
||
7 years ago
|
__ANT_BUTTON: true,
|
||
7 years ago
|
props: {
|
||
7 years ago
|
...props,
|
||
7 years ago
|
},
|
||
|
data () {
|
||
|
return {
|
||
|
sizeMap: {
|
||
|
large: 'lg',
|
||
|
small: 'sm',
|
||
|
},
|
||
|
clicked: false,
|
||
7 years ago
|
sLoading: !!this.loading,
|
||
7 years ago
|
}
|
||
|
},
|
||
7 years ago
|
watch: {
|
||
7 years ago
|
loading (val) {
|
||
|
clearTimeout(this.delayTimeout)
|
||
|
if (typeof val !== 'boolean' && val && val.delay) {
|
||
|
this.delayTimeout = setTimeout(() => { this.sLoading = !!val }, val.delay)
|
||
|
} else {
|
||
|
this.sLoading = !!val
|
||
|
}
|
||
7 years ago
|
},
|
||
|
},
|
||
7 years ago
|
computed: {
|
||
|
classes () {
|
||
7 years ago
|
const { prefixCls, type, shape, size, sLoading, ghost, clicked, sizeMap } = this
|
||
7 years ago
|
const sizeCls = sizeMap[size] || ''
|
||
|
return {
|
||
|
[`${prefixCls}`]: true,
|
||
|
[`${prefixCls}-${type}`]: type,
|
||
|
[`${prefixCls}-${shape}`]: shape,
|
||
|
[`${prefixCls}-${sizeCls}`]: sizeCls,
|
||
7 years ago
|
[`${prefixCls}-loading`]: sLoading,
|
||
7 years ago
|
[`${prefixCls}-clicked`]: clicked,
|
||
|
[`${prefixCls}-background-ghost`]: ghost || type === 'ghost',
|
||
|
}
|
||
|
},
|
||
|
iconType () {
|
||
7 years ago
|
const { sLoading, icon } = this
|
||
|
return sLoading ? 'loading' : icon
|
||
7 years ago
|
},
|
||
|
},
|
||
|
methods: {
|
||
|
handleClick (event) {
|
||
|
this.clicked = true
|
||
|
clearTimeout(this.timeout)
|
||
|
this.timeout = setTimeout(() => (this.clicked = false), 500)
|
||
|
this.$emit('click', event)
|
||
|
},
|
||
7 years ago
|
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
|
||
7 years ago
|
},
|
||
|
},
|
||
7 years ago
|
render () {
|
||
7 years ago
|
const { htmlType, classes, disabled, handleClick, iconType, $slots, $attrs, $listeners } = this
|
||
7 years ago
|
const buttonProps = {
|
||
|
props: {
|
||
|
},
|
||
|
attrs: {
|
||
|
...$attrs,
|
||
|
type: htmlType,
|
||
|
disabled,
|
||
|
},
|
||
|
class: classes,
|
||
|
on: {
|
||
7 years ago
|
...$listeners,
|
||
7 years ago
|
click: handleClick,
|
||
|
},
|
||
|
}
|
||
|
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>
|
||
|
)
|
||
|
},
|
||
7 years ago
|
beforeDestroy () {
|
||
|
if (this.timeout) {
|
||
|
clearTimeout(this.timeout)
|
||
|
}
|
||
7 years ago
|
if (this.delayTimeout) {
|
||
|
clearTimeout(this.delayTimeout)
|
||
|
}
|
||
7 years ago
|
},
|
||
|
}
|
||
7 years ago
|
|