Skip to content

Commit 1815480

Browse files
lj1990111kagol
authored andcommitted
feat(tag): add tag size
1 parent 4b332eb commit 1815480

File tree

5 files changed

+118
-22
lines changed

5 files changed

+118
-22
lines changed

packages/devui-vue/devui/tag/__tests__/tag.spec.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
import { mount } from '@vue/test-utils';
22
import { Tag } from '../index';
33
import { ref } from 'vue';
4+
import { useNamespace } from '../../shared/hooks/use-namespace';
5+
6+
const ns = useNamespace('tag', true);
7+
const baseClass = ns.b();
8+
const itemClass = ns.e('item');
9+
10+
const nsNoDot = useNamespace('tag', false);
11+
const primaryClass = nsNoDot.m('primary');
12+
const lgClass = nsNoDot.m('lg');
413

514
describe('tag test', () => {
615
it('init render', async () => {
716
const wrapper = mount(Tag);
8-
expect(wrapper.find('.devui-tag').exists()).toBeTruthy();
17+
expect(wrapper.find(baseClass).exists()).toBeTruthy();
918
});
1019
it('props type', () => {
1120
const wrapper = mount(Tag, {
1221
propsData: {
1322
type: 'primary',
1423
},
1524
});
16-
expect(wrapper.find('.devui-tag .devui-tag-item').classes()).toContain('devui-tag-primary');
25+
const itemTag = wrapper.find(itemClass);
26+
expect(itemTag.classes()).toContain(primaryClass);
1727
});
1828
it('props color', () => {
1929
const wrapper = mount(Tag, {
2030
propsData: {
2131
color: 'red-w98', // #f66f6a rgb(246, 111, 106)
2232
},
2333
});
24-
expect(wrapper.find('.devui-tag .devui-tag-item').attributes('style')).toContain('rgb(246, 111, 106)');
34+
expect(wrapper.find(itemClass).attributes('style')).toContain('rgb(246, 111, 106)');
2535
});
2636
it('props color custom', () => {
2737
const wrapper = mount(Tag, {
2838
propsData: {
2939
color: '#aa2116', // rgb(170, 33, 22)
3040
},
3141
});
32-
expect(wrapper.find('.devui-tag .devui-tag-item').attributes('style')).toContain('rgb(170, 33, 22)');
42+
expect(wrapper.find(itemClass).attributes('style')).toContain('rgb(170, 33, 22)');
3343
});
3444
it('props titleContent', () => {
3545
const titleContent = 'tagTitle test';
3646
const wrapper = mount(Tag, {
3747
props: { titleContent },
3848
});
39-
expect(wrapper.get('.devui-tag .devui-tag-item').attributes('title')).toBe(titleContent);
49+
expect(wrapper.get(itemClass).attributes('title')).toBe(titleContent);
4050
});
4151
it('props deletable show', async () => {
4252
const wrapper = mount(Tag, {
@@ -77,9 +87,9 @@ describe('tag test', () => {
7787
color: 'red-w98', // 对应颜色:rgb(246, 111, 106)
7888
},
7989
});
80-
expect(wrapper.find('.devui-tag .devui-tag-item').attributes('style')).toContain('color: rgb(246, 111, 106);');
90+
expect(wrapper.find(itemClass).attributes('style')).toContain('color: rgb(246, 111, 106);');
8191
await wrapper.setProps({ checked: true });
82-
expect(wrapper.find('.devui-tag .devui-tag-item').attributes('style')).toContain('background-color: rgb(246, 111, 106);');
92+
expect(wrapper.find(itemClass).attributes('style')).toContain('background-color: rgb(246, 111, 106);');
8393
expect(wrapper.emitted('checkedChange').length).toBeGreaterThan(0);
8494
});
8595
it('event checkedChange', async () => {
@@ -113,4 +123,14 @@ describe('tag test', () => {
113123
});
114124
expect(wrapper.find('i').classes()).toContain('icon-like');
115125
});
126+
127+
it('Tag size work', () => {
128+
const wrapper = mount(Tag, {
129+
propsData: {
130+
size: 'lg',
131+
},
132+
});
133+
const itemTag = wrapper.find(itemClass);
134+
expect(itemTag.classes()).toContain(lgClass);
135+
});
116136
});

packages/devui-vue/devui/tag/src/composables/use-class.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export default function (props: TagProps): ComputedRef<string> {
77
const ns = useNamespace('tag');
88
return computed(() => {
99
const { type, color, deletable } = props;
10-
return `${ns.e('item')} ${ns.m(type || (color ? 'colorful' : '') || 'default')} ${deletable ? ns.m('deletable') : ''}`;
10+
return `${ns.e('item')} ${ns.m(type || (color ? 'colorful' : '') || 'default')} ${deletable ? ns.m('deletable') : ''} ${ns.m(
11+
props.size
12+
)}`;
1113
});
1214
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
import type { PropType, ExtractPropTypes } from 'vue';
22

33
export type TagType = 'primary' | 'success' | 'warning' | 'danger';
4+
export type SizeType = 'lg' | 'md' | 'sm' | 'xs';
45

56
export const tagProps = {
67
type: {
78
type: String as PropType<TagType>,
8-
default: ''
9+
default: '',
910
},
1011
color: {
1112
type: String as PropType<string>,
12-
default: ''
13+
default: '',
1314
},
1415
titleContent: {
1516
type: String as PropType<string>,
16-
default: ''
17+
default: '',
1718
},
1819
checked: {
1920
type: Boolean as PropType<boolean>,
20-
default: false
21+
default: false,
2122
},
2223
deletable: {
2324
type: Boolean as PropType<boolean>,
24-
default: false
25-
}
25+
default: false,
26+
},
27+
size: {
28+
type: String as PropType<SizeType>,
29+
default: 'xs',
30+
},
2631
} as const;
2732

2833
export type TagProps = ExtractPropTypes<typeof tagProps>;

packages/devui-vue/devui/tag/src/tag.scss

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
@import '../../styles-var/devui-var.scss';
22

3+
$font-size-map: (
4+
lg: $devui-font-size-lg,
5+
md: $devui-font-size-lg,
6+
sm: $devui-font-size-sm,
7+
xs: $devui-font-size-sm,
8+
);
9+
10+
$tag-height-map: (
11+
lg: 32px,
12+
md: 28px,
13+
sm: 24px,
14+
xs: 20px,
15+
);
16+
17+
$tag-line-height-map: (
18+
lg: 30px,
19+
md: 26px,
20+
sm: 22px,
21+
xs: 18px,
22+
);
23+
324
$devui-tag-normal-config: (
425
default: (
526
border: 0,
@@ -38,7 +59,7 @@ $devui-tag-normal-config: (
3859
display: block;
3960
position: relative;
4061
padding: 0 8px;
41-
min-height: 20px;
62+
height: 20px;
4263
border: 1px solid;
4364
border-radius: $devui-border-radius;
4465
font-size: $devui-font-size;
@@ -51,6 +72,13 @@ $devui-tag-normal-config: (
5172
}
5273
}
5374
}
75+
@each $size in ('lg', 'md', 'sm', 'xs') {
76+
&.#{$devui-prefix}-tag--#{$size} {
77+
font-size: map-get($font-size-map, #{$size});
78+
height: map-get($tag-height-map, #{$size});
79+
line-height: map-get($tag-line-height-map, #{$size});
80+
}
81+
}
5482
}
5583

5684
.remove-button {

packages/devui-vue/docs/components/tag/index.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,50 @@ export default defineComponent({
142142

143143
:::
144144

145+
### 不同尺寸
146+
147+
:::demo Tag 默认的是 xs 尺寸,还有 lg、md、sm 三种尺寸可选
148+
149+
```vue
150+
<template>
151+
<div>
152+
<d-tag type="primary" size="lg">标签二</d-tag>
153+
<d-tag type="success" size="md">标签三</d-tag>
154+
<d-tag type="warning" size="sm">标签四</d-tag>
155+
<d-tag type="danger">标签五</d-tag>
156+
</div>
157+
<div>
158+
<d-tag type="primary" deletable size="lg">标签一</d-tag>
159+
<d-tag type="primary" deletable size="md">标签二</d-tag>
160+
<d-tag type="primary" deletable size="sm">标签三</d-tag>
161+
<d-tag type="primary" deletable size="xs">标签四</d-tag>
162+
</div>
163+
</template>
164+
<script>
165+
import { defineComponent } from 'vue';
166+
167+
export default defineComponent({
168+
setup() {
169+
return {};
170+
},
171+
});
172+
</script>
173+
174+
<style></style>
175+
```
176+
177+
:::
178+
145179
### Tag 参数
146180

147-
| 参数 | 类型 | 默认值 | 说明 | 跳转至 Demo |
148-
| :------------ | :------------------ | :-------- | :------------------------------------------ | :-------------------- |
149-
| type | [TagType](#tagtype) | 'defalut' | 可选,标签的类型,指定类型后则 color 不生效 | [基本用法](#基本用法) |
150-
| color | `string` | '' | 可选,标签的主题色 | [基本用法](#基本用法) |
151-
| title-content | `string` | '' | 可选,设置鼠标悬浮时 title 的显示内容 | [基本用法](#基本用法) |
152-
| checked | `boolean` | false | 可选,标签选中的初始状态 | [可被选中](#可被选中) |
153-
| deletable | `boolean` | false | 可选,设置标签是否可删除 | [可移除的](#可移除的) |
181+
| 参数 | 类型 | 默认值 | 说明 | 跳转至 Demo |
182+
| :------------ | :-------------------- | :-------- | :------------------------------------------ | :-------------------- |
183+
| type | [TagType](#tagtype) | 'defalut' | 可选,标签的类型,指定类型后则 color 不生效 | [基本用法](#基本用法) |
184+
| color | `string` | '' | 可选,标签的主题色 | [基本用法](#基本用法) |
185+
| title-content | `string` | '' | 可选,设置鼠标悬浮时 title 的显示内容 | [基本用法](#基本用法) |
186+
| checked | `boolean` | false | 可选,标签选中的初始状态 | [可被选中](#可被选中) |
187+
| deletable | `boolean` | false | 可选,设置标签是否可删除 | [可移除的](#可移除的) |
188+
| size | [SizeType](#sizetype) | xs | 可选,标签尺寸 | [不同尺寸](#不同尺寸) |
154189

155190
### Tag 事件
156191

@@ -167,3 +202,9 @@ export default defineComponent({
167202
```ts
168203
type TagType = 'primary' | 'success' | 'warning' | 'danger';
169204
```
205+
206+
#### SizeType
207+
208+
```ts
209+
type SizeType = 'lg' | 'md' | 'sm' | 'xs';
210+
```

0 commit comments

Comments
 (0)