Debian Codegraph @

node/@patternfly/react-core/src/components/DataList/DataListCell.tsx

raw
1 import { FunctionComponent, useRef, useState, useEffect } from 'react';
2 import { css } from '@patternfly/react-styles';
3 import styles from '@patternfly/react-styles/css/components/DataList/data-list';
4 import { DataListWrapModifier } from './DataList';
5 import { Tooltip } from '../Tooltip';
6
7 export interface DataListCellProps extends Omit<React.HTMLProps<HTMLDivElement>, 'width'> {
8 /** Content rendered inside the data list cell */
9 children?: React.ReactNode;
10 /** Additional classes added to the data list cell */
11 className?: string;
12 /** Width (from 1-5) to the data list cell */
13 width?: 1 | 2 | 3 | 4 | 5;
14 /** Enables the body content to fill the height of the card */
15 isFilled?: boolean;
16 /** Aligns the cell content to the right of its parent. */
17 alignRight?: boolean;
18 /** Set to true if the cell content is an icon */
19 isIcon?: boolean;
20 /** Determines which wrapping modifier to apply to the data list cell */
21 wrapModifier?: DataListWrapModifier | 'nowrap' | 'truncate' | 'breakWord';
22 }
23
24 export const DataListCell: FunctionComponent<DataListCellProps> = ({
25 children = null,
26 className = '',
27 width = 1,
28 isFilled = true,
29 alignRight = false,
30 isIcon = false,
31 wrapModifier = null,
32 ...props
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 };
67 DataListCell.displayName = 'DataListCell';