@@ -6,14 +6,17 @@ import type { EllipsisConfig } from 'antd/es/typography/Base';
66import type { ParagraphProps } from 'antd/es/typography/Paragraph' ;
77import type { TextProps } from 'antd/es/typography/Text' ;
88import type { TitleProps } from 'antd/es/typography/Title' ;
9+ import { useResizeDetector } from 'react-resize-detector' ;
10+ import { useRefFunction , useRefValue } from '../../hooks' ;
911
1012function withEllipsisTypography < T extends TextProps | ParagraphProps | TitleProps > (
1113 Component : ComponentType < MakeEllipsisTypographyProps < T > >
1214) {
1315 return function EllipsisText ( props : MakeEllipsisTypographyProps < T > ) {
14- const { ellipsis = true , children, text = children , ...rest } = props ;
16+ const { ellipsis = true , children, text = children , watchResize = true , ...rest } = props ;
1517 const [ isEllipsis , setIsEllipsis ] = useState ( false ) ;
1618 const [ dom , setDom ] = useState < HTMLElement | null > ( null ) ;
19+ const domRef = useRefValue ( dom ) ;
1720 const isAutoEllipsis = useMemo ( ( ) => ellipsis === true , [ ellipsis ] ) ;
1821 const isAutoTooltip = useMemo ( ( ) => typeof ellipsis === 'object' && ellipsis . tooltip === true , [ ellipsis ] ) ;
1922 const isAutoTooltipTitle = useMemo (
@@ -31,10 +34,22 @@ function withEllipsisTypography<T extends TextProps | ParagraphProps | TitleProp
3134 ) ;
3235 const tooltipTitle = useMemo ( ( ) => ( isEllipsis ? text : undefined ) , [ isEllipsis , text ] ) ;
3336
37+ const detectEllipsis = useRefFunction (
38+ ( ) => dom && setIsEllipsis ( dom . scrollWidth > dom . clientWidth || dom . scrollHeight > dom . clientHeight )
39+ ) ;
40+ useResizeDetector ( {
41+ targetRef : watchResize ? domRef : undefined ,
42+ onResize : detectEllipsis ,
43+ refreshRate : 10 ,
44+ refreshOptions : {
45+ leading : false ,
46+ } ,
47+ } ) ;
48+
3449 useEffect ( ( ) => {
3550 if ( dom && isAuto ) {
3651 Promise . resolve ( ) . then ( ( ) => {
37- setIsEllipsis ( dom . scrollWidth > dom . clientWidth || dom . scrollHeight > dom . clientHeight ) ;
52+ detectEllipsis ( ) ;
3853 } ) ;
3954 }
4055 } , [ text , isAuto , dom ] ) ;
@@ -72,8 +87,24 @@ function withEllipsisTypography<T extends TextProps | ParagraphProps | TitleProp
7287}
7388
7489export type MakeEllipsisTypographyProps < T > = Omit < T , 'children' > & {
90+ /**
91+ * - **EN:** The text content to display. If not provided, the children will be used.
92+ * - **CN:** 要显示的文本内容。如果未提供,将使用子元素。
93+ */
7594 text ?: string | undefined ;
95+ /**
96+ * - **EN:** The children content to display. The `text` prop will take precedence if both are
97+ * provided.
98+ * - **CN:** 要显示的子元素内容。如果同时提供了 `text` 属性,将优先使用 `text`。
99+ */
76100 children ?: string | undefined | null ;
101+ /**
102+ * - **EN:** Whether to monitor the component's size and adjust the ellipsis accordingly.
103+ * - **CN:** 是否监控组件的大小并相应地调整省略号。
104+ *
105+ * @default true
106+ */
107+ watchResize ?: boolean ;
77108} ;
78109
79110export default withEllipsisTypography ;
0 commit comments