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