Skip to content

Instantly share code, notes, and snippets.

@SebastianHGonzalez
Last active May 8, 2019 17:36
Show Gist options
  • Save SebastianHGonzalez/7e6117d4f91f13a5c7d8ccf24274a8b5 to your computer and use it in GitHub Desktop.
Save SebastianHGonzalez/7e6117d4f91f13a5c7d8ccf24274a8b5 to your computer and use it in GitHub Desktop.
React Router Wizard
/**
* Copyright 2019 Sebastian Gonzalez
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import React, { createContext } from "react";
import { MemoryRouter, Switch, Route, withRouter } from "react-router-dom";
const WizardSwitch = ({ children }) => (
<Switch>
{children.map((step, index) => (
<Route path={`${index}`}>{step}</Route>
))}
</Switch>
);
const withWizardMemoryRouter = (WrappedComponent) => (props) => (
<MemoryRouter initialEntries={[...props.children.keys()].map((k) => k.toString())}>
<WrappedComponent {...props} />
</MemoryRouter>
);
const withWizardContext = (WrappedComponent) => (props) => {
const {
history: { goForward, goBack },
} = props;
const nextStep = () => {
goForward();
};
const previousStep = () => {
goBack();
};
return (
<Wizard.Context.Provider value={{ nextStep, previousStep }}>
<WrappedComponent {...props} />
</Wizard.Context.Provider>
);
};
const Wizard = withWizardMemoryRouter(withRouter(withWizardContext(WizardSwitch)));
Wizard.Context = createContext();
export default Wizard;
/**
* Copyright 2019 Sebastian Gonzalez
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import React, { Component, Fragment } from "react";
import Wizard from "./Wizard2";
class WizardExample extends Component {
render() {
return (
<Wizard>
<div>
step1
<Wizard.Context.Consumer>
{({ nextStep, previousStep }) => (
<Fragment>
<input type="button" value="next" onClick={nextStep} />
<input type="button" value="back" onClick={previousStep} />
</Fragment>
)}
</Wizard.Context.Consumer>
</div>
<div>
step2
<Wizard.Context.Consumer>
{({ nextStep, previousStep }) => (
<Fragment>
<input type="button" value="next" onClick={nextStep} />
<input type="button" value="back" onClick={previousStep} />
</Fragment>
)}
</Wizard.Context.Consumer>
</div>
<div>
step3
<Wizard.Context.Consumer>
{({ nextStep, previousStep }) => (
<Fragment>
<input type="button" value="next" onClick={nextStep} />
<input type="button" value="back" onClick={previousStep} />
</Fragment>
)}
</Wizard.Context.Consumer>
</div>
</Wizard>
);
}
}
export default WizardExample;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment