2020-03-29 13:17:25 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import Wave from '../_util/wave';
|
2020-03-07 19:45:13 +08:00
|
|
|
import { hasProp, getListeners, getOptionProps } from '../_util/props-util';
|
2019-01-12 11:33:27 +08:00
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2019-09-20 19:19:59 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-03-07 19:45:13 +08:00
|
|
|
import warning from '../_util/warning';
|
|
|
|
|
|
|
|
const PresetColorTypes = [
|
|
|
|
'pink',
|
|
|
|
'red',
|
|
|
|
'yellow',
|
|
|
|
'orange',
|
|
|
|
'cyan',
|
|
|
|
'green',
|
|
|
|
'blue',
|
|
|
|
'purple',
|
|
|
|
'geekblue',
|
|
|
|
'magenta',
|
|
|
|
'volcano',
|
|
|
|
'gold',
|
|
|
|
'lime',
|
|
|
|
];
|
|
|
|
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
|
2017-11-09 15:58:53 +08:00
|
|
|
|
|
|
|
export default {
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ATag',
|
2018-09-16 21:36:22 +08:00
|
|
|
mixins: [BaseMixin],
|
2019-02-01 17:23:00 +08:00
|
|
|
model: {
|
|
|
|
prop: 'visible',
|
|
|
|
event: 'close.visible',
|
|
|
|
},
|
2017-11-09 15:58:53 +08:00
|
|
|
props: {
|
2019-09-20 19:19:59 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2018-09-16 21:36:22 +08:00
|
|
|
color: PropTypes.string,
|
2019-01-08 13:17:46 +08:00
|
|
|
closable: PropTypes.bool.def(false),
|
2018-09-16 21:36:22 +08:00
|
|
|
visible: PropTypes.bool,
|
2018-09-17 10:20:23 +08:00
|
|
|
afterClose: PropTypes.func,
|
2018-09-16 21:36:22 +08:00
|
|
|
},
|
2019-09-20 19:19:59 +08:00
|
|
|
inject: {
|
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
let _visible = true;
|
2020-03-07 19:45:13 +08:00
|
|
|
const props = getOptionProps(this);
|
|
|
|
if ('visible' in props) {
|
2019-01-12 11:33:27 +08:00
|
|
|
_visible = this.visible;
|
2018-09-16 21:36:22 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
warning(
|
|
|
|
!('afterClose' in props),
|
|
|
|
'Tag',
|
|
|
|
"'afterClose' will be deprecated, please use 'close' event, we will remove this in the next version.",
|
|
|
|
);
|
2019-01-08 13:17:46 +08:00
|
|
|
return {
|
|
|
|
_visible,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-11-09 15:58:53 +08:00
|
|
|
},
|
2018-09-16 21:36:22 +08:00
|
|
|
watch: {
|
2019-01-12 11:33:27 +08:00
|
|
|
visible(val) {
|
2018-09-16 21:36:22 +08:00
|
|
|
this.setState({
|
|
|
|
_visible: val,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2017-11-09 15:58:53 +08:00
|
|
|
},
|
2018-09-16 21:36:22 +08:00
|
|
|
},
|
2017-11-09 15:58:53 +08:00
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
setVisible(visible, e) {
|
|
|
|
this.$emit('close', e);
|
|
|
|
this.$emit('close.visible', false);
|
2020-03-07 19:45:13 +08:00
|
|
|
const afterClose = this.afterClose;
|
|
|
|
if (afterClose) {
|
|
|
|
// next version remove.
|
|
|
|
afterClose();
|
|
|
|
}
|
2019-01-08 13:17:46 +08:00
|
|
|
if (e.defaultPrevented) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-09-16 21:36:22 +08:00
|
|
|
}
|
2019-01-08 13:17:46 +08:00
|
|
|
if (!hasProp(this, 'visible')) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState({ _visible: visible });
|
2017-11-09 15:58:53 +08:00
|
|
|
}
|
2018-09-16 21:36:22 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleIconClick(e) {
|
2020-03-07 19:45:13 +08:00
|
|
|
e.stopPropagation();
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setVisible(false, e);
|
2018-09-16 21:36:22 +08:00
|
|
|
},
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
isPresetColor() {
|
|
|
|
const { color } = this.$props;
|
2019-01-12 11:33:27 +08:00
|
|
|
if (!color) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
return PresetColorRegex.test(color);
|
2017-11-09 15:58:53 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
getTagStyle() {
|
|
|
|
const { color } = this.$props;
|
2020-03-07 19:45:13 +08:00
|
|
|
const isPresetColor = this.isPresetColor();
|
2019-01-08 13:17:46 +08:00
|
|
|
return {
|
|
|
|
backgroundColor: color && !isPresetColor ? color : undefined,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2019-01-08 13:17:46 +08:00
|
|
|
},
|
|
|
|
|
2019-09-20 19:19:59 +08:00
|
|
|
getTagClassName(prefixCls) {
|
|
|
|
const { color } = this.$props;
|
2020-03-07 19:45:13 +08:00
|
|
|
const isPresetColor = this.isPresetColor();
|
2019-01-08 13:17:46 +08:00
|
|
|
return {
|
|
|
|
[prefixCls]: true,
|
|
|
|
[`${prefixCls}-${color}`]: isPresetColor,
|
|
|
|
[`${prefixCls}-has-color`]: color && !isPresetColor,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2019-01-08 13:17:46 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
renderCloseIcon() {
|
|
|
|
const { closable } = this.$props;
|
2020-03-29 13:17:25 +08:00
|
|
|
return closable ? <CloseOutlined onClick={this.handleIconClick} /> : null;
|
2019-01-08 13:17:46 +08:00
|
|
|
},
|
2017-11-09 15:58:53 +08:00
|
|
|
},
|
2018-09-16 21:36:22 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2019-09-20 19:19:59 +08:00
|
|
|
const { prefixCls: customizePrefixCls } = this.$props;
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
|
|
|
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
2019-01-12 11:33:27 +08:00
|
|
|
const { _visible: visible } = this.$data;
|
2019-01-08 13:17:46 +08:00
|
|
|
const tag = (
|
2020-03-07 19:45:13 +08:00
|
|
|
<span
|
2019-01-08 13:17:46 +08:00
|
|
|
v-show={visible}
|
2020-01-19 16:58:38 +08:00
|
|
|
{...{ on: omit(getListeners(this), ['close']) }}
|
2019-09-20 19:19:59 +08:00
|
|
|
class={this.getTagClassName(prefixCls)}
|
2019-01-08 13:17:46 +08:00
|
|
|
style={this.getTagStyle()}
|
2018-09-16 21:36:22 +08:00
|
|
|
>
|
|
|
|
{this.$slots.default}
|
2019-01-08 13:17:46 +08:00
|
|
|
{this.renderCloseIcon()}
|
2020-03-07 19:45:13 +08:00
|
|
|
</span>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-09-17 10:20:23 +08:00
|
|
|
const transitionProps = getTransitionProps(`${prefixCls}-zoom`, {
|
2019-01-08 13:17:46 +08:00
|
|
|
appear: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-02-05 19:12:41 +08:00
|
|
|
return (
|
2018-09-16 21:36:22 +08:00
|
|
|
<Wave>
|
2019-01-08 13:17:46 +08:00
|
|
|
<transition {...transitionProps}>{tag}</transition>
|
2018-09-16 21:36:22 +08:00
|
|
|
</Wave>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-02-05 19:12:41 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|