@@ -4,7 +4,7 @@ import iconArrowRight from '@ui5/webcomponents-icons/dist/slim-arrow-right.js';
4
4
import { useStylesheet } from '@ui5/webcomponents-react-base' ;
5
5
import { clsx } from 'clsx' ;
6
6
import type { ReactNode } from 'react' ;
7
- import { forwardRef , useContext } from 'react' ;
7
+ import { Children , forwardRef , useContext , useEffect , useRef , useState } from 'react' ;
8
8
import { FlexBoxAlignItems , FlexBoxDirection , ListItemType , ValueState } from '../../enums/index.js' ;
9
9
import { MessageViewContext } from '../../internal/MessageViewContext.js' ;
10
10
import type { CommonProps } from '../../types/index.js' ;
@@ -57,6 +57,9 @@ export interface MessageItemPropTypes extends CommonProps {
57
57
*/
58
58
const MessageItem = forwardRef < CustomListItemDomRef , MessageItemPropTypes > ( ( props , ref ) => {
59
59
const { titleText, subtitleText, counter, type = ValueState . Error , children, className, ...rest } = props ;
60
+ const [ isTitleTextOverflowing , setIsTitleTextIsOverflowing ] = useState ( false ) ;
61
+ const titleTextRef = useRef < HTMLSpanElement > ( null ) ;
62
+ const hasDetails = ! ! ( children || isTitleTextOverflowing ) ;
60
63
61
64
useStylesheet ( styleData , MessageItem . displayName ) ;
62
65
@@ -69,10 +72,10 @@ const MessageItem = forwardRef<CustomListItemDomRef, MessageItemPropTypes>((prop
69
72
subtitleText && classNames . withSubtitle
70
73
) ;
71
74
72
- const messageClasses = clsx ( classNames . message , children && classNames . withChildren ) ;
75
+ const messageClasses = clsx ( classNames . message , hasDetails && classNames . withChildren ) ;
73
76
74
77
const handleListItemClick = ( e ) => {
75
- if ( children ) {
78
+ if ( hasDetails ) {
76
79
selectMessage ( props ) ;
77
80
if ( typeof rest . onClick === 'function' ) {
78
81
rest . onClick ( e ) ;
@@ -88,13 +91,31 @@ const MessageItem = forwardRef<CustomListItemDomRef, MessageItemPropTypes>((prop
88
91
handleListItemClick ( e ) ;
89
92
}
90
93
} ;
94
+
95
+ const hasChildren = Children . count ( children ) ;
96
+ useEffect ( ( ) => {
97
+ const titleTextObserver = new ResizeObserver ( ( [ titleTextSpan ] ) => {
98
+ if ( titleTextSpan . target . scrollWidth > titleTextSpan . target . clientWidth ) {
99
+ setIsTitleTextIsOverflowing ( true ) ;
100
+ } else {
101
+ setIsTitleTextIsOverflowing ( false ) ;
102
+ }
103
+ } ) ;
104
+ if ( ! hasChildren && titleTextRef . current ) {
105
+ titleTextObserver . observe ( titleTextRef . current ) ;
106
+ }
107
+ return ( ) => {
108
+ titleTextObserver . disconnect ( ) ;
109
+ } ;
110
+ } , [ hasChildren ] ) ;
111
+
91
112
return (
92
113
< CustomListItem
93
114
onClick = { handleListItemClick }
94
115
onKeyDown = { handleKeyDown }
95
116
data-title = { titleText }
96
117
data-type = { type }
97
- type = { children ? ListItemType . Active : ListItemType . Inactive }
118
+ type = { hasDetails ? ListItemType . Active : ListItemType . Inactive }
98
119
{ ...rest }
99
120
className = { listItemClasses }
100
121
ref = { ref }
@@ -107,11 +128,15 @@ const MessageItem = forwardRef<CustomListItemDomRef, MessageItemPropTypes>((prop
107
128
direction = { FlexBoxDirection . Column }
108
129
style = { { flex : 'auto' , whiteSpace : 'nowrap' , overflow : 'hidden' , textOverflow : 'ellipsis' } }
109
130
>
110
- { titleText && < span className = { classNames . title } > { titleText } </ span > }
111
- { subtitleText && < Label className = { classNames . subtitle } > { subtitleText } </ Label > }
131
+ { titleText && (
132
+ < span className = { classNames . title } ref = { titleTextRef } >
133
+ { titleText }
134
+ </ span >
135
+ ) }
136
+ { titleText && subtitleText && < Label className = { classNames . subtitle } > { subtitleText } </ Label > }
112
137
</ FlexBox >
113
138
{ counter != null && < span className = { classNames . counter } > { counter } </ span > }
114
- { children && < Icon className = { classNames . navigation } name = { iconArrowRight } /> }
139
+ { hasDetails && < Icon className = { classNames . navigation } name = { iconArrowRight } /> }
115
140
</ FlexBox >
116
141
</ CustomListItem >
117
142
) ;
0 commit comments