Skip to content

Commit 09c4c77

Browse files
shiqiWang0beier
and
beier
authored
feat(tinytag): replace svg to render (#517)
* feat(tinytag): replace svg to render * test(tinytag): update tinyTag snapshots * feat(tinytag): remove default font-family && currentColor in svg * fix(tinytag): compute padding error && change height 15px * feat(tinytag): add useSvgWidth * feat(tinytag): update useSvgWidth hooks && change ref binding dom --------- Co-authored-by: beier <[email protected]>
1 parent 76c4034 commit 09c4c77

File tree

6 files changed

+108
-32
lines changed

6 files changed

+108
-32
lines changed

src/tinyTag/__tests__/__snapshots__/tinyTag.test.tsx.snap

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,36 @@ exports[`test StatusTag should match snapshot 1`] = `
55
<span
66
class="dtc-tinyTag dtc-test"
77
>
8-
完成
8+
<svg
9+
fill="none"
10+
height="15"
11+
viewBox="0 0 8 15"
12+
width="8"
13+
xmlns="http://www.w3.org/2000/svg"
14+
>
15+
<rect
16+
height="14"
17+
rx="1.5"
18+
stroke="currentColor"
19+
stroke-linejoin="bevel"
20+
width="7"
21+
x="0.5"
22+
y="0.5"
23+
/>
24+
<text
25+
fill="currentColor"
26+
font-size="10"
27+
letter-spacing="0px"
28+
xml:space="preserve"
29+
>
30+
<tspan
31+
x="4"
32+
y="11.1"
33+
>
34+
完成
35+
</tspan>
36+
</text>
37+
</svg>
938
</span>
1039
</DocumentFragment>
1140
`;

src/tinyTag/demos/basic.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import React from 'react';
22
import { Space } from 'antd';
33
import { TinyTag } from 'dt-react-component';
44

5+
import './style.scss';
6+
57
export default () => {
68
return (
79
<Space size={6}>
8-
<TinyTag value="袋鼠云" title="袋鼠云" />
9-
<TinyTag color="#d1d1d1" value="数据驱动" role="tag" />
10-
<TinyTag color="#d56161" value="UED" />
10+
<TinyTag value="袋鼠云" />
11+
<TinyTag className="data-tag" value="数据驱动" />
12+
<TinyTag className="ued-tag" value="UED" />
1113
</Space>
1214
);
1315
};

src/tinyTag/demos/style.scss

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.data-tag {
2+
color: #D1D1D1;
3+
&:hover {
4+
color: #D56161;
5+
}
6+
}
7+
8+
.ued-tag {
9+
color: #D56161;
10+
}

src/tinyTag/index.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ TinyTag 组件通常用于在某些文案后面,用于标识当前行数据的
1919

2020
### TinyTag
2121

22-
| 参数 | 说明 | 类型 | 默认值 |
23-
| --------- | ---------- | -------- | --------- |
24-
| value | 标签文案 | `string` | |
25-
| color | 颜色 | `string` | `#1D78FF` |
26-
| className | class 名称 | `string` | |
22+
| 参数 | 说明 | 类型 | 默认值 |
23+
| --------- | ---------- | -------- | ------ |
24+
| value | 标签文案 | `string` | |
25+
| className | class 名称 | `string` | |

src/tinyTag/index.tsx

+57-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
1-
import React from 'react';
1+
import React, { useCallback, useEffect, useState } from 'react';
22
import classNames from 'classnames';
33

44
import './style.scss';
55

6+
type UseSvgRef = (element: SVGTextElement) => void;
7+
type UseSvgWidthResult = [UseSvgRef, number, number];
8+
69
interface ITinyTag extends React.HTMLAttributes<HTMLSpanElement> {
7-
value?: string;
8-
color?: string;
10+
value: string;
911
}
1012

11-
export default function TinyTag({ color, value, className, ...restProps }: ITinyTag) {
13+
const useSvgWidth = (): UseSvgWidthResult => {
14+
const [element, ref] = useState<SVGTextElement | null>(null);
15+
const [svgWidth, setSvgWidth] = useState(0);
16+
const [rectWidth, setRectWidth] = useState(0);
17+
18+
const getWidth = useCallback(() => {
19+
if (!element) return;
20+
const paddingWidth = 8;
21+
const textWidth = Math.round(element.getBoundingClientRect()?.width);
22+
const svgWidth = textWidth + paddingWidth;
23+
setSvgWidth(svgWidth);
24+
setRectWidth(Math.max(svgWidth - 1, 0));
25+
}, [element]);
26+
27+
useEffect(() => {
28+
getWidth();
29+
}, [element]);
30+
31+
return [ref, svgWidth, rectWidth];
32+
};
33+
34+
export default function TinyTag({ value, className, ...restProps }: ITinyTag) {
35+
const [ref, svgWidth, rectWidth] = useSvgWidth();
1236
return (
13-
<span
14-
className={classNames('dtc-tinyTag', className)}
15-
style={{ borderColor: color, color }}
16-
{...restProps}
17-
>
18-
{value}
37+
<span className={classNames('dtc-tinyTag', className)} {...restProps}>
38+
<svg
39+
width={svgWidth}
40+
height="15"
41+
viewBox={`0 0 ${svgWidth} 15`}
42+
fill="none"
43+
xmlns="http://www.w3.org/2000/svg"
44+
>
45+
<rect
46+
x="0.5"
47+
y="0.5"
48+
width={rectWidth}
49+
height="14"
50+
rx="1.5"
51+
stroke="currentColor"
52+
strokeLinejoin="bevel"
53+
/>
54+
<text
55+
ref={ref}
56+
fill="currentColor"
57+
xmlSpace="preserve"
58+
fontSize="10"
59+
letterSpacing="0px"
60+
>
61+
<tspan x="4" y="11.1">
62+
{value}
63+
</tspan>
64+
</text>
65+
</svg>
1966
</span>
2067
);
2168
}

src/tinyTag/style.scss

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
$primaryColor: #1D78FF;
2-
$factor: 0.83;
3-
41
.dtc-tinyTag {
5-
border-radius: 2px;
6-
border: 1px solid $primaryColor;
7-
font-weight: 400;
8-
color: $primaryColor;
9-
line-height: calc(15px / $factor);
10-
padding: 0 calc(4px / $factor);
11-
font-size: 12px;
12-
display: inline-block;
13-
transform: scale($factor);
142
user-select: none;
15-
white-space: nowrap;
3+
display: inline-block;
4+
transform: translateY(3px);
165
}

0 commit comments

Comments
 (0)