|
1 | 1 | import { css } from '@patternfly/react-styles'; |
2 | 2 | import styles from '@patternfly/react-styles/css/components/DataList/data-list'; |
3 | 3 | import { DataListWrapModifier } from './DataList'; |
| 4 | +import { Tooltip } from '../Tooltip'; |
4 | 5 |
|
5 | 6 | export interface DataListCellProps extends Omit<React.HTMLProps<HTMLDivElement>, 'width'> { |
6 | 7 | /** Content rendered inside the DataList cell */ |
@@ -28,20 +29,38 @@ export const DataListCell: React.FunctionComponent<DataListCellProps> = ({ |
28 | 29 | isIcon = false, |
29 | 30 | wrapModifier = null, |
30 | 31 | ...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 | +}; |
47 | 66 | DataListCell.displayName = 'DataListCell'; |
0 commit comments