Skip to content

Commit 938f3fa

Browse files
author
kongshan
committed
feat: add Resize component and delete unnecessary code
1 parent 0e0d775 commit 938f3fa

File tree

3 files changed

+42
-51
lines changed

3 files changed

+42
-51
lines changed

src/components/editCell/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class EditCell extends React.PureComponent<EditCellProps, EditCel
6060
</div>
6161
) : (
6262
<>
63-
<EllipsisText value={editValue} />
63+
<EllipsisText value={editValue} maxWidth={120}/>
6464
{
6565
!isView && <a onClick={this.onEdit}>修改</a>
6666
}

src/components/ellipsisText/index.tsx

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React, { PureComponent } from "react";
22
import { Tooltip } from "antd";
3+
import Resize from '../resize';
34

45
export interface Props {
5-
value: string ;
6-
title?: string;
6+
value: string | number ;
7+
title?: string | number;
78
className?: string;
89
maxWidth?: string | number;
910
[propName: string]: any;
@@ -28,18 +29,7 @@ export default class EllipsisText extends PureComponent<Props, State> {
2829
};
2930

3031
componentDidMount() {
31-
const { maxWidth } = this.props;
3232
this.onResize();
33-
if (!maxWidth) {
34-
window.addEventListener("resize", this.onResize);
35-
}
36-
}
37-
38-
componentWillUnmount() {
39-
const { maxWidth } = this.props;
40-
if (!maxWidth) {
41-
window.removeEventListener("resize", this.onResize);
42-
}
4333
}
4434

4535
getRangeWidth = (ele: HTMLElement) => {
@@ -51,7 +41,8 @@ export default class EllipsisText extends PureComponent<Props, State> {
5141

5242
getStyle = (dom: NewHTMLElement, attr: string) => {
5343
// 兼容IE8
54-
const stylePadding = window?.getComputedStyle(dom)[attr] || dom.currentStyle[attr]
44+
const stylePadding =
45+
window?.getComputedStyle(dom)[attr] || dom.currentStyle[attr];
5546

5647
return stylePadding.slice(0, -2);
5748
};
@@ -62,18 +53,17 @@ export default class EllipsisText extends PureComponent<Props, State> {
6253
const { scrollWidth, offsetWidth, parentElement } = ele;
6354
if (scrollWidth === 0) {
6455
return this.getMaxWidth(parentElement);
65-
} else {
66-
const ellipsisNode = this.ellipsisRef;
67-
ellipsisNode.style.display = "none";
68-
const rangeWidth = this.getRangeWidth(ele);
69-
const ellipsisWidth =
70-
offsetWidth -
71-
rangeWidth -
72-
this.getStyle(ele, "paddingRight") -
73-
this.getStyle(ele, "paddingLeft");
74-
75-
return ellipsisWidth < 0 ? 0 : ellipsisWidth;
7656
}
57+
const ellipsisNode = this.ellipsisRef;
58+
ellipsisNode.style.display = "none";
59+
const rangeWidth = this.getRangeWidth(ele);
60+
const ellipsisWidth =
61+
offsetWidth -
62+
rangeWidth -
63+
this.getStyle(ele, "paddingRight") -
64+
this.getStyle(ele, "paddingLeft");
65+
66+
return ellipsisWidth < 0 ? 0 : ellipsisWidth;
7767
};
7868

7969
onResize = () => {
@@ -87,7 +77,6 @@ export default class EllipsisText extends PureComponent<Props, State> {
8777
actMaxWidth: ellipsisWidth,
8878
isEllipsis: rangeWidth > (maxWidth || ellipsisWidth)
8979
});
90-
9180
};
9281

9382
handleVisibleChange = (visible: boolean) => {
@@ -103,29 +92,31 @@ export default class EllipsisText extends PureComponent<Props, State> {
10392
const { title, value, className, maxWidth, ...other } = this.props;
10493

10594
return (
106-
<Tooltip
107-
title={title || value}
108-
visible={visible}
109-
onVisibleChange={this.handleVisibleChange}
110-
{...other}
111-
>
112-
<span
113-
ref={(ref) => (this.ellipsisRef = ref)}
114-
className={className}
115-
style={{
116-
whiteSpace: "nowrap",
117-
overflow: "hidden",
118-
textOverflow: "ellipsis",
119-
display: "inline-block",
120-
verticalAlign: "bottom",
121-
minWidth: "2em",
122-
maxWidth: maxWidth || actMaxWidth,
123-
cursor: isEllipsis? 'pointer':'default'
124-
}}
95+
<Resize onResize={maxWidth ? null:this.onResize}>
96+
<Tooltip
97+
title={title || value}
98+
visible={visible}
99+
onVisibleChange={this.handleVisibleChange}
100+
{...other}
125101
>
126-
{value}
127-
</span>
128-
</Tooltip>
102+
<span
103+
ref={(ref) => (this.ellipsisRef = ref)}
104+
className={className}
105+
style={{
106+
whiteSpace: "nowrap",
107+
overflow: "hidden",
108+
textOverflow: "ellipsis",
109+
display: "inline-block",
110+
verticalAlign: "bottom",
111+
minWidth: "2em",
112+
maxWidth: maxWidth || actMaxWidth,
113+
cursor: isEllipsis ? "pointer" : "default"
114+
}}
115+
>
116+
{value}
117+
</span>
118+
</Tooltip>
119+
</Resize>
129120
);
130121
}
131122
}

src/stories/ellipsisText.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ stories.addDecorator(withKnobs)
1010

1111
const propDefinitions = [{
1212
property: 'value',
13-
propType: 'string',
13+
propType: 'string | number',
1414
required: true,
1515
description: '显示文本内容',
1616
defaultValue: ''
@@ -28,7 +28,7 @@ const propDefinitions = [{
2828
defaultValue: ''
2929
}, {
3030
property: 'title',
31-
propType: 'string',
31+
propType: 'string | number',
3232
required: false,
3333
description: '提示文字',
3434
defaultValue: ''

0 commit comments

Comments
 (0)