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

125 lines
3.0 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
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';
2019-09-20 11:19:59 +00:00
import { ConfigConsumerProps } from '../config-provider';
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],
2019-02-01 09:23:00 +00:00
model: {
prop: 'visible',
event: 'close.visible',
},
2017-11-09 07:58:53 +00:00
props: {
2019-09-20 11:19:59 +00:00
prefixCls: PropTypes.string,
2018-09-16 13:36:22 +00:00
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
},
2019-09-20 11:19:59 +00:00
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
2019-01-12 03:33:27 +00:00
data() {
let _visible = true;
2019-01-08 05:17:46 +00:00
if (hasProp(this, 'visible')) {
2019-01-12 03:33:27 +00:00
_visible = this.visible;
2018-09-16 13:36:22 +00:00
}
2019-01-08 05:17:46 +00:00
return {
_visible,
2019-01-12 03:33:27 +00:00
};
2017-11-09 07:58:53 +00:00
},
2018-09-16 13:36:22 +00:00
watch: {
2019-01-12 03:33:27 +00:00
visible(val) {
2018-09-16 13:36:22 +00:00
this.setState({
_visible: val,
2019-01-12 03:33:27 +00:00
});
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-12 03:33:27 +00:00
setVisible(visible, e) {
this.$emit('close', e);
this.$emit('close.visible', false);
2019-01-08 05:17:46 +00:00
if (e.defaultPrevented) {
2019-01-12 03:33:27 +00:00
return;
2018-09-16 13:36:22 +00:00
}
2019-01-08 05:17:46 +00:00
if (!hasProp(this, 'visible')) {
2019-01-12 03:33:27 +00:00
this.setState({ _visible: visible });
2017-11-09 07:58:53 +00:00
}
2018-09-16 13:36:22 +00:00
},
2019-01-12 03:33:27 +00:00
handleIconClick(e) {
this.setVisible(false, e);
2018-09-16 13:36:22 +00:00
},
2019-01-12 03:33:27 +00:00
animationEnd() {
const afterClose = this.afterClose;
2019-01-08 05:17:46 +00:00
if (afterClose) {
2019-01-12 03:33:27 +00:00
afterClose();
2018-09-16 13:36:22 +00:00
}
},
2019-01-12 03:33:27 +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-12 03:33:27 +00:00
getTagStyle() {
const { color } = this.$props;
const isPresetColor = this.isPresetColor(color);
2019-01-08 05:17:46 +00:00
return {
backgroundColor: color && !isPresetColor ? color : undefined,
2019-01-12 03:33:27 +00:00
};
2019-01-08 05:17:46 +00:00
},
2019-09-20 11:19:59 +00:00
getTagClassName(prefixCls) {
const { color } = this.$props;
2019-01-12 03:33:27 +00:00
const isPresetColor = this.isPresetColor(color);
2019-01-08 05:17:46 +00:00
return {
[prefixCls]: true,
[`${prefixCls}-${color}`]: isPresetColor,
[`${prefixCls}-has-color`]: color && !isPresetColor,
2019-01-12 03:33:27 +00:00
};
2019-01-08 05:17:46 +00:00
},
2019-01-12 03:33:27 +00:00
renderCloseIcon() {
const { closable } = this.$props;
return closable ? <Icon type="close" onClick={this.handleIconClick} /> : null;
2019-01-08 05:17:46 +00:00
},
2017-11-09 07:58:53 +00:00
},
2018-09-16 13:36:22 +00:00
2019-01-12 03:33:27 +00:00
render() {
2019-09-20 11:19:59 +00:00
const { prefixCls: customizePrefixCls } = this.$props;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('tag', customizePrefixCls);
2019-01-12 03:33:27 +00:00
const { _visible: visible } = this.$data;
2019-01-08 05:17:46 +00:00
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-09-20 11:19:59 +00:00
class={this.getTagClassName(prefixCls)}
2019-01-08 05:17:46 +00:00
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-12 03:33:27 +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,
2019-01-12 03:33:27 +00:00
});
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>
2019-01-12 03:33:27 +00:00
);
2018-02-05 11:12:41 +00:00
},
2019-01-12 03:33:27 +00:00
};