react-toolbox/components/progress_bar/index.jsx

90 lines
2.4 KiB
React
Raw Normal View History

import React from 'react';
2015-10-09 22:02:12 +03:00
import style from './style';
import prefixer from '../utils/prefixer';
2015-10-22 02:31:17 +03:00
class ProgressBar extends React.Component {
static propTypes = {
2015-09-08 02:05:08 +03:00
buffer: React.PropTypes.number,
className: React.PropTypes.string,
max: React.PropTypes.number,
min: React.PropTypes.number,
mode: React.PropTypes.string,
multicolor: React.PropTypes.bool,
type: React.PropTypes.string,
value: React.PropTypes.number
};
2015-09-08 02:05:08 +03:00
static defaultProps = {
buffer: 0,
className: '',
max: 100,
min: 0,
mode: 'indeterminate',
multicolor: false,
type: 'linear',
value: 0
};
2015-09-08 02:05:08 +03:00
calculateRatio (value) {
if (value < this.props.min) return 0;
if (value > this.props.max) return 1;
return (value - this.props.min) / (this.props.max - this.props.min);
}
2015-09-08 02:05:08 +03:00
circularStyle () {
if (this.props.mode !== 'indeterminate') {
return {strokeDasharray: `${2 * Math.PI * 25 * this.calculateRatio(this.props.value)}, 400`};
}
}
2015-09-08 02:05:08 +03:00
linearStyle () {
if (this.props.mode !== 'indeterminate') {
return {
buffer: prefixer({transform: `scaleX(${this.calculateRatio(this.props.buffer)})`}),
value: prefixer({transform: `scaleX(${this.calculateRatio(this.props.value)})`})
};
} else {
return {};
}
}
2015-09-08 02:05:08 +03:00
renderCircular () {
return (
<svg className={style.circle}>
<circle className={style.path} style={this.circularStyle()} cx='30' cy='30' r='25' />
</svg>
);
}
2015-09-08 02:05:08 +03:00
renderLinear () {
const {buffer, value} = this.linearStyle();
return (
<div>
2015-10-09 22:02:12 +03:00
<span ref='buffer' data-ref='buffer' className={style.buffer} style={buffer}></span>
<span ref='value' data-ref='value' className={style.value} style={value}></span>
2015-09-08 02:05:08 +03:00
</div>
);
}
2015-09-08 02:05:08 +03:00
render () {
2015-10-09 22:02:12 +03:00
let className = this.props.type === 'linear' ? style.linear : style.circular;
if (this.props.mode) className += ` ${style[this.props.mode]}`;
if (this.props.multicolor) className += ` ${style.multicolor}`;
2015-09-08 02:05:08 +03:00
if (this.props.className) className += ` ${this.props.className}`;
return (
<div
2015-10-06 05:42:56 +03:00
data-react-toolbox='progress-bar'
2015-09-08 02:05:08 +03:00
aria-valuenow={this.props.value}
aria-valuemin={this.props.min}
2015-10-06 05:42:56 +03:00
aria-valuemax={this.props.max}
className={className}
2015-10-06 05:42:56 +03:00
>
{ this.props.type === 'circular' ? this.renderCircular() : this.renderLinear() }
2015-09-08 02:05:08 +03:00
</div>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default ProgressBar;