Skip to content

Instantly share code, notes, and snippets.

@nodox
Created July 1, 2018 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodox/9fc97589835c9eaadcfb333eeadbaaae to your computer and use it in GitHub Desktop.
Save nodox/9fc97589835c9eaadcfb333eeadbaaae to your computer and use it in GitHub Desktop.
Building a stepp - steplist.js
import React from "react";
class StepList extends React.Component {
constructor(props) {
super(props);
this.state = {
currentStep: 0,
totalSteps: this.props.children.length - 1,
};
}
goToPreviousStep = () => {
this.setState({ currentStep: this.state.currentStep - 1 });
};
goToNextStep = () => {
this.setState({ currentStep: this.state.currentStep + 1 });
};
render() {
const children = React.Children.map(this.props.children, (child, index) => {
const { currentStep, totalSteps } = this.state;
return React.cloneElement(child, {
isActive: index === currentStep,
displayPrevious: currentStep > 0,
displayNext: currentStep < totalSteps,
displaySubmit: currentStep === totalSteps,
goToPreviousStep: () => this.goToPreviousStep(),
goToNextStep: () => this.goToNextStep(),
});
});
return children;
}
}
export { StepList };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment