Skip to content

Commit d4fbe6b

Browse files
committed
feat: add a series of EllipsisTypography components
1 parent 401b62d commit d4fbe6b

File tree

8 files changed

+167
-3
lines changed

8 files changed

+167
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
# Changelog
44

5+
## 1.6.0
6+
7+
2026-1-1
8+
9+
### Features
10+
11+
- ✨ Add some `EllipsisTypography` components: `EllipsisTitle`, `EllipsisText`, and `EllipsisParagraph` with auto tooltip functionality.
12+
513
## 1.5.6
614

715
2025-12-8

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tiny-codes/react-easy",
3-
"version": "1.5.6",
3+
"version": "1.6.0",
44
"description": "Simplify React and AntDesign development with practical components and hooks",
55
"keywords": [
66
"react",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Typography } from 'antd';
2+
import type { ParagraphProps } from 'antd/es/typography/Paragraph';
3+
import withEllipsisTypography, { type MakeEllipsisTypographyProps } from './withEllipsisTypography';
4+
5+
export type EllipsisParagraphProps = MakeEllipsisTypographyProps<ParagraphProps>;
6+
7+
/**
8+
* - **EN:** A paragraph component with automatic ellipsis and tooltip functionality that displays a
9+
* tooltip when the text overflows, and does not display a tooltip when the text does not
10+
* overflow. The following three methods can enable the automatic tooltip feature:
11+
*
12+
* 1. Set the `ellipsis` property to `true`
13+
* 2. Set the `ellipsis.tooltip` property to `true`
14+
* 3. Set the `ellipsis.tooltip.title` property to `true`
15+
* - **CN:** 具有自动省略号和提示功能的段落组件,在文本溢出时显示工具提示,如果文本未溢出,则不显示工具提示。以下三种方式均可开启自动提示功能:
16+
*
17+
* 1. 设置 `ellipsis` 属性为 `true`
18+
* 2. 设置 `ellipsis.tooltip` 属性为 `true`
19+
* 3. 设置 `ellipsis.tooltip.title` 属性为 `true`
20+
*/
21+
const EllipsisParagraph = withEllipsisTypography(Typography.Paragraph);
22+
23+
export default EllipsisParagraph;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Typography } from 'antd';
2+
import type { TextProps } from 'antd/es/typography/Text';
3+
import withEllipsisTypography, { type MakeEllipsisTypographyProps } from './withEllipsisTypography';
4+
5+
export type EllipsisTextProps = MakeEllipsisTypographyProps<TextProps>;
6+
7+
/**
8+
* - **EN:** A text component with automatic ellipsis and tooltip functionality that displays a
9+
* tooltip when the text overflows, and does not display a tooltip when the text does not
10+
* overflow. The following three methods can enable the automatic tooltip feature:
11+
*
12+
* 1. Set the `ellipsis` property to `true`
13+
* 2. Set the `ellipsis.tooltip` property to `true`
14+
* 3. Set the `ellipsis.tooltip.title` property to `true`
15+
* - **CN:** 具有自动省略号和提示功能的文本组件,在文本溢出时显示工具提示,如果文本未溢出,则不显示工具提示。以下三种方式均可开启自动提示功能:
16+
*
17+
* 1. 设置 `ellipsis` 属性为 `true`
18+
* 2. 设置 `ellipsis.tooltip` 属性为 `true`
19+
* 3. 设置 `ellipsis.tooltip.title` 属性为 `true`
20+
*/
21+
const EllipsisText = withEllipsisTypography(Typography.Text);
22+
23+
export default EllipsisText;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Typography } from 'antd';
2+
import type { TitleProps } from 'antd/es/typography/Title';
3+
import withEllipsisTypography, { type MakeEllipsisTypographyProps } from './withEllipsisTypography';
4+
5+
export type EllipsisTitleProps = MakeEllipsisTypographyProps<TitleProps>;
6+
7+
/**
8+
* - **EN:** A title component with automatic ellipsis and tooltip functionality that displays a
9+
* tooltip when the text overflows, and does not display a tooltip when the text does not
10+
* overflow. The following three methods can enable the automatic tooltip feature:
11+
*
12+
* 1. Set the `ellipsis` property to `true`
13+
* 2. Set the `ellipsis.tooltip` property to `true`
14+
* 3. Set the `ellipsis.tooltip.title` property to `true`
15+
* - **CN:** 具有自动省略号和提示功能的标题组件,在文本溢出时显示工具提示,如果文本未溢出,则不显示工具提示。以下三种方式均可开启自动提示功能:
16+
*
17+
* 1. 设置 `ellipsis` 属性为 `true`
18+
* 2. 设置 `ellipsis.tooltip` 属性为 `true`
19+
* 3. 设置 `ellipsis.tooltip.title` 属性为 `true`
20+
*/
21+
const EllipsisTitle = withEllipsisTypography(Typography.Title);
22+
23+
export default EllipsisTitle;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { ComponentType } from 'react';
2+
import { useEffect, useMemo, useState } from 'react';
3+
import type { TooltipProps } from 'antd';
4+
import { Tooltip } from 'antd';
5+
import type { EllipsisConfig } from 'antd/es/typography/Base';
6+
import type { ParagraphProps } from 'antd/es/typography/Paragraph';
7+
import type { TextProps } from 'antd/es/typography/Text';
8+
import type { TitleProps } from 'antd/es/typography/Title';
9+
10+
function withEllipsisTypography<T extends TextProps | ParagraphProps | TitleProps>(
11+
Component: ComponentType<MakeEllipsisTypographyProps<T>>
12+
) {
13+
return function EllipsisText(props: MakeEllipsisTypographyProps<T>) {
14+
const { ellipsis, text, ...rest } = props;
15+
const [isEllipsis, setIsEllipsis] = useState(false);
16+
const [dom, setDom] = useState<HTMLElement | null>(null);
17+
const isAutoEllipsis = useMemo(() => ellipsis === true, [ellipsis]);
18+
const isAutoTooltip = useMemo(() => typeof ellipsis === 'object' && ellipsis.tooltip === true, [ellipsis]);
19+
const isAutoTooltipTitle = useMemo(
20+
() =>
21+
typeof ellipsis === 'object' &&
22+
ellipsis.tooltip &&
23+
typeof ellipsis.tooltip === 'object' &&
24+
'title' in ellipsis.tooltip &&
25+
ellipsis.tooltip.title === true,
26+
[ellipsis]
27+
);
28+
const isAuto = useMemo(
29+
() => isAutoEllipsis || isAutoTooltip || isAutoTooltipTitle,
30+
[isAutoEllipsis, isAutoTooltip, isAutoTooltipTitle]
31+
);
32+
const tooltipTitle = useMemo(() => (isEllipsis ? text : undefined), [isEllipsis, text]);
33+
34+
useEffect(() => {
35+
if (dom && isAuto) {
36+
Promise.resolve().then(() => {
37+
setIsEllipsis(dom.scrollWidth > dom.clientWidth || dom.scrollHeight > dom.clientHeight);
38+
});
39+
}
40+
}, [text, isAuto, dom]);
41+
42+
return (
43+
<Tooltip title={tooltipTitle}>
44+
<Component
45+
ref={setDom}
46+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
47+
{...(rest as any)}
48+
ellipsis={
49+
isAutoEllipsis
50+
? { tooltip: undefined }
51+
: isAutoTooltip
52+
? {
53+
...(ellipsis as EllipsisConfig),
54+
tooltip: undefined,
55+
}
56+
: isAutoTooltipTitle
57+
? {
58+
...(ellipsis as EllipsisConfig),
59+
tooltip: {
60+
...((ellipsis as EllipsisConfig)?.tooltip as TooltipProps),
61+
title: undefined,
62+
},
63+
}
64+
: ellipsis
65+
}
66+
>
67+
{text}
68+
</Component>
69+
</Tooltip>
70+
);
71+
};
72+
}
73+
74+
export type MakeEllipsisTypographyProps<T> = Omit<T, 'children'> & {
75+
text: string | undefined;
76+
};
77+
78+
export default withEllipsisTypography;

src/components/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export { default as withDeleteConfirmAction } from './DeleteConfirmAction/withDe
3030
export type { EditableTextProps } from './EditableText';
3131
export { default as EditableText } from './EditableText';
3232

33+
export * from './EllipsisTypography/EllipsisParagraph';
34+
export { default as EllipsisParagraph } from './EllipsisTypography/EllipsisParagraph';
35+
36+
export * from './EllipsisTypography/EllipsisText';
37+
export { default as EllipsisText } from './EllipsisTypography/EllipsisText';
38+
39+
export * from './EllipsisTypography/EllipsisTitle';
40+
export { default as EllipsisTitle } from './EllipsisTypography/EllipsisTitle';
41+
3342
export type { FloatDrawerProps } from './FloatDrawer';
3443
export { default as FloatDrawer } from './FloatDrawer';
3544

0 commit comments

Comments
 (0)