| 1 |
import { useState } from 'react'; |
| 2 |
import { Tooltip } from '@patternfly/react-core/dist/esm/components/Tooltip'; |
| 3 |
import { Bullseye } from '@patternfly/react-core/dist/esm/layouts/Bullseye'; |
| 4 |
import { EmptyState } from '@patternfly/react-core/dist/esm/components/EmptyState'; |
| 5 |
import { SelectProps } from '@patternfly/react-core/dist/esm/components/Select'; |
| 6 |
import { Td } from '../../../components'; |
| 7 |
|
| 8 |
export interface BodyCellProps { |
| 9 |
'data-label'?: string; |
| 10 |
className?: string; |
| 11 |
colSpan?: number; |
| 12 |
component?: React.ReactNode; |
| 13 |
errorText?: string; |
| 14 |
isVisible?: boolean; |
| 15 |
parentId?: number; |
| 16 |
textCenter?: boolean; |
| 17 |
isOpen?: boolean; |
| 18 |
ariaControls?: string; |
| 19 |
editableValue?: any; |
| 20 |
editableSelectProps?: SelectProps; |
| 21 |
options?: React.ReactElement<any>[]; |
| 22 |
isSelectOpen?: boolean; |
| 23 |
value?: any; |
| 24 |
isValid?: boolean; |
| 25 |
name?: string; |
| 26 |
tooltip?: string; |
| 27 |
onMouseEnter?: (event: any) => void; |
| 28 |
children: React.ReactNode; |
| 29 |
} |
| 30 |
export const BodyCell: React.FunctionComponent<BodyCellProps> = ({ |
| 31 |
'data-label': dataLabel = '', |
| 32 |
className = '', |
| 33 |
colSpan, |
| 34 |
component = 'td', |
| 35 |
isVisible, |
| 36 |
parentId, |
| 37 |
textCenter = false, |
| 38 |
tooltip: tooltipProp = '', |
| 39 |
onMouseEnter: onMouseEnterProp = () => {}, |
| 40 |
children, |
| 41 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 42 |
errorText, |
| 43 |
isValid, |
| 44 |
isOpen, |
| 45 |
ariaControls, |
| 46 |
editableValue, |
| 47 |
editableSelectProps, |
| 48 |
options, |
| 49 |
isSelectOpen, |
| 50 |
value, |
| 51 |
name, |
| 52 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 53 |
...props |
| 54 |
}: BodyCellProps) => { |
| 55 |
const [tooltip, setTooltip] = useState(''); |
| 56 |
const onMouseEnter = (event: any) => { |
| 57 |
if (event.target.offsetWidth < event.target.scrollWidth) { |
| 58 |
if (tooltipProp) { |
| 59 |
setTooltip(tooltipProp); |
| 60 |
} else if (typeof children === 'string') { |
| 61 |
setTooltip(children); |
| 62 |
} |
| 63 |
} else { |
| 64 |
setTooltip(''); |
| 65 |
} |
| 66 |
onMouseEnterProp(event); |
| 67 |
}; |
| 68 |
|
| 69 |
let isEmptyStateCell = false; |
| 70 |
if (children) { |
| 71 |
isEmptyStateCell = |
| 72 |
((children as React.ReactElement<any>).type === Bullseye && |
| 73 |
(children as React.ReactElement<any>).props.children && |
| 74 |
(children as React.ReactElement<any>).props.children.type === EmptyState) || |
| 75 |
(children as React.ReactElement<any>).type === EmptyState; |
| 76 |
} |
| 77 |
|
| 78 |
const cell = ( |
| 79 |
<Td |
| 80 |
className={className} |
| 81 |
component={component} |
| 82 |
dataLabel={dataLabel && parentId == null && !isEmptyStateCell ? dataLabel : null} |
| 83 |
onMouseEnter={onMouseEnter} |
| 84 |
textCenter={textCenter} |
| 85 |
colSpan={colSpan} |
| 86 |
{...props} |
| 87 |
> |
| 88 |
{children} |
| 89 |
</Td> |
| 90 |
); |
| 91 |
|
| 92 |
const bodyCell = |
| 93 |
tooltip !== '' ? ( |
| 94 |
<Tooltip content={tooltip} isVisible> |
| 95 |
{cell} |
| 96 |
</Tooltip> |
| 97 |
) : ( |
| 98 |
cell |
| 99 |
); |
| 100 |
|
| 101 |
return (parentId !== undefined && colSpan === undefined) || !isVisible ? null : bodyCell; |
| 102 |
}; |
| 103 |
BodyCell.displayName = 'BodyCell'; |