2020-06-12 13:50:59 +08:00
|
|
|
import { inject } from 'vue';
|
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 Wave from '../_util/wave';
|
2020-06-12 12:54:21 +08:00
|
|
|
import { hasProp, 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
|
|
|
|
|
|
|
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],
|
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,
|
2020-06-12 13:50:59 +08:00
|
|
|
onClose: PropTypes.func,
|
2018-09-16 21:36:22 +08:00
|
|
|
},
|
2020-06-12 12:54:21 +08:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
};
|
2019-09-20 19:19:59 +08:00
|
|
|
},
|
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
|
|
|
}
|
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);
|
2020-06-12 12:54:21 +08:00
|
|
|
this.$emit('update:visible', false);
|
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;
|
2020-06-12 13:50:59 +08:00
|
|
|
const isNeedWave = 'onClick' in this.$attrs;
|
2020-06-12 12:54:21 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-09-20 19:19:59 +08:00
|
|
|
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-06-12 12:54:21 +08:00
|
|
|
<span v-show={visible} class={this.getTagClassName(prefixCls)} style={this.getTagStyle()}>
|
2020-06-12 13:50:59 +08:00
|
|
|
{this.$slots.default && 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
|
|
|
);
|
2020-06-12 13:50:59 +08:00
|
|
|
return isNeedWave ? <Wave>{tag}</Wave> : tag;
|
2018-02-05 19:12:41 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|