Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tinytag): replace svg to render #517

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/tinyTag/__tests__/__snapshots__/tinyTag.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@ exports[`test StatusTag should match snapshot 1`] = `
<span
class="dtc-tinyTag dtc-test"
>
完成
<svg
fill="none"
height="15"
viewBox="0 0 8 15"
width="8"
xmlns="http://www.w3.org/2000/svg"
>
<rect
height="14"
rx="1.5"
stroke="currentColor"
stroke-linejoin="bevel"
width="7"
x="0.5"
y="0.5"
/>
<text
fill="currentColor"
font-size="10"
letter-spacing="0px"
xml:space="preserve"
>
<tspan
x="4"
y="11.1"
>
完成
</tspan>
</text>
</svg>
</span>
</DocumentFragment>
`;
8 changes: 5 additions & 3 deletions src/tinyTag/demos/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React from 'react';
import { Space } from 'antd';
import { TinyTag } from 'dt-react-component';

import './style.scss';

export default () => {
return (
<Space size={6}>
<TinyTag value="袋鼠云" title="袋鼠云" />
<TinyTag color="#d1d1d1" value="数据驱动" role="tag" />
<TinyTag color="#d56161" value="UED" />
<TinyTag value="袋鼠云" />
<TinyTag className="data-tag" value="数据驱动" />
<TinyTag className="ued-tag" value="UED" />
</Space>
);
};
10 changes: 10 additions & 0 deletions src/tinyTag/demos/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.data-tag {
color: #D1D1D1;
&:hover {
color: #D56161;
}
}

.ued-tag {
color: #D56161;
}
9 changes: 4 additions & 5 deletions src/tinyTag/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ TinyTag 组件通常用于在某些文案后面,用于标识当前行数据的

### TinyTag

| 参数 | 说明 | 类型 | 默认值 |
| --------- | ---------- | -------- | --------- |
| value | 标签文案 | `string` | |
| color | 颜色 | `string` | `#1D78FF` |
| className | class 名称 | `string` | |
| 参数 | 说明 | 类型 | 默认值 |
| --------- | ---------- | -------- | ------ |
| value | 标签文案 | `string` | |
| className | class 名称 | `string` | |
50 changes: 40 additions & 10 deletions src/tinyTag/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
import React from 'react';
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
LuckyFBB marked this conversation as resolved.
Show resolved Hide resolved

import './style.scss';

interface ITinyTag extends React.HTMLAttributes<HTMLSpanElement> {
value?: string;
color?: string;
value: string;
}

export default function TinyTag({ color, value, className, ...restProps }: ITinyTag) {
export default function TinyTag({ value, className, ...restProps }: ITinyTag) {
const domRef = useRef<HTMLDivElement>(null);
const [width, setWidth] = useState(0);
shiqiWang0 marked this conversation as resolved.
Show resolved Hide resolved

const getTextWidth = () => {
const text = domRef?.current?.getElementsByTagName('text')?.[0];
const paddingWidth = 8;
if (!text) return;
const textWidth = Math.round(text.getBoundingClientRect()?.width);
const widthSvg = textWidth + paddingWidth;
setWidth(widthSvg);
};
useEffect(() => {
getTextWidth();
}, []);
return (
<span
className={classNames('dtc-tinyTag', className)}
style={{ borderColor: color, color }}
{...restProps}
>
{value}
<span ref={domRef} className={classNames('dtc-tinyTag', className)} {...restProps}>
<svg
width={width}
height="15"
viewBox={`0 0 ${width} 15`}
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<rect
x="0.5"
y="0.5"
width={width - 1}
height="14"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里加个判断吧?width初始值为0,在第一次渲染时,rect的width变成了 -1,会出现错误
Error: attribute width: A negative value is not valid. ("-1")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 会提取到 hook 里面, 处理掉

rx="1.5"
stroke="currentColor"
strokeLinejoin="bevel"
/>
<text fill="currentColor" xmlSpace="preserve" fontSize="10" letterSpacing="0px">
<tspan x="4" y="11.1">
{value}
</tspan>
</text>
</svg>
</span>
);
}
15 changes: 2 additions & 13 deletions src/tinyTag/style.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
$primaryColor: #1D78FF;
$factor: 0.83;

.dtc-tinyTag {
border-radius: 2px;
border: 1px solid $primaryColor;
font-weight: 400;
color: $primaryColor;
line-height: calc(15px / $factor);
padding: 0 calc(4px / $factor);
font-size: 12px;
display: inline-block;
transform: scale($factor);
user-select: none;
white-space: nowrap;
display: inline-block;
transform: translateY(3px);
}
Loading