|
| 1 | +import { Tooltip, TooltipProps } from 'antd'; |
| 2 | +import React, { FC, memo, useEffect, useState } from 'react'; |
| 3 | +import './index.less'; |
| 4 | + |
| 5 | +interface ITextEllipsisProps { |
| 6 | + text: string; |
| 7 | + style?: object; |
| 8 | + className?: string; |
| 9 | + toolTipProps?: TooltipProps; |
| 10 | + type?: 'text' | 'paragraph'; |
| 11 | + rows?: number; |
| 12 | +} |
| 13 | +const TextEllipsis: FC<ITextEllipsisProps> = ({ |
| 14 | + text, |
| 15 | + style, |
| 16 | + toolTipProps, |
| 17 | + type = 'text', |
| 18 | + rows = 1, |
| 19 | + className, |
| 20 | +}) => { |
| 21 | + const typographyRef = React.useRef<HTMLElement>(null); |
| 22 | + const [isEllipse, setIsEllipse] = useState(false); |
| 23 | + |
| 24 | + const component = |
| 25 | + type === 'paragraph' ? ( |
| 26 | + <span |
| 27 | + ref={typographyRef} |
| 28 | + className={`paragraph-ellipsis ${className}`} |
| 29 | + style={{ |
| 30 | + ...style, |
| 31 | + display: '-webkit-box', |
| 32 | + overflow: 'hidden', |
| 33 | + WebkitBoxOrient: 'vertical', |
| 34 | + WebkitLineClamp: rows, |
| 35 | + }} |
| 36 | + > |
| 37 | + {text} |
| 38 | + </span> |
| 39 | + ) : ( |
| 40 | + <span |
| 41 | + ref={typographyRef} |
| 42 | + className={`text-ellipsis ${className}`} |
| 43 | + style={style} |
| 44 | + > |
| 45 | + {text} |
| 46 | + </span> |
| 47 | + ); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (text) { |
| 51 | + if (type === 'paragraph') { |
| 52 | + const { offsetHeight, scrollHeight, clientHeight } = |
| 53 | + typographyRef.current; |
| 54 | + setIsEllipse(scrollHeight > clientHeight); |
| 55 | + } else { |
| 56 | + const isEllipse = isEleEllipsis(typographyRef?.current); |
| 57 | + setIsEllipse(isEllipse); |
| 58 | + } |
| 59 | + } |
| 60 | + }, [text]); |
| 61 | + |
| 62 | + const isEleEllipsis = (ele: HTMLElement): boolean => { |
| 63 | + const childDiv = document.createElement('em'); |
| 64 | + ele.appendChild(childDiv); |
| 65 | + |
| 66 | + const rect = ele.getBoundingClientRect(); |
| 67 | + const childRect = childDiv.getBoundingClientRect(); |
| 68 | + |
| 69 | + ele.removeChild(childDiv); |
| 70 | + |
| 71 | + return ( |
| 72 | + rect.left > childRect.left || |
| 73 | + childRect.right > rect.right || |
| 74 | + rect.top > childRect.top || |
| 75 | + childRect.bottom > rect.bottom |
| 76 | + ); |
| 77 | + }; |
| 78 | + if (isEllipse) { |
| 79 | + return ( |
| 80 | + <Tooltip |
| 81 | + title={text} |
| 82 | + getPopupContainer={() => |
| 83 | + document.getElementById('xflow-container') as HTMLElement |
| 84 | + } |
| 85 | + color="#ffff" |
| 86 | + overlayInnerStyle={{ |
| 87 | + color: '#354052', |
| 88 | + fontSize: '12px', |
| 89 | + borderRadius: '8px', |
| 90 | + }} |
| 91 | + placement="bottomRight" |
| 92 | + {...toolTipProps} |
| 93 | + > |
| 94 | + {component} |
| 95 | + </Tooltip> |
| 96 | + ); |
| 97 | + } |
| 98 | + return component; |
| 99 | +}; |
| 100 | + |
| 101 | +export default memo(TextEllipsis); |
0 commit comments