All files / src/components/Stepper Stepper.react.js

33.33% Statements 10/30
11.11% Branches 2/18
37.5% Functions 3/8
41.67% Lines 10/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                                                                                                    1x                                   2x 2x                                                                       2x   2x 8x     2x         2x   2x                                                                                   1x  
// @flow
 
import React, { Component } from 'react';
 
import {Step, StepLabel, Stepper as MuiStepper} from 'material-ui/Stepper';
import RaisedButton from 'material-ui/RaisedButton';
import FlatButton from 'material-ui/FlatButton';
 
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
 
type Props = {
  /** Set the active step (zero based index). This will enable Step control helpers */
  activeStep?: number,
  /** The style for the back button */
  backButtonStyle?: Object,
  /** CSS class name of the root element */
  className?: string,
  /** The style for the button displayed after all steps have been finished */
  finishedButtonStyle?: Object,
  /** The text to display on the final button when all steps have been completed */
  finishedText?: string,
  /** Dash event handler for click events */
  fireEvent?: () => void,
  /** Dash ID */
  id: string,
  /** If set to true, the Stepper will assist in controlling steps for linear flow */
  linear?: boolean,
  /** The style for the next button */
  nextButtonStyle?: Object,
  /** The stepper orientation (layout flow direction) */
  orientation?: 'horizontal' | 'vertical',
  /** Dash callback to update props on the server */
  setProps?: (props: {stepIndex?: number}) => void,
  /** The number of steps that this component will contain */
  stepCount?: number,
  /** The text labels that will be shown next to each step number. The length of this array must
   * match the total number of steps
   */
  stepLabels?: Array<string>,
  /** Override the inline-style of the root element */
  style?: Object,
};
 
type State = {
  finished: boolean,
  stepIndex: number,
};
 
const defaultProps = {
  activeStep: 0,
  backButtonStyle: {marginRight: 12},
  className: '',
  finishedButtonStyle: {},
  finishedText: 'Click here to view again',
  fireEvent: () => {},
  linear: true,
  nextButtonStyle: {},
  orientation: 'horizontal',
  setProps: () => {},
  stepCount: 3,
  stepLabels: ['Step 1', 'Step 2', 'Step 3'],
  style: {},
};
 
export default class Stepper extends Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = {
      stepIndex: props.activeStep,
      finished: false,
    };
  }
 
  handleNext = () => {
    const {stepIndex} = this.state;
    const increased = stepIndex + 1;
 
    this.setState({
      stepIndex: increased,
      finished: increased >= this.props.stepCount,
    });
    if (this.props.setProps) this.props.setProps({activeStep: increased});
    if (this.props.fireEvent) this.props.fireEvent({event: 'click'});
  };
 
  handlePrev = () => {
    const {stepIndex} = this.state;
    const decreased = stepIndex - 1;
 
    if (stepIndex > 0) {
      this.setState({stepIndex: decreased});
      if (this.props.setProps) this.props.setProps({activeStep: decreased});
      if (this.props.fireEvent) this.props.fireEvent({event: 'click'});
    }
  };
 
  resetSteps = () => {
    this.setState({stepIndex: 0, finished: false});
    if (this.props.setProps) this.props.setProps({activeStep: 0});
    if (this.props.fireEvent) this.props.fireEvent({event: 'click'});
  };
 
  createSteps = (stepCount: number) => {
    const steps = [];
 
    for (let i = 0; i < stepCount; i += 1) {
      steps.push(<Step><StepLabel>{this.props.stepLabels[i]}</StepLabel></Step>);
    }
 
    return steps;
  };
 
  render() {
    const { id, backButtonStyle, className, finishedButtonStyle, finishedText, linear,
      nextButtonStyle, orientation, stepCount, style } = this.props;
 
    return (
      <div id={id} className={className} style={style}>
        <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
          <div>
            <MuiStepper
              activeStep={this.state.stepIndex}
              linear={linear}
              orientation={orientation}
            >
              {this.createSteps(stepCount)}
            </MuiStepper>
            {this.state.finished ? (
              <RaisedButton
                label={finishedText}
                onClick={this.resetSteps}
                style={finishedButtonStyle}
              />
            ) : (
              <div>
                <div style={{marginTop: 14}}>
                  <FlatButton
                    label="Back"
                    disabled={this.state.stepIndex === 0}
                    onClick={this.handlePrev}
                    style={backButtonStyle}
                  />
                  <RaisedButton
                    label={this.state.stepIndex >= stepCount - 1 ? 'Finish' : 'Next'}
                    primary={true}
                    onClick={this.handleNext}
                    style={nextButtonStyle}
                  />
                </div>
              </div>
            )}
          </div>
        </MuiThemeProvider>
      </div>
    );
  }
}
 
Stepper.defaultProps = defaultProps;