Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions packages/react-core/src/components/DataList/DataListCell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FunctionComponent, useRef, useState, useEffect } from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/DataList/data-list';
import { DataListWrapModifier } from './DataList';
import { Tooltip } from '../Tooltip';

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

export const DataListCell: React.FunctionComponent<DataListCellProps> = ({
export const DataListCell: FunctionComponent<DataListCellProps> = ({
children = null,
className = '',
width = 1,
Expand All @@ -28,20 +30,38 @@ export const DataListCell: React.FunctionComponent<DataListCellProps> = ({
isIcon = false,
wrapModifier = null,
...props
}: DataListCellProps) => (
<div
className={css(
styles.dataListCell,
width > 1 && styles.modifiers[`flex_${width}` as 'flex_2' | 'flex_3' | 'flex_4' | 'flex_5'],
!isFilled && styles.modifiers.noFill,
alignRight && styles.modifiers.alignRight,
isIcon && styles.modifiers.icon,
className,
wrapModifier && styles.modifiers[wrapModifier]
)}
{...props}
>
{children}
</div>
);
}: DataListCellProps) => {
const cellRef = useRef(null);
const [isTooltipVisible, setIsTooltipVisible] = useState(false);

useEffect(() => {
if (!cellRef.current || wrapModifier !== 'truncate') {
return;
}
const showTooltip = cellRef.current && cellRef.current.offsetWidth < cellRef.current.scrollWidth;
if (isTooltipVisible !== showTooltip) {
setIsTooltipVisible(showTooltip);
}
}, [cellRef, wrapModifier, isTooltipVisible]);

return (
<div
className={css(
styles.dataListCell,
width > 1 && styles.modifiers[`flex_${width}` as 'flex_2' | 'flex_3' | 'flex_4' | 'flex_5'],
!isFilled && styles.modifiers.noFill,
alignRight && styles.modifiers.alignRight,
isIcon && styles.modifiers.icon,
className,
wrapModifier && styles.modifiers[wrapModifier]
)}
{...(isTooltipVisible && { tabIndex: 0 })}
ref={cellRef}
{...props}
>
{children}
{isTooltipVisible && <Tooltip content={children} triggerRef={cellRef} />}
</div>
);
};
DataListCell.displayName = 'DataListCell';
Loading