Skip to content

Commit e5c5425

Browse files
committed
feat(DataList): add tooltip to truncated DataListCell
1 parent b8da713 commit e5c5425

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
@@ -1,6 +1,7 @@
11
import { css } from '@patternfly/react-styles';
22
import styles from '@patternfly/react-styles/css/components/DataList/data-list';
33
import { DataListWrapModifier } from './DataList';
4+
import { Tooltip } from '../Tooltip';
45

56
export interface DataListCellProps extends Omit<React.HTMLProps<HTMLDivElement>, 'width'> {
67
/** Content rendered inside the DataList cell */
@@ -28,20 +29,38 @@ export const DataListCell: React.FunctionComponent<DataListCellProps> = ({
2829
isIcon = false,
2930
wrapModifier = null,
3031
...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-
);
32+
}: DataListCellProps) => {
33+
const cellRef = React.useRef(null);
34+
const [isTooltipVisible, setIsTooltipVisible] = React.useState(false);
35+
36+
React.useEffect(() => {
37+
if (!cellRef.current || wrapModifier !== 'truncate') {
38+
return;
39+
}
40+
const showTooltip = cellRef.current && cellRef.current.offsetWidth < cellRef.current.scrollWidth;
41+
if (isTooltipVisible !== showTooltip) {
42+
setIsTooltipVisible(showTooltip);
43+
}
44+
}, [cellRef, wrapModifier, isTooltipVisible]);
45+
46+
return (
47+
<div
48+
className={css(
49+
styles.dataListCell,
50+
width > 1 && styles.modifiers[`flex_${width}` as 'flex_2' | 'flex_3' | 'flex_4' | 'flex_5'],
51+
!isFilled && styles.modifiers.noFill,
52+
alignRight && styles.modifiers.alignRight,
53+
isIcon && styles.modifiers.icon,
54+
className,
55+
wrapModifier && styles.modifiers[wrapModifier]
56+
)}
57+
{...(isTooltipVisible && { tabIndex: 0 })}
58+
ref={cellRef}
59+
{...props}
60+
>
61+
{children}
62+
{isTooltipVisible && <Tooltip content={children} triggerRef={cellRef} />}
63+
</div>
64+
);
65+
};
4766
DataListCell.displayName = 'DataListCell';

0 commit comments

Comments
 (0)