Skip to content

Commit 92ed4a7

Browse files
committed
feat(DataList): add tooltip to truncated DataListCell
1 parent eeb1714 commit 92ed4a7

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

packages/react-core/src/components/DataList/DataListCell.tsx

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { css } from '@patternfly/react-styles';
33
import styles from '@patternfly/react-styles/css/components/DataList/data-list';
44
import { DataListWrapModifier } from './DataList';
5+
import { Tooltip } from '../Tooltip';
56

67
export interface DataListCellProps extends Omit<React.HTMLProps<HTMLDivElement>, 'width'> {
78
/** Content rendered inside the DataList cell */
@@ -29,20 +30,38 @@ export const DataListCell: React.FunctionComponent<DataListCellProps> = ({
2930
isIcon = false,
3031
wrapModifier = null,
3132
...props
32-
}: DataListCellProps) => (
33-
<div
34-
className={css(
35-
styles.dataListCell,
36-
width > 1 && styles.modifiers[`flex_${width}` as 'flex_2' | 'flex_3' | 'flex_4' | 'flex_5'],
37-
!isFilled && styles.modifiers.noFill,
38-
alignRight && styles.modifiers.alignRight,
39-
isIcon && styles.modifiers.icon,
40-
className,
41-
wrapModifier && styles.modifiers[wrapModifier]
42-
)}
43-
{...props}
44-
>
45-
{children}
46-
</div>
47-
);
33+
}: DataListCellProps) => {
34+
const cellRef = React.useRef(null);
35+
const [isTooltipVisible, setIsTooltipVisible] = React.useState(false);
36+
37+
React.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+
};
4867
DataListCell.displayName = 'DataListCell';

0 commit comments

Comments
 (0)