/* @flow */ import React from 'react'; import { Component, PropTypes } from '../../libs'; type PathStyle = { strokeDasharray: string, strokeDashoffset: string, transition: string }; export default class Progress extends Component { static defaultProps = { type: 'line', percentage: 0, strokeWidth: 6, width: 126, showText: true, textInside: false }; constructor(props: Object) { super(props); } relativeStrokeWidth(): string { const { strokeWidth, width } = this.props; return (strokeWidth / width * 100).toFixed(1); } trackPath(): string { const radius = parseInt( 50 - parseFloat(this.relativeStrokeWidth()) / 2, 10 ); return `M 50 50 m 0 -${radius} a ${radius} ${radius} 0 1 1 0 ${radius * 2} a ${radius} ${radius} 0 1 1 0 -${radius * 2}`; } perimeter(): number { const radius = 50 - parseFloat(this.relativeStrokeWidth()) / 2; return 2 * Math.PI * radius; } circlePathStyle(): PathStyle { const perimeter = this.perimeter(); return { strokeDasharray: `${perimeter}px,${perimeter}px`, strokeDashoffset: (1 - this.props.percentage / 100) * perimeter + 'px', transition: 'stroke-dashoffset 0.6s ease 0s, stroke 0.6s ease' }; } stroke(): string { let ret; switch (this.props.status) { case 'success': ret = '#13ce66'; break; case 'exception': ret = '#ff4949'; break; default: ret = '#20a0ff'; } return ret; } iconClass(): string { const { type, status } = this.props; return type === 'line' ? status === 'success' ? 'el-icon-circle-check' : 'el-icon-circle-cross' : status === 'success' ? 'el-icon-check' : 'el-icon-close'; } progressTextSize(): number { const { type, strokeWidth, width } = this.props; return type === 'line' ? 12 + strokeWidth * 0.4 : width * 0.111111 + 2; } render(): React.DOM { const { type, percentage, status, strokeWidth, textInside, width, showText } = this.props; let progress; if (type === 'line') { progress = (
{showText && textInside &&
{`${percentage}%`}
}
); } else { progress = (
); } const progressInfo = showText && !textInside &&
{status ? : `${percentage}%`}
; return (
{progress} {progressInfo}
); } } Progress.propTypes = { type: PropTypes.oneOf(['line', 'circle']), percentage: PropTypes.range(0, 100).isRequired, status: PropTypes.string, strokeWidth: PropTypes.number, width: PropTypes.number, textInside: PropTypes.bool, showText: PropTypes.bool };