All files / progress index.js

100% Statements 18/18
100% Branches 6/6
100% Functions 2/2
100% Lines 18/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 80 81 82 83 84 85 86 87 88 89  1x 1x                               2x 2x 1x 1x             7x 7x   282x                                               2x 2x 2x 2x 2x 2x 2x 2x         2x                                          
 
import React from 'react'
import {Motion, spring} from 'react-motion'
 
class Linear extends React.Component {
 
  static propTypes = {
    percentage: React.PropTypes.number
  }
 
  static defaultProps = {
    percentage: 75
  }
 
  state = {
    isDone: false
  }
 
  componentWillReceiveProps (nextProps) {
    if (nextProps.percentage >= 100) {
      window.setTimeout(() => {
        this.setState({
          isDone: true
        })
      }, 500)
    }
  }
 
  render () {
    return (
      <Motion defaultStyle={{x: 0}} style={{x: spring(this.state.isDone ? 0 : 4)}}>
        {(value) => value.x !== 0 &&
          <div className='Progress-linear'>
            <div className='Progress-linear-background' style={{height: value.x}}>
              <div className='Progress-linear-inner' style={{width: `${this.props.percentage}%`}} />
            </div>
          </div>
        }
      </Motion>
    )
  }
 
}
 
class Circular extends React.Component {
 
  static propTypes = {
    percentage: React.PropTypes.number
  }
 
  static defaultProps = {
    percentage: 0
  }
 
  render () {
    const radius = 30
    const strokeWidth = 6
    const length = 2 * Math.PI * 30
    const size = 2 * radius + strokeWidth
    const svgSize = '100%'
    const viewBox = '0 0 ' + size + ' ' + size
    const center = size / 2
    const style = {
      strokeDasharray: length + ' ' + length,
      strokeDashoffset: length - (this.props.percentage * length / 100),
      strokeWidth: strokeWidth
    }
    return (
      <svg
        className='Progress-circular'
        width={svgSize}
        height={svgSize}
        viewBox={viewBox}
      >
        <circle
          style={style}
          className='Progress-circular-path'
          fill='none'
          cx={center}
          cy={center}
          r={radius}
        />
      </svg>
    )
  }
}
 
export default {Linear, Circular}