Skip to content

Commit ddfb0b6

Browse files
author
昔梦
committed
fix:增加最佳实践案例
1 parent ef01d1a commit ddfb0b6

File tree

13 files changed

+1438
-2
lines changed

13 files changed

+1438
-2
lines changed

Diff for: docs/xflow/best-practices.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ group:
77
order: 3
88
---
99

10-
# 在项目中使用
10+
# 最佳实践
1111

1212

13+
## 案例1
1314
<code src="./demo/best/basic/index.tsx"></code>
1415

16+
## 案例2
17+
<code src="./demo/best/demo2/index.tsx"></code>
1518

1619

Diff for: docs/xflow/demo/best/basic/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Tools } from './tools';
1010
export default () => {
1111
return (
1212
<div style={{ height: '600px',position:'relative' }}>
13-
<Header data={ {}} />
13+
{/* <Header data={ {}} /> */}
1414
<XFlow
1515
initialValues={{ nodes, edges }}
1616
settings={settings}

Diff for: docs/xflow/demo/best/demo2/TextEllipsis/index.less

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.text-ellipsis {
2+
display: inline-block;
3+
max-width: 100%;
4+
overflow: hidden;
5+
white-space: nowrap;
6+
text-overflow: ellipsis;
7+
}
8+
9+
.paragraph-ellipsis {
10+
display: block;
11+
width: 100%;
12+
word-break: break-all;
13+
}

Diff for: docs/xflow/demo/best/demo2/TextEllipsis/index.tsx

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)