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.
ant-design-vue/components/tag/Tag.jsx

119 lines
2.8 KiB

import PropTypes from '../_util/vue-types';
import Icon from '../icon';
import getTransitionProps from '../_util/getTransitionProps';
import omit from 'omit.js';
import Wave from '../_util/wave';
import { hasProp } from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
7 years ago
export default {
name: 'ATag',
mixins: [BaseMixin],
7 years ago
props: {
prefixCls: PropTypes.string.def('ant-tag'),
color: PropTypes.string,
6 years ago
closable: PropTypes.bool.def(false),
visible: PropTypes.bool,
afterClose: PropTypes.func,
},
model: {
prop: 'visible',
event: 'close.visible',
7 years ago
},
data() {
let _visible = true;
6 years ago
if (hasProp(this, 'visible')) {
_visible = this.visible;
}
6 years ago
return {
_visible,
};
7 years ago
},
watch: {
visible(val) {
this.setState({
_visible: val,
});
7 years ago
},
},
7 years ago
methods: {
setVisible(visible, e) {
this.$emit('close', e);
this.$emit('close.visible', false);
6 years ago
if (e.defaultPrevented) {
return;
}
6 years ago
if (!hasProp(this, 'visible')) {
this.setState({ _visible: visible });
7 years ago
}
},
handleIconClick(e) {
this.setVisible(false, e);
},
animationEnd() {
const afterClose = this.afterClose;
6 years ago
if (afterClose) {
afterClose();
}
},
isPresetColor(color) {
if (!color) {
return false;
}
return /^(pink|red|yellow|orange|cyan|green|blue|purple|geekblue|magenta|volcano|gold|lime)(-inverse)?$/.test(
color,
);
7 years ago
},
getTagStyle() {
const { color } = this.$props;
const isPresetColor = this.isPresetColor(color);
6 years ago
return {
backgroundColor: color && !isPresetColor ? color : undefined,
};
6 years ago
},
getTagClassName() {
const { prefixCls, color } = this.$props;
const isPresetColor = this.isPresetColor(color);
6 years ago
return {
[prefixCls]: true,
[`${prefixCls}-${color}`]: isPresetColor,
[`${prefixCls}-has-color`]: color && !isPresetColor,
};
6 years ago
},
renderCloseIcon() {
const { closable } = this.$props;
return closable ? <Icon type="close" onClick={this.handleIconClick} /> : null;
6 years ago
},
7 years ago
},
render() {
const { prefixCls } = this.$props;
const { _visible: visible } = this.$data;
6 years ago
const tag = (
<div
6 years ago
v-show={visible}
{...{ on: omit(this.$listeners, ['close']) }}
6 years ago
class={this.getTagClassName()}
style={this.getTagStyle()}
>
{this.$slots.default}
6 years ago
{this.renderCloseIcon()}
</div>
);
const transitionProps = getTransitionProps(`${prefixCls}-zoom`, {
6 years ago
appear: false,
afterLeave: this.animationEnd,
});
return (
<Wave>
6 years ago
<transition {...transitionProps}>{tag}</transition>
</Wave>
);
},
};