|
|
@@ -1,16 +1,23 @@
|
|
|
-import { useState, useRef, useEffect } from 'react';
|
|
|
+import React, { useState, useRef, useEffect } from 'react';
|
|
|
import { unmountComponentAtNode } from 'react-dom';
|
|
|
import { useSize } from 'ahooks';
|
|
|
import Style from './index.less';
|
|
|
import classnames from 'classnames';
|
|
|
import { Tooltip } from 'antd';
|
|
|
+import type { TooltipProps } from 'antd';
|
|
|
|
|
|
interface EllipsisProps {
|
|
|
- title?: string;
|
|
|
+ title?: string | number;
|
|
|
maxWidth?: string | number;
|
|
|
- titleStyle?: CSSStyleSheet;
|
|
|
+ /**
|
|
|
+ * 用于max-width的情况
|
|
|
+ */
|
|
|
+ limitWidth?: number;
|
|
|
+ style?: React.CSSProperties;
|
|
|
+ titleStyle?: React.CSSProperties;
|
|
|
titleClassName?: string;
|
|
|
showToolTip?: boolean;
|
|
|
+ tooltip?: Omit<TooltipProps, 'title'>;
|
|
|
/**
|
|
|
* @default 1
|
|
|
*/
|
|
|
@@ -21,25 +28,43 @@ export default (props: EllipsisProps) => {
|
|
|
const parentNode = useRef<HTMLDivElement | null>(null);
|
|
|
const extraNode = useRef<HTMLDivElement | null>(null);
|
|
|
const titleNode = useRef<HTMLDivElement | null>(null);
|
|
|
- const parentSize = useSize(parentNode.current);
|
|
|
const extraSize = useSize(extraNode.current);
|
|
|
const [isEllipsis, setIsEllipsis] = useState(false);
|
|
|
// const [show, setShow] = useState(false);
|
|
|
|
|
|
useEffect(() => {
|
|
|
- if (extraSize.width && parentSize.width) {
|
|
|
- if (extraSize.width > parentSize.width * (props.row || 1)) {
|
|
|
- setIsEllipsis(true);
|
|
|
- } else {
|
|
|
- setIsEllipsis(false);
|
|
|
- }
|
|
|
- if (extraNode.current) {
|
|
|
- unmountComponentAtNode(extraNode.current);
|
|
|
- extraNode.current.innerHTML = '';
|
|
|
- extraNode.current.setAttribute('style', 'display: none');
|
|
|
+ if (extraNode.current && props.title) {
|
|
|
+ extraNode.current.innerHTML = props.title as string;
|
|
|
+ extraNode.current.setAttribute('style', 'display: block');
|
|
|
+ }
|
|
|
+ }, [props.title, extraNode.current]);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (extraNode.current && parentNode.current) {
|
|
|
+ const extraWidth = window.getComputedStyle(extraNode.current).getPropertyValue('width');
|
|
|
+ const parentWidth = window.getComputedStyle(parentNode.current).getPropertyValue('width');
|
|
|
+ const extraWidthNumber = extraWidth ? Number(extraWidth.replace('px', '')) : 0;
|
|
|
+ const parentWidthNumber = parentWidth ? Number(parentWidth.replace('px', '')) : 0;
|
|
|
+
|
|
|
+ // console.log(extraWidthNumber, parentWidthNumber, props.title)
|
|
|
+ if (extraWidthNumber && parentWidthNumber) {
|
|
|
+ const _width = props.limitWidth
|
|
|
+ ? props.limitWidth * (props.row || 1)
|
|
|
+ : parentWidthNumber * (props.row || 1);
|
|
|
+ console.log(extraSize.width, _width, props.title);
|
|
|
+ if (extraWidthNumber >= _width) {
|
|
|
+ setIsEllipsis(true);
|
|
|
+ } else {
|
|
|
+ setIsEllipsis(false);
|
|
|
+ }
|
|
|
+ if (extraNode.current) {
|
|
|
+ unmountComponentAtNode(extraNode.current);
|
|
|
+ extraNode.current.innerHTML = '';
|
|
|
+ extraNode.current.setAttribute('style', 'display: none');
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- }, [props.title, extraSize]);
|
|
|
+ }, [extraSize, extraNode.current]);
|
|
|
|
|
|
const ellipsisTitleClass = `ellipsis-${props.row || 1}`;
|
|
|
|
|
|
@@ -59,14 +84,16 @@ export default (props: EllipsisProps) => {
|
|
|
);
|
|
|
|
|
|
return (
|
|
|
- <div className={Style['ellipsis-warp']} ref={parentNode}>
|
|
|
+ <div className={Style['ellipsis-warp']} style={props.style} ref={parentNode}>
|
|
|
{isEllipsis && props.showToolTip !== false ? (
|
|
|
- <Tooltip title={props.title}>{ellipsisNode}</Tooltip>
|
|
|
+ <Tooltip {...props.tooltip} title={props.title}>
|
|
|
+ {ellipsisNode}
|
|
|
+ </Tooltip>
|
|
|
) : (
|
|
|
ellipsisNode
|
|
|
)}
|
|
|
<div
|
|
|
- className={classnames(props.titleClassName, Style['ellipsis-max'])}
|
|
|
+ className={classnames(props.titleClassName?.replace('ellipsis', ''), Style['ellipsis-max'])}
|
|
|
style={{ ...props.titleStyle, width: 'max-content !important' }}
|
|
|
ref={extraNode}
|
|
|
>
|