Skip to content

Commit 62b48cb

Browse files
authored
Feat progress bar (#320)
* feat: optimize progress bar * docs: basic use * feat: nodeJS.Timeout --------- Co-authored-by: liuyi <> Co-authored-by: liuyi <[email protected]>
1 parent 7b086ad commit 62b48cb

File tree

4 files changed

+71
-41
lines changed

4 files changed

+71
-41
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import '@testing-library/jest-dom/extend-expect';
2+
import ProgressBar from '..';
3+
4+
describe('test ProgressBar', () => {
5+
const progressBar = ProgressBar;
6+
7+
test('show method should add progress bar and loading image to the DOM', () => {
8+
progressBar.show();
9+
// show 方法内有延时
10+
setTimeout(() => {
11+
expect(progressBar.hasAdded()).toBe(true);
12+
}, 200);
13+
});
14+
15+
test('hide method should remove progress bar and loading image from the DOM', () => {
16+
progressBar.show();
17+
progressBar.hide();
18+
expect(progressBar.hasAdded()).toBe(false);
19+
});
20+
});

src/progressBar/index.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ demo:
1717
```jsx
1818
/**
1919
* iframe: "true"
20+
* title: "基础使用"
2021
*/
2122
import React from 'react';
2223
import { ProgressBar } from 'dt-react-component';
@@ -26,14 +27,15 @@ export default () => {
2627
return (
2728
<>
2829
<Button
30+
style={{ margin: '40px 10px 0 20px' }}
31+
type="primary"
2932
onClick={() => {
3033
ProgressBar.show();
3134
}}
3235
>
3336
发起请求
3437
</Button>
3538
<Button
36-
style={{ marginLeft: '10px' }}
3739
onClick={() => {
3840
ProgressBar.hide();
3941
}}
@@ -44,3 +46,10 @@ export default () => {
4446
);
4547
};
4648
```
49+
50+
## API
51+
52+
| 参数 | 说明 | 类型 | 默认值 |
53+
| ---- | ---------- | ---------- | ------ |
54+
| show | 显示进度条 | `Function` | - |
55+
| hide | 隐藏进度条 | `Function` | - |

src/progressBar/index.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import './style.scss';
22

33
class ProgressBar {
4-
_clock: any;
4+
// 定时器
5+
_timer: NodeJS.Timeout | null;
6+
// 计数,小于等于 0 时表示需要清除进度条
57
_count: number;
6-
className: string;
8+
// 进度条
79
img: HTMLDivElement;
8-
hodor: HTMLDivElement;
10+
// loading 图片
11+
bar: HTMLDivElement;
12+
913
constructor() {
10-
this._clock = null;
14+
this._timer = null;
1115
this._count = 0;
12-
this.className = 'dtc-progress-progress-bar';
13-
this.hodor = document.createElement('div');
14-
this.hodor.className = this.className;
16+
17+
this.bar = document.createElement('div');
18+
this.bar.className = 'dtc-progress-bar';
1519

1620
this.img = document.createElement('div');
17-
this.img.className = 'dtc-progress-progress-img';
21+
this.img.className = 'dtc-progress-img';
1822
this.img.innerHTML = `<svg version="1.1" id="loader-1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
1923
width="30px" height="30px" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50;" xml:space="preserve">
2024
<path fill="#2491F7" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z">
@@ -31,9 +35,9 @@ class ProgressBar {
3135

3236
show() {
3337
this._count++;
34-
if (!this.hasAdded() && !this._clock) {
35-
this._clock = setTimeout(() => {
36-
document.body.appendChild(this.hodor);
38+
if (!this.hasAdded() && !this._timer) {
39+
this._timer = setTimeout(() => {
40+
document.body.appendChild(this.bar);
3741
document.body.appendChild(this.img);
3842
}, 200);
3943
}
@@ -42,19 +46,20 @@ class ProgressBar {
4246
hide() {
4347
this._count--;
4448
if (this._count <= 0) {
45-
if (this._clock) {
46-
clearTimeout(this._clock);
47-
this._clock = null;
49+
if (this._timer) {
50+
clearTimeout(this._timer);
51+
this._timer = null;
4852
}
4953
if (this.hasAdded()) {
50-
this.hodor.remove();
54+
this.bar.remove();
5155
this.img.remove();
5256
}
5357
}
5458
}
5559

5660
hasAdded() {
57-
return document.getElementsByClassName(this.className).length > 0;
61+
return document.getElementsByClassName('dtc-progress-bar').length > 0;
5862
}
5963
}
64+
6065
export default new ProgressBar();

src/progressBar/style.scss

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
$prefix: "dtc-progress";
2-
.#{$prefix}-progress-bar {
3-
position: fixed;
4-
top: 0;
5-
left: -100%;
6-
z-index: 10000;
7-
width: 100%;
8-
height: 1px;
9-
background: #2491F7;
10-
box-shadow: 0 1px 3px #2491F7;
11-
animation: dtc-progress-loading-anim 1s ease-in-out infinite;
12-
}
13-
14-
.#{$prefix}-progress-img {
15-
position: fixed;
16-
top: 0;
17-
left: 0;
18-
width: 30px;
19-
height: 30px;
20-
z-index: 10000;
21-
}
222

23-
@keyframes dtc-progress-loading-anim {
24-
to {
25-
transform: translateX(200%);
3+
.#{$prefix} {
4+
&-bar {
5+
position: fixed;
6+
top: 0;
7+
left: -100%;
8+
z-index: 10000;
9+
width: 100%;
10+
height: 1px;
11+
background: #2491F7;
12+
box-shadow: 0 1px 3px #2491F7;
13+
animation: dtc-progress-loading 1s ease-in-out infinite;
14+
}
15+
&-img {
16+
position: fixed;
17+
top: 0;
18+
left: 0;
19+
width: 30px;
20+
height: 30px;
21+
z-index: 10000;
2622
}
2723
}
2824

29-
@keyframes dtc-progress-loading-anim {
25+
@keyframes dtc-progress-loading {
3026
to {
3127
transform: translateX(200%);
3228
}

0 commit comments

Comments
 (0)