-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathProgressBarExamples.js
62 lines (50 loc) · 1.64 KB
/
ProgressBarExamples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { Component } from 'react';
import Button from '../../src/components/button';
import ProgressBar from '../../src/components/progress-bar';
class ProgressBarExamples extends Component {
constructor(props) {
super(props);
this.intervalId = null;
this.state = {
progress: 0,
};
}
setWidth = progress => () => this.setState({ progress });
startProgress = () => {
if (this.intervalId) {
return;
}
this.intervalId = setInterval(() => {
const increment = Math.random() * 10;
this.setState({
progress: this.state.progress + increment,
});
if (this.state.progress >= 100) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}, 750);
};
stopProgress = () => {
if (!this.intervalId) {
return;
}
clearInterval(this.intervalId);
this.intervalId = null;
};
render() {
const { progress } = this.state;
return (
<div>
<ProgressBar progress={progress} />
<Button onClick={this.startProgress}>Start</Button>
<Button onClick={this.stopProgress}>Stop</Button>
<Button onClick={this.setWidth(100)}>Complete</Button>
<Button onClick={this.setWidth(progress + 10)}>Increment by 10%</Button>
<Button onClick={this.setWidth(0)}>Reset</Button>
</div>
);
}
}
ProgressBarExamples.displayName = 'ProgressBarExamples';
export default ProgressBarExamples;