| 1 |
import { createRef, useEffect, useState } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Table/table'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { Tooltip, TooltipProps } from '@patternfly/react-core/dist/esm/components/Tooltip'; |
| 5 |
|
| 6 |
export enum TableTextVariant { |
| 7 |
div = 'div', |
| 8 |
nav = 'nav' |
| 9 |
} |
| 10 |
|
| 11 |
export enum WrapModifier { |
| 12 |
wrap = 'wrap', |
| 13 |
nowrap = 'nowrap', |
| 14 |
truncate = 'truncate', |
| 15 |
breakWord = 'breakWord', |
| 16 |
fitContent = 'fitContent' |
| 17 |
} |
| 18 |
|
| 19 |
export interface TableTextProps extends React.HTMLProps<HTMLDivElement> { |
| 20 |
/** Content rendered within the table text */ |
| 21 |
children?: React.ReactNode; |
| 22 |
/** Additional classes added to the table text */ |
| 23 |
className?: string; |
| 24 |
/** Determines which element to render as a table text */ |
| 25 |
variant?: TableTextVariant | 'span' | 'div'; |
| 26 |
/** Determines which wrapping modifier to apply to the table text */ |
| 27 |
wrapModifier?: WrapModifier | 'wrap' | 'nowrap' | 'truncate' | 'breakWord' | 'fitContent'; |
| 28 |
/** text to display on the tooltip */ |
| 29 |
tooltip?: React.ReactNode; |
| 30 |
/** other props to pass to the tooltip */ |
| 31 |
tooltipProps?: Omit<TooltipProps, 'content'>; |
| 32 |
/** callback used to create the tooltip if text is truncated */ |
| 33 |
onMouseEnter?: (event: any) => void; |
| 34 |
/** Determines if the TableText is focused by parent component */ |
| 35 |
focused?: boolean; |
| 36 |
/** Determines if tooltip should have normal visibility behavior. If false, the tooltip will only be shown when children is not entirely visible */ |
| 37 |
tooltipHasDefaultBehavior?: boolean; |
| 38 |
} |
| 39 |
|
| 40 |
export const TableText: React.FunctionComponent<TableTextProps> = ({ |
| 41 |
children = null, |
| 42 |
className = '', |
| 43 |
variant = 'span', |
| 44 |
wrapModifier = null, |
| 45 |
tooltip: tooltipProp = '', |
| 46 |
tooltipProps = {}, |
| 47 |
onMouseEnter: onMouseEnterProp = () => {}, |
| 48 |
focused = false, |
| 49 |
tooltipHasDefaultBehavior = false, |
| 50 |
...props |
| 51 |
}: TableTextProps) => { |
| 52 |
const Component: TableTextVariant | 'span' | 'div' = variant; |
| 53 |
const textRef = createRef<HTMLElement>(); |
| 54 |
|
| 55 |
const [tooltip, setTooltip] = useState(tooltipProp); |
| 56 |
const onMouseEnter = (event: any) => { |
| 57 |
if (event.target.offsetWidth < event.target.scrollWidth) { |
| 58 |
setTooltip(tooltipProp || event.target.innerText); |
| 59 |
} else { |
| 60 |
setTooltip(''); |
| 61 |
} |
| 62 |
onMouseEnterProp(event); |
| 63 |
}; |
| 64 |
|
| 65 |
const onFocus = (element: HTMLElement) => { |
| 66 |
if (element.offsetWidth < element.scrollWidth) { |
| 67 |
setTooltip(tooltipProp || element.innerText); |
| 68 |
} else { |
| 69 |
setTooltip(''); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
const text = ( |
| 74 |
<Component |
| 75 |
ref={textRef as React.Ref<HTMLDivElement>} |
| 76 |
onMouseEnter={!tooltipHasDefaultBehavior ? onMouseEnter : undefined} |
| 77 |
className={css(className, wrapModifier && styles.modifiers[wrapModifier], styles.tableText)} |
| 78 |
{...props} |
| 79 |
> |
| 80 |
{children} |
| 81 |
</Component> |
| 82 |
); |
| 83 |
|
| 84 |
useEffect(() => { |
| 85 |
if (!tooltipHasDefaultBehavior) { |
| 86 |
if (focused) { |
| 87 |
onFocus(textRef.current); |
| 88 |
} else { |
| 89 |
setTooltip(''); |
| 90 |
} |
| 91 |
} |
| 92 |
}, [focused, tooltipHasDefaultBehavior]); |
| 93 |
|
| 94 |
return tooltip !== '' ? ( |
| 95 |
<Tooltip |
| 96 |
triggerRef={textRef} |
| 97 |
content={tooltip} |
| 98 |
{...(!tooltipHasDefaultBehavior && { isVisible: true })} |
| 99 |
{...tooltipProps} |
| 100 |
> |
| 101 |
{text} |
| 102 |
</Tooltip> |
| 103 |
) : ( |
| 104 |
text |
| 105 |
); |
| 106 |
}; |
| 107 |
TableText.displayName = 'TableText'; |