import React from 'react'; import style from './style'; import prefixer from '../utils/prefixer'; class ProgressBar extends React.Component { static propTypes = { 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 }; static defaultProps = { buffer: 0, className: '', max: 100, min: 0, mode: 'indeterminate', multicolor: false, type: 'linear', value: 0 }; 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); } circularStyle () { if (this.props.mode !== 'indeterminate') { return {strokeDasharray: `${2 * Math.PI * 25 * this.calculateRatio(this.props.value)}, 400`}; } } renderCircular () { return ( ); } 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 {}; } } renderLinear () { const {buffer, value} = this.linearStyle(); return (
); } render () { 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}`; if (this.props.className) className += ` ${this.props.className}`; return (
{ this.props.type === 'circular' ? this.renderCircular() : this.renderLinear() }
); } } export default ProgressBar;