ant-design-vue/components/tag/Tag.jsx

119 lines
2.8 KiB
Vue
Raw Normal View History

2018-09-16 13:36:22 +00:00
import PropTypes from '../_util/vue-types'
2017-11-09 07:58:53 +00:00
import Icon from '../icon'
2018-02-05 11:12:41 +00:00
import getTransitionProps from '../_util/getTransitionProps'
import omit from 'omit.js'
2018-09-16 13:36:22 +00:00
import Wave from '../_util/wave'
import { hasProp, getOptionProps } from '../_util/props-util'
import BaseMixin from '../_util/BaseMixin'
2017-11-09 07:58:53 +00:00
export default {
2018-04-08 13:17:20 +00:00
name: 'ATag',
2018-09-16 13:36:22 +00:00
mixins: [BaseMixin],
2017-11-09 07:58:53 +00:00
props: {
2018-09-16 13:36:22 +00:00
prefixCls: PropTypes.string.def('ant-tag'),
color: PropTypes.string,
2019-01-08 05:17:46 +00:00
closable: PropTypes.bool.def(false),
2018-09-16 13:36:22 +00:00
visible: PropTypes.bool,
2018-09-17 02:20:23 +00:00
afterClose: PropTypes.func,
2018-09-16 13:36:22 +00:00
},
model: {
prop: 'visible',
event: 'close.visible',
2017-11-09 07:58:53 +00:00
},
data () {
2019-01-08 05:17:46 +00:00
let _visible = true
if (hasProp(this, 'visible')) {
_visible = this.visible
2018-09-16 13:36:22 +00:00
}
2019-01-08 05:17:46 +00:00
return {
_visible,
2017-11-09 07:58:53 +00:00
}
},
2018-09-16 13:36:22 +00:00
watch: {
visible (val) {
this.setState({
_visible: val,
})
2017-11-09 07:58:53 +00:00
},
2018-09-16 13:36:22 +00:00
},
2017-11-09 07:58:53 +00:00
methods: {
2019-01-08 05:17:46 +00:00
setVisible (visible, e) {
2017-11-09 07:58:53 +00:00
this.$emit('close', e)
2018-09-16 13:36:22 +00:00
this.$emit('close.visible', false)
2019-01-08 05:17:46 +00:00
if (e.defaultPrevented) {
2018-09-16 13:36:22 +00:00
return
}
2019-01-08 05:17:46 +00:00
if (!hasProp(this, 'visible')) {
this.setState({ _visible: visible })
2017-11-09 07:58:53 +00:00
}
2018-09-16 13:36:22 +00:00
},
2019-01-08 05:17:46 +00:00
handleIconClick (e) {
this.setVisible(false, e)
2018-09-16 13:36:22 +00:00
},
2019-01-08 05:17:46 +00:00
animationEnd () {
const afterClose = this.afterClose
if (afterClose) {
afterClose()
2018-09-16 13:36:22 +00:00
}
},
isPresetColor (color) {
if (!color) { return false }
return (
/^(pink|red|yellow|orange|cyan|green|blue|purple|geekblue|magenta|volcano|gold|lime)(-inverse)?$/
.test(color)
)
2017-11-09 07:58:53 +00:00
},
2019-01-08 05:17:46 +00:00
getTagStyle () {
const { color } = this.$props
const isPresetColor = this.isPresetColor(color)
return {
backgroundColor: color && !isPresetColor ? color : undefined,
}
},
getTagClassName () {
const { prefixCls, color } = this.$props
const isPresetColor = this.isPresetColor(color)
return {
[prefixCls]: true,
[`${prefixCls}-${color}`]: isPresetColor,
[`${prefixCls}-has-color`]: color && !isPresetColor,
}
},
renderCloseIcon () {
const { closable } = this.$props
return closable ? <Icon type='close' onClick={this.handleIconClick} /> : null
},
2017-11-09 07:58:53 +00:00
},
2018-09-16 13:36:22 +00:00
2018-02-05 11:12:41 +00:00
render () {
2019-01-08 05:17:46 +00:00
const { prefixCls } = this.$props
const { _visible: visible } = this.$data
const tag = (
2018-09-16 13:36:22 +00:00
<div
2019-01-08 05:17:46 +00:00
v-show={visible}
2018-09-16 13:36:22 +00:00
{...{ on: omit(this.$listeners, ['close']) }}
2019-01-08 05:17:46 +00:00
class={this.getTagClassName()}
style={this.getTagStyle()}
2018-09-16 13:36:22 +00:00
>
{this.$slots.default}
2019-01-08 05:17:46 +00:00
{this.renderCloseIcon()}
2018-09-16 13:36:22 +00:00
</div>
2019-01-08 05:17:46 +00:00
)
2018-09-17 02:20:23 +00:00
const transitionProps = getTransitionProps(`${prefixCls}-zoom`, {
2019-01-08 05:17:46 +00:00
appear: false,
afterLeave: this.animationEnd,
2018-02-05 11:12:41 +00:00
})
return (
2018-09-16 13:36:22 +00:00
<Wave>
2019-01-08 05:17:46 +00:00
<transition {...transitionProps}>{tag}</transition>
2018-09-16 13:36:22 +00:00
</Wave>
2018-02-05 11:12:41 +00:00
)
},
2017-11-09 07:58:53 +00:00
}
2018-03-19 02:16:27 +00:00