All files / src/components/ProgressBar/Bar index.js

0% Statements 0/18
0% Branches 0/9
0% Functions 0/1
0% Lines 0/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                                                             
/**
 * @Component Bar
 * @Type 进度条组件
 * @Author 瞿龙俊 - qulongjun@shine.design
 * @Date 2019-03-25 20:17
 */
import React, {PureComponent} from 'react';
import _ from 'lodash';
import classNames from 'classnames';
import * as PropTypes from 'prop-types';
import {classPrefix} from 'variables';
 
class Bar extends PureComponent {
  render() {
    const {percent, format, showInfo, isStriped, isAnimated, className, attributes} = this.props;
    let checkedPercent = percent;
 
    const classes = classNames(
      `${classPrefix}-progress-bar`,
      {
        [`${classPrefix}-progress-bar-striped`]: isStriped || isAnimated,
        [`${classPrefix}-progress-bar-animated`]: isAnimated,
      },
      className,
    );
 
    if (percent < 0)
      checkedPercent = 0;
    else if (percent > 100)
      checkedPercent = 100;
 
    /** 进度条占比计算 */
    const percentStyle = {width: checkedPercent + '%'};
 
    return (
      <div
        className={classes}
        role="progressbar"
        style={percentStyle}
        aria-valuenow="25"
        aria-valuemin="0"
        aria-valuemax="100"
        {...attributes}
      >
        {!!showInfo && _.isFunction(format) && format.call(_.noop(), checkedPercent)}
      </div>
    );
  }
}
 
Bar.propTypes = {
  /** 当前比例,百分数,范围 0 ~ 100 */
  percent: PropTypes.number,
  /** 定义进度文本展示格式 */
  format: PropTypes.func,
  /** 定义是否显示当前进度 */
  showInfo: PropTypes.bool,
  /** 是否显示条纹 */
  isStriped: PropTypes.bool,
  /** 是否显示动画效果,该属性会默认设置 isStriped 为 true */
  isAnimated: PropTypes.bool,
  /** 用户自定义修饰符 */
  className: PropTypes.string,
  /** 用户自定义属性 */
  attributes: PropTypes.object,
};
 
Bar.defaultProps = {
  percent: 0,
  format: percent => percent + '%',
  showInfo: true,
  isStriped: false,
  isAnimated: false,
  className: '',
  attributes: {},
};
 
export default Bar;