2017-04-17 17:14:17 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-05-22 22:50:22 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2017-01-26 20:05:32 +03:00
|
|
|
import { PROGRESS_BAR } from '../identifiers';
|
|
|
|
import prefixer from '../utils/prefixer';
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-28 20:37:46 +03:00
|
|
|
class ProgressBar extends Component {
|
2015-11-22 23:41:28 +03:00
|
|
|
static propTypes = {
|
2016-05-28 20:37:46 +03:00
|
|
|
buffer: PropTypes.number,
|
|
|
|
className: PropTypes.string,
|
2016-10-19 15:20:07 +03:00
|
|
|
disabled: PropTypes.bool,
|
2016-05-28 20:37:46 +03:00
|
|
|
max: PropTypes.number,
|
|
|
|
min: PropTypes.number,
|
|
|
|
mode: PropTypes.oneOf(['determinate', 'indeterminate']),
|
|
|
|
multicolor: PropTypes.bool,
|
|
|
|
theme: PropTypes.shape({
|
|
|
|
buffer: PropTypes.string,
|
|
|
|
circle: PropTypes.string,
|
|
|
|
circular: PropTypes.string,
|
|
|
|
indeterminate: PropTypes.string,
|
|
|
|
linear: PropTypes.string,
|
|
|
|
multicolor: PropTypes.string,
|
|
|
|
path: PropTypes.string,
|
2017-01-26 20:05:32 +03:00
|
|
|
value: PropTypes.string,
|
2016-05-22 22:50:22 +03:00
|
|
|
}),
|
2016-05-28 20:37:46 +03:00
|
|
|
type: PropTypes.oneOf(['linear', 'circular']),
|
2017-01-26 20:05:32 +03:00
|
|
|
value: PropTypes.number,
|
2015-11-22 23:41:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
buffer: 0,
|
|
|
|
className: '',
|
|
|
|
max: 100,
|
|
|
|
min: 0,
|
|
|
|
mode: 'indeterminate',
|
|
|
|
multicolor: false,
|
|
|
|
type: 'linear',
|
2017-01-26 20:05:32 +03:00
|
|
|
value: 0,
|
2015-11-22 23:41:28 +03:00
|
|
|
};
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
calculateRatio(value) {
|
2015-11-22 23:41:28 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
circularStyle() {
|
|
|
|
return this.props.mode !== 'indeterminate'
|
|
|
|
? { strokeDasharray: `${2 * Math.PI * 25 * this.calculateRatio(this.props.value)}, 400` }
|
|
|
|
: undefined;
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
linearStyle() {
|
2015-11-22 23:41:28 +03:00
|
|
|
if (this.props.mode !== 'indeterminate') {
|
|
|
|
return {
|
2017-01-26 20:05:32 +03:00
|
|
|
buffer: prefixer({ transform: `scaleX(${this.calculateRatio(this.props.buffer)})` }),
|
|
|
|
value: prefixer({ transform: `scaleX(${this.calculateRatio(this.props.value)})` }),
|
2015-11-22 23:41:28 +03:00
|
|
|
};
|
|
|
|
}
|
2017-01-26 20:05:32 +03:00
|
|
|
return {};
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
renderCircular() {
|
2015-11-22 23:41:28 +03:00
|
|
|
return (
|
2016-08-04 07:32:40 +03:00
|
|
|
<svg className={this.props.theme.circle} viewBox="0 0 60 60">
|
2017-01-26 20:05:32 +03:00
|
|
|
<circle className={this.props.theme.path} style={this.circularStyle()} cx="30" cy="30" r="25" />
|
2015-11-22 23:41:28 +03:00
|
|
|
</svg>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
renderLinear() {
|
|
|
|
const { buffer, value } = this.linearStyle();
|
2015-11-22 23:41:28 +03:00
|
|
|
return (
|
|
|
|
<div>
|
2017-01-26 20:05:32 +03:00
|
|
|
<span data-ref="buffer" className={this.props.theme.buffer} style={buffer} />
|
|
|
|
<span data-ref="value" className={this.props.theme.value} style={value} />
|
2015-11-22 23:41:28 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
render() {
|
2016-10-19 15:20:07 +03:00
|
|
|
const { className, disabled, max, min, mode, multicolor, type, theme, value } = this.props;
|
2016-05-22 22:50:22 +03:00
|
|
|
const _className = classnames(theme[type], {
|
|
|
|
[theme[mode]]: mode,
|
2017-01-26 20:05:32 +03:00
|
|
|
[theme.multicolor]: multicolor,
|
2016-05-22 22:50:22 +03:00
|
|
|
}, className);
|
2015-11-22 23:41:28 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2016-10-19 15:20:07 +03:00
|
|
|
disabled={disabled}
|
2017-01-26 20:05:32 +03:00
|
|
|
data-react-toolbox="progress-bar"
|
2016-05-22 22:50:22 +03:00
|
|
|
aria-valuenow={value}
|
|
|
|
aria-valuemin={min}
|
|
|
|
aria-valuemax={max}
|
|
|
|
className={_className}
|
2015-11-22 23:41:28 +03:00
|
|
|
>
|
2016-05-22 22:50:22 +03:00
|
|
|
{type === 'circular' ? this.renderCircular() : this.renderLinear()}
|
2015-11-22 23:41:28 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 20:37:46 +03:00
|
|
|
export default themr(PROGRESS_BAR)(ProgressBar);
|
2016-05-25 01:25:43 +03:00
|
|
|
export { ProgressBar };
|