Skip to content

Commit 981470f

Browse files
committed
chore: improve tag type
1 parent 2b6ef8e commit 981470f

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Tag visibility can be controlled by visible with hidden as initial value 1`] = `<span class="ant-tag" style="display: none;"><!----><!----></span>`;
3+
exports[`Tag visibility can be controlled by visible with hidden as initial value 1`] = `<span class="ant-tag ant-tag-hidden"><!----><!----></span>`;
44

5-
exports[`Tag visibility can be controlled by visible with hidden as initial value 2`] = `<span class="ant-tag" style=""><!----><!----></span>`;
5+
exports[`Tag visibility can be controlled by visible with hidden as initial value 2`] = `<span class="ant-tag"><!----><!----></span>`;
66

7-
exports[`Tag visibility can be controlled by visible with hidden as initial value 3`] = `<span class="ant-tag" style="display: none;"><!----><!----></span>`;
7+
exports[`Tag visibility can be controlled by visible with hidden as initial value 3`] = `<span class="ant-tag ant-tag-hidden"><!----><!----></span>`;
88

99
exports[`Tag visibility can be controlled by visible with visible as initial value 1`] = `<span class="ant-tag"><!----><!----></span>`;
1010

11-
exports[`Tag visibility can be controlled by visible with visible as initial value 2`] = `<span class="ant-tag" style="display: none;"><!----><!----></span>`;
11+
exports[`Tag visibility can be controlled by visible with visible as initial value 2`] = `<span class="ant-tag ant-tag-hidden"><!----><!----></span>`;
1212

13-
exports[`Tag visibility can be controlled by visible with visible as initial value 3`] = `<span class="ant-tag" style=""><!----><!----></span>`;
13+
exports[`Tag visibility can be controlled by visible with visible as initial value 3`] = `<span class="ant-tag"><!----><!----></span>`;

components/tag/__tests__/index.test.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,17 @@ describe('Tag', () => {
1919
await asyncExpect(() => {
2020
expect(wrapper.findAll('.anticon-close').length).toBe(1);
2121
expect(
22-
wrapper.findAll('.ant-tag').filter(w => {
23-
const style = window.getComputedStyle(w.element, null);
24-
return style.display !== 'none';
25-
}).length,
26-
).toBe(1);
22+
wrapper.findAll('.ant-tag').filter(w => w.element.classList.contains('ant-tag-hidden'))
23+
.length,
24+
).toBe(0);
2725
wrapper.find('.anticon-close').trigger('click');
2826
expect(onClose).toBeCalled();
2927
});
3028
await asyncExpect(() => {
3129
expect(
32-
wrapper.findAll('.ant-tag').filter(w => {
33-
const style = window.getComputedStyle(w.element, null);
34-
return style.display !== 'none';
35-
}).length,
36-
).toBe(0);
30+
wrapper.findAll('.ant-tag').filter(w => w.element.classList.contains('ant-tag-hidden'))
31+
.length,
32+
).toBe(1);
3733
});
3834
});
3935

@@ -52,11 +48,9 @@ describe('Tag', () => {
5248
await asyncExpect(() => {
5349
expect(wrapper.findAll('.anticon-close').length).toBe(1);
5450
expect(
55-
wrapper.findAll('.ant-tag').filter(w => {
56-
const style = window.getComputedStyle(w.element, null);
57-
return style.display !== 'none';
58-
}).length,
59-
).toBe(1);
51+
wrapper.findAll('.ant-tag').filter(w => w.element.classList.contains('ant-tag-hidden'))
52+
.length,
53+
).toBe(0);
6054
wrapper.find('.anticon-close').trigger('click');
6155
});
6256
// await asyncExpect(() => {

components/tag/index.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { inject, ref, HTMLAttributes, defineComponent, App, VNodeTypes, watchEffect } from 'vue';
1+
import {
2+
inject,
3+
ref,
4+
HTMLAttributes,
5+
defineComponent,
6+
App,
7+
watchEffect,
8+
PropType,
9+
ExtractPropTypes,
10+
} from 'vue';
211
import classNames from '../_util/classNames';
312
import PropTypes from '../_util/vue-types';
413
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
@@ -16,15 +25,21 @@ import CheckableTag from './CheckableTag';
1625
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
1726
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
1827

19-
export interface TagProps extends HTMLAttributes {
20-
prefixCls?: string;
21-
color?: LiteralUnion<PresetColorType | PresetStatusColorType, string>;
22-
closable?: boolean;
23-
closeIcon?: VNodeTypes;
24-
visible?: boolean;
25-
onClose?: (e: MouseEvent) => void;
26-
icon?: VNodeTypes;
27-
}
28+
const tagProps = {
29+
prefixCls: PropTypes.string,
30+
color: {
31+
type: String as PropType<LiteralUnion<PresetColorType | PresetStatusColorType, string>>,
32+
},
33+
closable: PropTypes.looseBool.def(false),
34+
closeIcon: PropTypes.VNodeChild,
35+
visible: PropTypes.looseBool,
36+
onClose: {
37+
type: Function as PropType<(e: MouseEvent) => void>,
38+
},
39+
icon: PropTypes.VNodeChild,
40+
};
41+
42+
export type TagProps = HTMLAttributes & Partial<ExtractPropTypes<typeof tagProps>>;
2843

2944
const Tag = defineComponent({
3045
name: 'ATag',
@@ -110,7 +125,7 @@ const Tag = defineComponent({
110125
const isNeedWave = 'onClick' in attrs;
111126

112127
const tagNode = (
113-
<span v-show={visible.value} class={tagClassName} style={tagStyle}>
128+
<span class={tagClassName} style={tagStyle}>
114129
{kids}
115130
{renderCloseIcon()}
116131
</span>
@@ -121,22 +136,16 @@ const Tag = defineComponent({
121136
},
122137
});
123138

124-
Tag.props = {
125-
prefixCls: PropTypes.string,
126-
color: PropTypes.string,
127-
closable: PropTypes.looseBool.def(false),
128-
closeIcon: PropTypes.any,
129-
visible: PropTypes.looseBool,
130-
onClose: PropTypes.func,
131-
icon: PropTypes.any,
132-
};
139+
Tag.props = tagProps;
133140

134141
Tag.CheckableTag = CheckableTag;
135142

136-
Tag.install = (app: App) => {
143+
Tag.install = function(app: App) {
137144
app.component(Tag.name, Tag);
138145
app.component(CheckableTag.name, CheckableTag);
139146
return app;
140147
};
141148

142-
export default Tag;
149+
export default Tag as typeof Tag & {
150+
readonly CheckableTag: typeof CheckableTag;
151+
};

0 commit comments

Comments
 (0)