83 lines
2 KiB
JavaScript
83 lines
2 KiB
JavaScript
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
|
|
function ActiveBar () {
|
|
return <span className="progress-bar progress-bar--active">| </span>;
|
|
}
|
|
|
|
function InactiveBar () {
|
|
return <span className="progress-bar progress-bar--inactive">| </span>;
|
|
}
|
|
|
|
class ProgressBar extends React.Component {
|
|
constructor (props) {
|
|
super(props);
|
|
this.state = {
|
|
size : 12,
|
|
bars : [],
|
|
index : 0,
|
|
incrementer: 1,
|
|
};
|
|
this.createBars = this.createBars.bind(this);
|
|
this.startProgressBar = this.startProgressBar.bind(this);
|
|
this.updateProgressBar = this.updateProgressBar.bind(this);
|
|
this.stopProgressBar = this.stopProgressBar.bind(this);
|
|
}
|
|
componentDidMount () {
|
|
this.createBars();
|
|
this.startProgressBar();
|
|
}
|
|
componentWillUnmount () {
|
|
this.stopProgressBar();
|
|
}
|
|
createBars () {
|
|
const bars = [];
|
|
for (let i = 0; i <= this.props.size; i++) {
|
|
bars.push({isActive: false});
|
|
}
|
|
this.setState({ bars });
|
|
}
|
|
startProgressBar () {
|
|
this.updateInterval = setInterval(this.updateProgressBar.bind(this), 300);
|
|
};
|
|
updateProgressBar () {
|
|
let index = this.state.index;
|
|
let incrementer = this.state.incrementer;
|
|
let bars = this.state.bars;
|
|
// flip incrementer if necessary, to stay in bounds
|
|
if ((index < 0) || (index > this.props.size)) {
|
|
incrementer = incrementer * -1;
|
|
index += incrementer;
|
|
}
|
|
// update the indexed bar
|
|
if (incrementer > 0) {
|
|
bars[index].isActive = true;
|
|
} else {
|
|
bars[index].isActive = false;
|
|
};
|
|
// increment index
|
|
index += incrementer;
|
|
// update state
|
|
this.setState({
|
|
bars,
|
|
incrementer,
|
|
index,
|
|
});
|
|
};
|
|
stopProgressBar () {
|
|
clearInterval(this.updateInterval);
|
|
};
|
|
render () {
|
|
return (
|
|
<div>
|
|
{this.state.bars.map((bar, index) => bar.isActive ? <ActiveBar key={index} /> : <InactiveBar key={index}/> )}
|
|
</div>
|
|
);
|
|
}
|
|
};
|
|
|
|
ProgressBar.propTypes = {
|
|
size: PropTypes.number.isRequired,
|
|
};
|
|
|
|
export default ProgressBar;
|