Skip to content

Commit 542f96d

Browse files
authored
feat(DataList): add tooltip to truncated DataListCell (#11643)
* feat(DataList): add tooltip to truncated DataListCell * update to react 19 syntax
1 parent e3dd20f commit 542f96d

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed
Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { FunctionComponent, useRef, useState, useEffect } from 'react';
12
import { css } from '@patternfly/react-styles';
23
import styles from '@patternfly/react-styles/css/components/DataList/data-list';
34
import { DataListWrapModifier } from './DataList';
5+
import { Tooltip } from '../Tooltip';
46

57
export interface DataListCellProps extends Omit<React.HTMLProps<HTMLDivElement>, 'width'> {
68
/** Content rendered inside the DataList cell */
@@ -19,7 +21,7 @@ export interface DataListCellProps extends Omit<React.HTMLProps<HTMLDivElement>,
1921
wrapModifier?: DataListWrapModifier | 'nowrap' | 'truncate' | 'breakWord';
2022
}
2123

22-
export const DataListCell: React.FunctionComponent<DataListCellProps> = ({
24+
export const DataListCell: FunctionComponent<DataListCellProps> = ({
2325
children = null,
2426
className = '',
2527
width = 1,
@@ -28,20 +30,38 @@ export const DataListCell: React.FunctionComponent<DataListCellProps> = ({
2830
isIcon = false,
2931
wrapModifier = null,
3032
...props
31-
}: DataListCellProps) => (
32-
<div
33-
className={css(
34-
styles.dataListCell,
35-
width > 1 && styles.modifiers[`flex_${width}` as 'flex_2' | 'flex_3' | 'flex_4' | 'flex_5'],
36-
!isFilled && styles.modifiers.noFill,
37-
alignRight && styles.modifiers.alignRight,
38-
isIcon && styles.modifiers.icon,
39-
className,
40-
wrapModifier && styles.modifiers[wrapModifier]
41-
)}
42-
{...props}
43-
>
44-
{children}
45-
</div>
46-
);
33+
}: DataListCellProps) => {
34+
const cellRef = useRef(null);
35+
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
36+
37+
useEffect(() => {
38+
if (!cellRef.current || wrapModifier !== 'truncate') {
39+
return;
40+
}
41+
const showTooltip = cellRef.current && cellRef.current.offsetWidth < cellRef.current.scrollWidth;
42+
if (isTooltipVisible !== showTooltip) {
43+
setIsTooltipVisible(showTooltip);
44+
}
45+
}, [cellRef, wrapModifier, isTooltipVisible]);
46+
47+
return (
48+
<div
49+
className={css(
50+
styles.dataListCell,
51+
width > 1 && styles.modifiers[`flex_${width}` as 'flex_2' | 'flex_3' | 'flex_4' | 'flex_5'],
52+
!isFilled && styles.modifiers.noFill,
53+
alignRight && styles.modifiers.alignRight,
54+
isIcon && styles.modifiers.icon,
55+
className,
56+
wrapModifier && styles.modifiers[wrapModifier]
57+
)}
58+
{...(isTooltipVisible && { tabIndex: 0 })}
59+
ref={cellRef}
60+
{...props}
61+
>
62+
{children}
63+
{isTooltipVisible && <Tooltip content={children} triggerRef={cellRef} />}
64+
</div>
65+
);
66+
};
4767
DataListCell.displayName = 'DataListCell';

0 commit comments

Comments
 (0)