From 938f3fa278945f0496f5f6006daeb7259d591224 Mon Sep 17 00:00:00 2001 From: kongshan Date: Fri, 15 Jan 2021 16:33:22 +0800 Subject: [PATCH] feat: add Resize component and delete unnecessary code --- src/components/editCell/index.tsx | 2 +- src/components/ellipsisText/index.tsx | 87 ++++++++++++--------------- src/stories/ellipsisText.stories.tsx | 4 +- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/src/components/editCell/index.tsx b/src/components/editCell/index.tsx index 42e8c63f9..2047aa111 100644 --- a/src/components/editCell/index.tsx +++ b/src/components/editCell/index.tsx @@ -60,7 +60,7 @@ export default class EditCell extends React.PureComponent ) : ( <> - + { !isView && 修改 } diff --git a/src/components/ellipsisText/index.tsx b/src/components/ellipsisText/index.tsx index b450e9add..ffa2b6ff5 100644 --- a/src/components/ellipsisText/index.tsx +++ b/src/components/ellipsisText/index.tsx @@ -1,9 +1,10 @@ import React, { PureComponent } from "react"; import { Tooltip } from "antd"; +import Resize from '../resize'; export interface Props { - value: string ; - title?: string; + value: string | number ; + title?: string | number; className?: string; maxWidth?: string | number; [propName: string]: any; @@ -28,18 +29,7 @@ export default class EllipsisText extends PureComponent { }; componentDidMount() { - const { maxWidth } = this.props; this.onResize(); - if (!maxWidth) { - window.addEventListener("resize", this.onResize); - } - } - - componentWillUnmount() { - const { maxWidth } = this.props; - if (!maxWidth) { - window.removeEventListener("resize", this.onResize); - } } getRangeWidth = (ele: HTMLElement) => { @@ -51,7 +41,8 @@ export default class EllipsisText extends PureComponent { getStyle = (dom: NewHTMLElement, attr: string) => { // 兼容IE8 - const stylePadding = window?.getComputedStyle(dom)[attr] || dom.currentStyle[attr] + const stylePadding = + window?.getComputedStyle(dom)[attr] || dom.currentStyle[attr]; return stylePadding.slice(0, -2); }; @@ -62,18 +53,17 @@ export default class EllipsisText extends PureComponent { const { scrollWidth, offsetWidth, parentElement } = ele; if (scrollWidth === 0) { return this.getMaxWidth(parentElement); - } else { - const ellipsisNode = this.ellipsisRef; - ellipsisNode.style.display = "none"; - const rangeWidth = this.getRangeWidth(ele); - const ellipsisWidth = - offsetWidth - - rangeWidth - - this.getStyle(ele, "paddingRight") - - this.getStyle(ele, "paddingLeft"); - - return ellipsisWidth < 0 ? 0 : ellipsisWidth; } + const ellipsisNode = this.ellipsisRef; + ellipsisNode.style.display = "none"; + const rangeWidth = this.getRangeWidth(ele); + const ellipsisWidth = + offsetWidth - + rangeWidth - + this.getStyle(ele, "paddingRight") - + this.getStyle(ele, "paddingLeft"); + + return ellipsisWidth < 0 ? 0 : ellipsisWidth; }; onResize = () => { @@ -87,7 +77,6 @@ export default class EllipsisText extends PureComponent { actMaxWidth: ellipsisWidth, isEllipsis: rangeWidth > (maxWidth || ellipsisWidth) }); - }; handleVisibleChange = (visible: boolean) => { @@ -103,29 +92,31 @@ export default class EllipsisText extends PureComponent { const { title, value, className, maxWidth, ...other } = this.props; return ( - - (this.ellipsisRef = ref)} - className={className} - style={{ - whiteSpace: "nowrap", - overflow: "hidden", - textOverflow: "ellipsis", - display: "inline-block", - verticalAlign: "bottom", - minWidth: "2em", - maxWidth: maxWidth || actMaxWidth, - cursor: isEllipsis? 'pointer':'default' - }} + + - {value} - - + (this.ellipsisRef = ref)} + className={className} + style={{ + whiteSpace: "nowrap", + overflow: "hidden", + textOverflow: "ellipsis", + display: "inline-block", + verticalAlign: "bottom", + minWidth: "2em", + maxWidth: maxWidth || actMaxWidth, + cursor: isEllipsis ? "pointer" : "default" + }} + > + {value} + + + ); } } \ No newline at end of file diff --git a/src/stories/ellipsisText.stories.tsx b/src/stories/ellipsisText.stories.tsx index a62eeed6e..ca03d4873 100644 --- a/src/stories/ellipsisText.stories.tsx +++ b/src/stories/ellipsisText.stories.tsx @@ -10,7 +10,7 @@ stories.addDecorator(withKnobs) const propDefinitions = [{ property: 'value', - propType: 'string', + propType: 'string | number', required: true, description: '显示文本内容', defaultValue: '' @@ -28,7 +28,7 @@ const propDefinitions = [{ defaultValue: '' }, { property: 'title', - propType: 'string', + propType: 'string | number', required: false, description: '提示文字', defaultValue: ''