@@ -64,7 +64,7 @@ const EllipsisText = (props: IEllipsisTextProps) => {
6464 : null ;
6565
6666 const [ visible , setVisible ] = useState ( false ) ;
67- const [ width , setWidth ] = useState < number | string > ( DEFAULT_MAX_WIDTH ) ;
67+ const [ width , setWidth ] = useState < number | string | undefined > ( maxWidth ) ;
6868 const [ cursor , setCursor ] = useState ( 'default' ) ;
6969
7070 useLayoutEffect ( ( ) => {
@@ -96,40 +96,6 @@ const EllipsisText = (props: IEllipsisTextProps) => {
9696 return parseInt ( getStyle ( dom , attr ) ) ;
9797 } ;
9898
99- /**
100- * @description : 10 -> 10,
101- * @description : 10px -> 10,
102- * @description : 90% -> ele.width * 0.9
103- * @description : calc(100% - 32px) -> ele.width - 32
104- * @param {* } ele
105- * @param {string & number } maxWidth
106- * @return {* }
107- */
108- const transitionWidth = ( ele : HTMLElement , maxWidth : string | number ) => {
109- const eleWidth = getActualWidth ( ele ) ;
110-
111- if ( typeof maxWidth === 'number' ) {
112- return maxWidth > eleWidth ? eleWidth : maxWidth ; // 如果父元素的宽度小于传入的最大宽度,返回父元素的宽度
113- }
114-
115- const numMatch = maxWidth . match ( / ^ ( \d + ) ( p x ) ? $ / ) ;
116- if ( numMatch ) {
117- return + numMatch [ 1 ] > eleWidth ? eleWidth : + numMatch [ 1 ] ; // 如果父元素的宽度小于传入的最大宽度,返回父元素的宽度
118- }
119-
120- const percentMatch = maxWidth . match ( / ^ ( \d + ) % $ / ) ;
121- if ( percentMatch ) {
122- return eleWidth * ( parseInt ( percentMatch [ 1 ] ) / 100 ) ;
123- }
124-
125- const relativeMatch = maxWidth . match ( / ^ c a l c \( 1 0 0 % - ( \d + ) p x \) $ / ) ;
126- if ( relativeMatch ) {
127- return eleWidth - parseInt ( relativeMatch [ 1 ] ) ;
128- }
129-
130- return eleWidth ;
131- } ;
132-
13399 const hideEleContent = ( node : HTMLElement ) => {
134100 node . style . display = 'none' ;
135101 } ;
@@ -144,21 +110,24 @@ const EllipsisText = (props: IEllipsisTextProps) => {
144110 * @return {* }
145111 */
146112 const getContainerWidth = ( ele : HTMLElement ) : number | string => {
113+ // 如果设置了最大宽度,则直接返回宽度
114+ if ( maxWidth ) return maxWidth ;
147115 if ( ! ele ) return DEFAULT_MAX_WIDTH ;
148116
149- const { scrollWidth, parentElement } = ele ;
150-
151- // 如果是行内元素,获取不到宽度,则向上寻找父元素
152- if ( scrollWidth === 0 ) {
153- return getContainerWidth ( parentElement ! ) ;
154- }
155- // 如果设置了最大宽度,则直接返回宽度
156- if ( maxWidth ) {
157- return transitionWidth ( ele , maxWidth ) ;
117+ // 如果是行内元素获取不到宽度或者本身没有宽度,则向上寻找父元素
118+ if ( ele . scrollWidth === 0 || ele . clientWidth === 0 ) {
119+ return getContainerWidth ( ele . parentElement ! ) ;
158120 }
159121
122+ // 隐藏当前文本元素后,从查找到的父组件宽度中计算剩余可用宽度
160123 hideEleContent ( ellipsisRef . current ! ) ;
161124
125+ // 如果父组件是非行内元素,但其宽度是由子元素撑开的,仍需要向上查找
126+ if ( ele . scrollWidth === 0 || ele . clientWidth === 0 ) {
127+ showEleContent ( ellipsisRef . current ! ) ;
128+ return getContainerWidth ( ele . parentElement ! ) ;
129+ }
130+
162131 const availableWidth = getAvailableWidth ( ele ) ;
163132
164133 return availableWidth < 0 ? 0 : availableWidth ;
0 commit comments