| 1 |
import { useState } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/DataList/data-list'; |
| 4 |
import { Tooltip } from '../Tooltip'; |
| 5 |
import { DataListWrapModifier } from './DataList'; |
| 6 |
|
| 7 |
export interface DataListTextProps extends React.HTMLProps<HTMLDivElement> { |
| 8 |
/** Content rendered within the data list text */ |
| 9 |
children?: React.ReactNode; |
| 10 |
/** Additional classes added to the data list text */ |
| 11 |
className?: string; |
| 12 |
/** Determines which element to render as a data list text. Usually div or span */ |
| 13 |
component?: React.ReactNode; |
| 14 |
/** Determines which wrapping modifier to apply to the data list text */ |
| 15 |
wrapModifier?: DataListWrapModifier | 'nowrap' | 'truncate' | 'breakWord'; |
| 16 |
/** text to display on the tooltip */ |
| 17 |
tooltip?: string; |
| 18 |
/** callback used to create the tooltip if text is truncated */ |
| 19 |
onMouseEnter?: (event: any) => void; |
| 20 |
} |
| 21 |
|
| 22 |
export const DataListText: React.FunctionComponent<DataListTextProps> = ({ |
| 23 |
children = null, |
| 24 |
className = '', |
| 25 |
component = 'span', |
| 26 |
wrapModifier = null, |
| 27 |
tooltip: tooltipProp = '', |
| 28 |
onMouseEnter: onMouseEnterProp = () => {}, |
| 29 |
...props |
| 30 |
}: DataListTextProps) => { |
| 31 |
const Component = component as any; |
| 32 |
|
| 33 |
const [tooltip, setTooltip] = useState(''); |
| 34 |
const onMouseEnter = (event: any) => { |
| 35 |
if (event.target.offsetWidth < event.target.scrollWidth) { |
| 36 |
setTooltip(tooltipProp || event.target.innerHTML); |
| 37 |
} else { |
| 38 |
setTooltip(''); |
| 39 |
} |
| 40 |
onMouseEnterProp(event); |
| 41 |
}; |
| 42 |
|
| 43 |
const text = ( |
| 44 |
<Component |
| 45 |
onMouseEnter={onMouseEnter} |
| 46 |
className={css(className, wrapModifier && styles.modifiers[wrapModifier], styles.dataListText)} |
| 47 |
{...props} |
| 48 |
> |
| 49 |
{children} |
| 50 |
</Component> |
| 51 |
); |
| 52 |
|
| 53 |
return tooltip !== '' ? ( |
| 54 |
<Tooltip content={tooltip} isVisible> |
| 55 |
{text} |
| 56 |
</Tooltip> |
| 57 |
) : ( |
| 58 |
text |
| 59 |
); |
| 60 |
}; |
| 61 |
DataListText.displayName = 'DataListText'; |