Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit 9911f15

Browse files
committed
✨ feat: 新增 loading 状态控制 props
1 parent 881ca57 commit 9911f15

File tree

6 files changed

+94
-16
lines changed

6 files changed

+94
-16
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { useState } from 'react';
2+
import { Button, Card } from 'antd';
3+
4+
import PageLoading from '@arvinxu/page-loading';
5+
6+
const FullScreenLoading = () => {
7+
const [fullscreen, setFullScreen] = useState(false);
8+
9+
return (
10+
<div>
11+
{fullscreen ? (
12+
<Button style={{ zIndex: 100000 }} onClick={() => setFullScreen(false)}>
13+
取消全屏
14+
</Button>
15+
) : (
16+
<Button onClick={() => setFullScreen(true)}>全屏</Button>
17+
)}
18+
19+
<div style={{ height: 300, marginTop: 16 }}>
20+
<PageLoading fullscreen={fullscreen} id={'fullscreen'}>
21+
<Card>内容卡片</Card>
22+
</PageLoading>
23+
</div>
24+
</div>
25+
);
26+
};
27+
28+
export default FullScreenLoading;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { useState } from 'react';
2+
import { Button, Card } from 'antd';
3+
4+
import PageLoading from '@arvinxu/page-loading';
5+
6+
const { Group } = Button;
7+
const Loading = () => {
8+
const [loading, setLoading] = useState(false);
9+
10+
return (
11+
<div>
12+
<Group>
13+
<Button onClick={() => setLoading(true)}>加载中</Button>
14+
<Button onClick={() => setLoading(false)}>加载完成</Button>
15+
</Group>
16+
<span style={{ marginLeft: 8 }}>
17+
状态: {loading ? '加载中' : '加载完成'}
18+
</span>
19+
<div style={{ height: 300, marginTop: 16 }}>
20+
<PageLoading loading={loading} id={'loading'}>
21+
<Card>内容卡片</Card>
22+
</PageLoading>
23+
</div>
24+
</div>
25+
);
26+
};
27+
28+
export default Loading;

docs/components/common/page-loading.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ group:
2626

2727
### 全屏加载
2828

29-
TODO
29+
<code src="./examples/PageLoading/Fullscreen" />
3030

3131
### 加载状态控制
3232

33-
TODO
33+
<code src="./examples/PageLoading/LoadingState" />
3434

3535
###
3636

packages/page-loading/src/index.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CSSProperties } from 'react';
1+
import type { CSSProperties, FC } from 'react';
22
import React from 'react';
33
import classNames from 'classnames';
44

@@ -11,46 +11,59 @@ const clsPrefix = 'avx-page-loading';
1111

1212
export interface PageLoadingProps {
1313
/**
14-
* 是否全屏
14+
* 是否全屏(可选)
1515
* @default false
1616
*/
1717
fullscreen?: boolean;
1818

1919
/**
20-
* 是否显示加载进度条
20+
* 显示加载进度条(可选)
2121
* @default true
2222
*/
2323
progress?: boolean;
2424

2525
/**
26-
* 主色
26+
* 主色(可选)
2727
* @default #1890ff
2828
*/
2929
color?: string;
3030
/**
31-
* 背景颜色
31+
* 背景颜色(可选)
3232
* @default #f3f4f6
3333
*/
3434
backgroundColor?: string;
3535

3636
/**
37-
* 进度条 id。如果存在多个 PageLoading 组件,需要指定 id 以区分进度条
37+
* 进度条 id。(可选)
38+
* 如果存在多个 PageLoading 组件,需要指定 id 以区分进度条
39+
*
3840
*/
3941
id?: string;
42+
43+
/**
44+
* 加载状态
45+
*/
46+
loading?: boolean;
4047
}
4148

42-
const PageLoading: React.FC<PageLoadingProps> = ({
43-
progress,
49+
const PageLoading: FC<PageLoadingProps> = ({
50+
progress = true,
4451
fullscreen,
4552
color = '#1890ff',
4653
id = 'avx-page-loading-container',
4754
backgroundColor = '#f5f5f5',
55+
loading = true,
56+
children,
4857
}) => {
4958
// 控制 Progress 显示
50-
useProgress(id, color, progress);
59+
useProgress(id, { color, progress, loading, fullscreen });
5160

5261
const colorStyle: CSSProperties = { background: color };
5362

63+
// 如果明确指定了 loading 为 false
64+
// 返回 children
65+
if (typeof loading === 'boolean' && !loading) return <>{children}</>;
66+
5467
return (
5568
<div
5669
id={id}
@@ -81,4 +94,5 @@ const PageLoading: React.FC<PageLoadingProps> = ({
8194
</div>
8295
);
8396
};
97+
8498
export default PageLoading;

packages/page-loading/src/style.less

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
}
1515

1616
&-fullscreen {
17-
position: absolute;
17+
position: fixed;
1818
top: 0;
1919
right: 0;
2020
bottom: 0;
2121
left: 0;
22-
z-index: 1000;
22+
z-index: 9900;
2323
}
2424

2525
&-content {

packages/page-loading/src/useProgress.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ const progressColor = (color) => `
1616
border-left-color: ${color};
1717
}`;
1818

19-
export const useProgress = (container, color, enable = true) => {
19+
export const useProgress = (container, config) => {
20+
const { color, enable = true, loading = true, fullscreen } = config;
2021
// 控制 Progress 显示
2122
useEffect(() => {
23+
if (typeof loading === 'boolean' && !loading) return;
24+
2225
// 如果传入 progress props 且为 false
2326
if (typeof enable === 'boolean' && !enable) return;
2427

@@ -33,9 +36,14 @@ export const useProgress = (container, color, enable = true) => {
3336
np.inc();
3437

3538
return () => {
36-
np.done();
39+
try {
40+
np.done();
41+
// eslint-disable-next-line no-empty
42+
} catch (e) {
43+
np.remove();
44+
}
3745
};
38-
}, [container, enable]);
46+
}, [container, enable, loading, fullscreen]);
3947

4048
// 控制 progress 颜色
4149
useEffect(() => {

0 commit comments

Comments
 (0)