Skip to content

Commit f5b01db

Browse files
author
Minh Tran
committed
Create component
1 parent 33173cd commit f5b01db

File tree

4 files changed

+215
-16
lines changed

4 files changed

+215
-16
lines changed

example/app.js

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,85 @@
11
var React = require('react');
22

33
require('./bower_components/bootstrap-customize/css/bootstrap.css');
4+
require('../src/progress-bar.scss');
45
require('./assets/styles/app.scss');
56

67
var Header = require('./components/Header');
78
var Footer = require('./components/Footer');
8-
var Component = require('../src/Component');
9+
var ProgressBar = require('../src/ProgressBar');
910

1011
var App = React.createClass({
12+
getInitialState: function () {
13+
return {
14+
percent: -1,
15+
autoIncrement: false,
16+
intervalTime: 200
17+
};
18+
},
19+
setPercent: function (percent) {
20+
return function () {
21+
this.setState({
22+
percent: percent
23+
});
24+
}.bind(this);
25+
},
26+
startWithAutoIncrement: function () {
27+
this.setState({
28+
percent: 0,
29+
autoIncrement: true,
30+
intervalTime: (Math.random() * 1000)
31+
});
32+
},
1133
render: function () {
1234
return (
13-
<div className={"layout-page"}>
35+
<div className={'layout-page'}>
1436
<Header/>
15-
<main className={"layout-main"}>
16-
<div className={"container"}>
17-
<Component/>
37+
<main className={'layout-main'}>
38+
<div className={'container'}>
39+
<ProgressBar percent={this.state.percent}
40+
autoIncrement={this.state.autoIncrement}
41+
intervalTime={this.state.intervalTime}/>
42+
43+
<div className='text-center'>
44+
<div className='btn-group'>
45+
<button className='btn btn-default'
46+
onClick={this.setPercent(0)}>
47+
Start
48+
</button>
49+
<button className='btn btn-default'
50+
onClick={this.setPercent(25)}>
51+
Set 25
52+
</button>
53+
<button className='btn btn-default'
54+
onClick={this.setPercent(50)}>
55+
Set 50
56+
</button>
57+
<button className='btn btn-default'
58+
onClick={this.setPercent(75)}>
59+
Set 75
60+
</button>
61+
<button className='btn btn-default'
62+
onClick={this.setPercent(100)}>Finish
63+
</button>
64+
</div>
65+
</div>
66+
<hr/>
67+
<div className='text-center'>
68+
<h4>
69+
Current intervalTime: <code>{this.state.intervalTime}</code>
70+
</h4>
71+
72+
<div className='btn-group'>
73+
<button className='btn btn-default'
74+
onClick={this.startWithAutoIncrement}>
75+
Start with auto increment
76+
</button>
77+
<button className='btn btn-default'
78+
onClick={this.setPercent(100)}>
79+
Finish
80+
</button>
81+
</div>
82+
</div>
1883
</div>
1984
</main>
2085
<Footer/>

src/Component.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/ProgressBar.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var React = require('react');
2+
3+
var ProgressBar = React.createClass({
4+
propTypes: {
5+
percent: React.PropTypes.number.isRequired,
6+
onTop: React.PropTypes.bool,
7+
autoIncrement: React.PropTypes.bool,
8+
intervalTime: React.PropTypes.number
9+
},
10+
getDefaultProps: function () {
11+
return {
12+
percent: -1,
13+
onTop: false,
14+
autoIncrement: false,
15+
intervalTime: 200
16+
};
17+
},
18+
getInitialState: function () {
19+
return {
20+
percent: this.props.percent
21+
};
22+
},
23+
increment: function () {
24+
var percent = this.state.percent + (Math.random() + 1 - Math.random());
25+
percent = percent < 99 ? percent : 99;
26+
this.setState({
27+
percent: percent
28+
});
29+
},
30+
componentWillUnmount: function () {
31+
if (this.interval) {
32+
clearInterval(this.interval);
33+
}
34+
},
35+
componentWillReceiveProps: function (nextProps) {
36+
if (this.interval) {
37+
clearInterval(this.interval);
38+
}
39+
40+
if (nextProps.autoIncrement && nextProps.percent >= 0 && nextProps.percent < 99) {
41+
this.interval = setInterval(this.increment, nextProps.intervalTime);
42+
}
43+
44+
if (nextProps.percent >= 100) {
45+
this.setState({
46+
percent: 99.9
47+
}, function () {
48+
setTimeout(function () {
49+
this.setState({
50+
percent: 100
51+
});
52+
}.bind(this), 400);
53+
});
54+
} else {
55+
this.setState({
56+
percent: nextProps.percent
57+
});
58+
}
59+
},
60+
render: function () {
61+
var className = 'react-progress-bar' + (this.props.onTop ? ' react-progress-bar-on-top' : '');
62+
if (this.state.percent < 0 || this.state.percent >= 100) {
63+
className += ' react-progress-bar-hide';
64+
}
65+
var style = {width: this.state.percent + '%'};
66+
return (
67+
<div className={className}>
68+
<div className='react-progress-bar-percent' style={style}/>
69+
<div className='react-progress-bar-spinner'>
70+
<div className='react-progress-bar-spinner-icon'></div>
71+
</div>
72+
</div>
73+
);
74+
}
75+
});
76+
77+
module.exports = ProgressBar;

src/progress-bar.scss

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
@-webkit-keyframes react-progress-spinner {
2+
0% {
3+
-webkit-transform: rotate(0deg);
4+
}
5+
100% {
6+
-webkit-transform: rotate(360deg);
7+
}
8+
}
9+
10+
@keyframes react-progress-spinner {
11+
0% {
12+
transform: rotate(0deg);
13+
}
14+
100% {
15+
transform: rotate(360deg);
16+
}
17+
}
18+
19+
.react-progress-bar {
20+
position: fixed;
21+
top: 0;
22+
left: 0;
23+
width: 100%;
24+
visibility: visible;
25+
opacity: 1;
26+
transition: all 400ms;
27+
z-index: 9999999;
28+
&.react-progress-bar-on-top {
29+
height: 100%;
30+
}
31+
&.react-progress-bar-hide {
32+
opacity: 0;
33+
visibility: hidden;
34+
z-index: -10;
35+
.react-progress-bar-percent {
36+
width: 0;
37+
}
38+
}
39+
}
40+
41+
.react-progress-bar-percent {
42+
height: 2px;
43+
background: #29D;
44+
box-shadow: 0 0 10px #29D, 0 0 5px #29D;
45+
transition: all 200ms ease;
46+
width: 5px;
47+
}
48+
49+
.react-progress-bar-spinner {
50+
display: block;
51+
position: fixed;
52+
top: 15px;
53+
left: 15px;
54+
}
55+
56+
.react-progress-bar-spinner-icon {
57+
width: 18px;
58+
height: 18px;
59+
box-sizing: border-box;
60+
61+
border: solid 2px transparent;
62+
border-top-color: #29d;
63+
border-left-color: #29d;
64+
border-radius: 50%;
65+
66+
-webkit-animation: react-progress-spinner 400ms linear infinite;
67+
animation: react-progress-spinner 400ms linear infinite;
68+
}

0 commit comments

Comments
 (0)