forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTag.jsx
138 lines (131 loc) · 3.39 KB
/
Tag.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Transition, inject } from 'vue';
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
import PropTypes from '../_util/vue-types';
import getTransitionProps from '../_util/getTransitionProps';
import Wave from '../_util/wave';
import { hasProp, getOptionProps } from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
import { ConfigConsumerProps } from '../config-provider';
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)?$`);
export default {
name: 'ATag',
mixins: [BaseMixin],
props: {
prefixCls: PropTypes.string,
color: PropTypes.string,
closable: PropTypes.bool.def(false),
visible: PropTypes.bool,
afterClose: PropTypes.func,
},
setup() {
return {
configProvider: inject('configProvider', ConfigConsumerProps),
};
},
data() {
let _visible = true;
const props = getOptionProps(this);
if ('visible' in props) {
_visible = this.visible;
}
warning(
!('afterClose' in props),
'Tag',
"'afterClose' will be deprecated, please use 'close' event, we will remove this in the next version.",
);
return {
_visible,
};
},
watch: {
visible(val) {
this.setState({
_visible: val,
});
},
},
methods: {
setVisible(visible, e) {
this.$emit('close', e);
this.$emit('update:visible', false);
const afterClose = this.afterClose;
if (afterClose) {
// next version remove.
afterClose();
}
if (e.defaultPrevented) {
return;
}
if (!hasProp(this, 'visible')) {
this.setState({ _visible: visible });
}
},
handleIconClick(e) {
e.stopPropagation();
this.setVisible(false, e);
},
isPresetColor() {
const { color } = this.$props;
if (!color) {
return false;
}
return PresetColorRegex.test(color);
},
getTagStyle() {
const { color } = this.$props;
const isPresetColor = this.isPresetColor();
return {
backgroundColor: color && !isPresetColor ? color : undefined,
};
},
getTagClassName(prefixCls) {
const { color } = this.$props;
const isPresetColor = this.isPresetColor();
return {
[prefixCls]: true,
[`${prefixCls}-${color}`]: isPresetColor,
[`${prefixCls}-has-color`]: color && !isPresetColor,
};
},
renderCloseIcon() {
const { closable } = this.$props;
return closable ? <CloseOutlined onClick={this.handleIconClick} /> : null;
},
},
render() {
const { prefixCls: customizePrefixCls } = this.$props;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('tag', customizePrefixCls);
const { _visible: visible } = this.$data;
const tag = (
<span v-show={visible} class={this.getTagClassName(prefixCls)} style={this.getTagStyle()}>
{this.$slots.default()}
{this.renderCloseIcon()}
</span>
);
const transitionProps = getTransitionProps(`${prefixCls}-zoom`, {
appear: false,
});
return (
<Wave>
<Transition {...transitionProps}>{tag}</Transition>
</Wave>
);
},
};