forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
175 lines (152 loc) · 5.01 KB
/
index.tsx
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type { HTMLAttributes, App, PropType, ExtractPropTypes, Plugin, CSSProperties } from 'vue';
import { shallowRef, defineComponent, watchEffect, computed } from 'vue';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
import Wave from '../_util/wave';
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
import { isPresetColor, isPresetStatusColor } from '../_util/colors';
import { eventType } from '../_util/type';
import type { CustomSlotsType, LiteralUnion } from '../_util/type';
import CheckableTag from './CheckableTag';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import warning from '../_util/warning';
import useStyle from './style';
export const tagProps = () => ({
prefixCls: String,
color: {
type: String as PropType<LiteralUnion<PresetColorType | PresetStatusColorType>>,
},
closable: { type: Boolean, default: false },
closeIcon: PropTypes.any,
/** @deprecated `visible` will be removed in next major version. */
visible: { type: Boolean, default: undefined },
onClose: {
type: Function as PropType<(e: MouseEvent) => void>,
},
onClick: eventType<(e: MouseEvent) => void>(),
'onUpdate:visible': Function as PropType<(vis: boolean) => void>,
icon: PropTypes.any,
bordered: { type: Boolean, default: true },
});
export type TagProps = HTMLAttributes & Partial<ExtractPropTypes<ReturnType<typeof tagProps>>>;
const Tag = defineComponent({
compatConfig: { MODE: 3 },
name: 'ATag',
inheritAttrs: false,
props: tagProps(),
// emits: ['update:visible', 'close'],
slots: Object as CustomSlotsType<{
closeIcon: any;
icon: any;
default: any;
}>,
setup(props, { slots, emit, attrs }) {
const { prefixCls, direction } = useConfigInject('tag', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const visible = shallowRef(true);
// Warning for deprecated usage
if (process.env.NODE_ENV !== 'production') {
warning(
props.visible === undefined,
'Tag',
'`visible` is deprecated, please use `<Tag v-show="visible" />` instead.',
);
}
watchEffect(() => {
if (props.visible !== undefined) {
visible.value = props.visible!;
}
});
const handleCloseClick = (e: MouseEvent) => {
e.stopPropagation();
emit('update:visible', false);
emit('close', e);
if (e.defaultPrevented) {
return;
}
if (props.visible === undefined) {
visible.value = false;
}
};
// const isPresetColor = computed(() => {
// const { color } = props;
// if (!color) {
// return false;
// }
// return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
// });
const isInternalColor = computed(
() => isPresetColor(props.color) || isPresetStatusColor(props.color),
);
const tagClassName = computed(() =>
classNames(prefixCls.value, hashId.value, {
[`${prefixCls.value}-${props.color}`]: isInternalColor.value,
[`${prefixCls.value}-has-color`]: props.color && !isInternalColor.value,
[`${prefixCls.value}-hidden`]: !visible.value,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[`${prefixCls.value}-borderless`]: !props.bordered,
}),
);
const handleClick = (e: MouseEvent) => {
emit('click', e);
};
return () => {
const {
icon = slots.icon?.(),
color,
closeIcon = slots.closeIcon?.(),
closable = false,
} = props;
const renderCloseIcon = () => {
if (closable) {
return closeIcon ? (
<span class={`${prefixCls.value}-close-icon`} onClick={handleCloseClick}>
{closeIcon}
</span>
) : (
<CloseOutlined class={`${prefixCls.value}-close-icon`} onClick={handleCloseClick} />
);
}
return null;
};
const tagStyle = {
backgroundColor: color && !isInternalColor.value ? color : undefined,
};
const iconNode = icon || null;
const children = slots.default?.();
const kids = iconNode ? (
<>
{iconNode}
<span>{children}</span>
</>
) : (
children
);
const isNeedWave = props.onClick !== undefined;
const tagNode = (
<span
{...attrs}
onClick={handleClick}
class={[tagClassName.value, attrs.class]}
style={[tagStyle, attrs.style as CSSProperties]}
>
{kids}
{renderCloseIcon()}
</span>
);
return wrapSSR(isNeedWave ? <Wave>{tagNode}</Wave> : tagNode);
};
},
});
Tag.CheckableTag = CheckableTag;
Tag.install = function (app: App) {
app.component(Tag.name, Tag);
app.component(CheckableTag.name, CheckableTag);
return app;
};
export { CheckableTag };
export default Tag as typeof Tag &
Plugin & {
readonly CheckableTag: typeof CheckableTag;
};