Skip to content

Commit 02e134f

Browse files
committed
refactor: tag
1 parent 8c1979e commit 02e134f

File tree

4 files changed

+64
-40
lines changed

4 files changed

+64
-40
lines changed

components/tag/CheckableTag.tsx

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { inject, defineComponent, PropType } from 'vue';
1+
import { defineComponent, PropType, computed } from 'vue';
22
import classNames from '../_util/classNames';
3-
import { defaultConfigProvider } from '../config-provider';
43
import PropTypes from '../_util/vue-types';
4+
import useConfigInject from '../_util/hooks/useConfigInject';
55

66
const CheckableTag = defineComponent({
77
name: 'ACheckableTag',
@@ -17,24 +17,24 @@ const CheckableTag = defineComponent({
1717
},
1818
emits: ['update:checked', 'change', 'click'],
1919
setup(props, { slots, emit }) {
20-
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
20+
const { prefixCls } = useConfigInject('tag', props);
2121
const handleClick = (e: MouseEvent) => {
2222
const { checked } = props;
2323
emit('update:checked', !checked);
2424
emit('change', !checked);
2525
emit('click', e);
2626
};
2727

28-
return () => {
29-
const { checked, prefixCls: customizePrefixCls } = props;
30-
const prefixCls = getPrefixCls('tag', customizePrefixCls);
31-
const cls = classNames(prefixCls, {
32-
[`${prefixCls}-checkable`]: true,
33-
[`${prefixCls}-checkable-checked`]: checked,
34-
});
28+
const cls = computed(() =>
29+
classNames(prefixCls.value, {
30+
[`${prefixCls.value}-checkable`]: true,
31+
[`${prefixCls.value}-checkable-checked`]: props.checked,
32+
}),
33+
);
3534

35+
return () => {
3636
return (
37-
<span class={cls} onClick={handleClick}>
37+
<span class={cls.value} onClick={handleClick}>
3838
{slots.default?.()}
3939
</span>
4040
);

components/tag/index.tsx

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
inject,
32
ref,
43
HTMLAttributes,
54
defineComponent,
@@ -8,6 +7,7 @@ import {
87
PropType,
98
ExtractPropTypes,
109
Plugin,
10+
computed,
1111
} from 'vue';
1212
import classNames from '../_util/classNames';
1313
import PropTypes from '../_util/vue-types';
@@ -20,8 +20,8 @@ import {
2020
PresetStatusColorType,
2121
} from '../_util/colors';
2222
import { LiteralUnion } from '../_util/type';
23-
import { defaultConfigProvider } from '../config-provider';
2423
import CheckableTag from './CheckableTag';
24+
import useConfigInject from '../_util/hooks/useConfigInject';
2525

2626
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
2727
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
@@ -46,8 +46,9 @@ const Tag = defineComponent({
4646
name: 'ATag',
4747
props: tagProps,
4848
emits: ['update:visible', 'close'],
49+
slots: ['closeIcon', 'icon'],
4950
setup(props: TagProps, { slots, emit, attrs }) {
50-
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
51+
const { prefixCls, direction } = useConfigInject('tag', props);
5152

5253
const visible = ref(true);
5354

@@ -70,26 +71,31 @@ const Tag = defineComponent({
7071
}
7172
};
7273

73-
const isPresetColor = (): boolean => {
74+
const isPresetColor = computed(() => {
7475
const { color } = props;
7576
if (!color) {
7677
return false;
7778
}
7879
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
79-
};
80+
});
81+
82+
const tagClassName = computed(() =>
83+
classNames(prefixCls.value, {
84+
[`${prefixCls.value}-${props.color}`]: isPresetColor.value,
85+
[`${prefixCls.value}-has-color`]: props.color && !isPresetColor.value,
86+
[`${prefixCls.value}-hidden`]: !visible.value,
87+
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
88+
}),
89+
);
8090

8191
return () => {
8292
const {
83-
prefixCls: customizePrefixCls,
8493
icon = slots.icon?.(),
8594
color,
8695
closeIcon = slots.closeIcon?.(),
8796
closable = false,
8897
} = props;
8998

90-
const presetColor = isPresetColor();
91-
const prefixCls = getPrefixCls('tag', customizePrefixCls);
92-
9399
const renderCloseIcon = () => {
94100
if (closable) {
95101
return closeIcon ? (
@@ -104,15 +110,9 @@ const Tag = defineComponent({
104110
};
105111

106112
const tagStyle = {
107-
backgroundColor: color && !isPresetColor() ? color : undefined,
113+
backgroundColor: color && !isPresetColor.value ? color : undefined,
108114
};
109115

110-
const tagClassName = classNames(prefixCls, {
111-
[`${prefixCls}-${color}`]: presetColor,
112-
[`${prefixCls}-has-color`]: color && !presetColor,
113-
[`${prefixCls}-hidden`]: !visible.value,
114-
});
115-
116116
const iconNode = icon || null;
117117
const children = slots.default?.();
118118
const kids = iconNode ? (
@@ -127,7 +127,7 @@ const Tag = defineComponent({
127127
const isNeedWave = 'onClick' in attrs;
128128

129129
const tagNode = (
130-
<span class={tagClassName} style={tagStyle}>
130+
<span class={tagClassName.value} style={tagStyle}>
131131
{kids}
132132
{renderCloseIcon()}
133133
</span>

components/tag/style/index.less

+8-12
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@
1616
background: @tag-default-bg;
1717
border: @border-width-base @border-style-base @border-color-base;
1818
border-radius: @border-radius-base;
19-
cursor: default;
2019
opacity: 1;
21-
transition: all 0.3s @ease-in-out-circ;
22-
23-
&:hover {
24-
opacity: 0.85;
25-
}
20+
transition: all 0.3s;
2621

2722
&,
2823
a,
@@ -36,14 +31,12 @@
3631
padding: 0 8px;
3732
}
3833

39-
.@{iconfont-css-prefix}-close {
40-
.iconfont-size-under-12px(10px);
41-
34+
&-close-icon {
4235
margin-left: 3px;
4336
color: @text-color-secondary;
44-
font-weight: bold;
37+
font-size: 10px;
4538
cursor: pointer;
46-
transition: all 0.3s @ease-in-out-circ;
39+
transition: all 0.3s;
4740

4841
&:hover {
4942
color: @heading-color;
@@ -91,8 +84,9 @@
9184
@lightColor: '@{color}-1';
9285
@lightBorderColor: '@{color}-3';
9386
@darkColor: '@{color}-6';
87+
@textColor: '@{color}-7';
9488
&-@{color} {
95-
color: @@darkColor;
89+
color: @@textColor;
9690
background: @@lightColor;
9791
border-color: @@lightBorderColor;
9892
}
@@ -127,3 +121,5 @@
127121
margin-left: 7px;
128122
}
129123
}
124+
125+
@import './rtl';

components/tag/style/rtl.less

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@import '../../style/themes/index';
2+
@import '../../style/mixins/index';
3+
4+
@tag-prefix-cls: ~'@{ant-prefix}-tag';
5+
6+
.@{tag-prefix-cls} {
7+
&&-rtl {
8+
margin-right: 0;
9+
margin-left: 8px;
10+
direction: rtl;
11+
text-align: right;
12+
}
13+
14+
&-close-icon {
15+
.@{tag-prefix-cls}-rtl & {
16+
margin-right: 3px;
17+
margin-left: 0;
18+
}
19+
}
20+
21+
> .@{iconfont-css-prefix} + span,
22+
> span + .@{iconfont-css-prefix} {
23+
.@{tag-prefix-cls}-rtl& {
24+
margin-right: 7px;
25+
margin-left: 0;
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)