-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from rahsheen/migrate-to-hooks
Migrate to hooks
- Loading branch information
Showing
10 changed files
with
1,179 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,9 @@ | ||
import React, { PureComponent } from "react"; | ||
import React, { PureComponent } from "react" | ||
|
||
export default class Step extends PureComponent { | ||
render() { | ||
const { | ||
children, | ||
currentIndex, | ||
isLast, | ||
prevStep, | ||
nextStep, | ||
onChangeValue, | ||
onSubmit, | ||
values | ||
} = this.props; | ||
const { children, ...rest } = this.props | ||
|
||
return children({ | ||
onChangeValue, | ||
values, | ||
prevStep, | ||
nextStep, | ||
currentIndex, | ||
isLast, | ||
onSubmit | ||
}); | ||
return children({ ...rest }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from "react" | ||
import { Step, useWizard } from "." | ||
|
||
const Wizard = props => { | ||
const { children, initialValues } = props | ||
|
||
if (!children) return null | ||
|
||
const { | ||
nextStep, | ||
prevStep, | ||
index, | ||
onChangeValue, | ||
onSubmit, | ||
values | ||
} = useWizard({ | ||
initialValues, | ||
size: children.length, | ||
onSubmit: props.onSubmit | ||
}) | ||
|
||
const enabledSteps = React.Children.toArray(children).filter( | ||
child => !child.props.disabled | ||
) | ||
|
||
let currentElement = enabledSteps[index] | ||
|
||
return Boolean(currentElement) | ||
? React.cloneElement(currentElement, { | ||
currentIndex: index, | ||
prevStep, | ||
nextStep, | ||
onChangeValue, | ||
onSubmit, | ||
values, | ||
isLast: index === children.length - 1 | ||
}) | ||
: null | ||
} | ||
|
||
Wizard.Step = props => <Step {...props} /> | ||
|
||
export default Wizard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,5 @@ | ||
import React, { PureComponent } from "react"; | ||
import Step from "./Step"; | ||
import useWizard from "./useWizard" | ||
import Wizard from "./Wizard" | ||
import Step from "./Step" | ||
|
||
class Wizard extends PureComponent { | ||
static Step = props => <Step {...props} />; | ||
state = { | ||
index: 0, | ||
values: { | ||
...this.props.initialValues | ||
} | ||
}; | ||
|
||
_prevStep = () => { | ||
if (this.state.index <= 0) return; | ||
this.setState(prevState => ({ | ||
index: prevState.index - 1 | ||
})); | ||
}; | ||
|
||
_nextStep = () => { | ||
if (this.state.index >= this.props.children.length - 1) { | ||
return; | ||
} | ||
|
||
this.setState(prevState => ({ | ||
index: prevState.index + 1 | ||
})); | ||
}; | ||
|
||
_onChangeValue = (name, value) => { | ||
this.setState(prevState => ({ | ||
values: { | ||
...prevState.values, | ||
[name]: value | ||
} | ||
})); | ||
}; | ||
|
||
_onSubmit = () => { | ||
this.props.onSubmit(this.state.values); | ||
}; | ||
|
||
render() { | ||
const { children } = this.props; | ||
const enabledSteps = React.Children.toArray(children).filter( | ||
child => !child.props.disabled | ||
); | ||
|
||
return enabledSteps.map((c, i) => { | ||
return i === this.state.index | ||
? React.cloneElement(c, { | ||
currentIndex: this.state.index, | ||
prevStep: this._prevStep, | ||
nextStep: this._nextStep, | ||
onChangeValue: this._onChangeValue, | ||
onSubmit: this._onSubmit, | ||
isLast: this.state.index === this.props.children.length - 1, | ||
values: this.state.values | ||
}) | ||
: null; | ||
}); | ||
} | ||
} | ||
|
||
export default Wizard; | ||
export { Wizard, useWizard, Step } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState } from "react" | ||
|
||
function useWizard({ | ||
initialValues = {}, | ||
size = 2, | ||
onSubmit: onSubmitWizard = () => {} | ||
} = {}) { | ||
const [index, setIndex] = useState(0) | ||
const [values, setValues] = useState({ ...initialValues }) | ||
|
||
const onSubmit = () => { | ||
onSubmitWizard(values) | ||
} | ||
|
||
const prevStep = () => { | ||
if (index <= 0) return | ||
setIndex(index - 1) | ||
} | ||
|
||
const nextStep = () => { | ||
if (index >= size - 1) return | ||
setIndex(index + 1) | ||
} | ||
|
||
const onChangeValue = (name, value) => { | ||
setValues({ | ||
...values, | ||
[name]: value | ||
}) | ||
} | ||
|
||
return { | ||
nextStep, | ||
prevStep, | ||
index, | ||
onChangeValue, | ||
onSubmit, | ||
values | ||
} | ||
} | ||
|
||
export default useWizard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import expect from "expect" | ||
import React from "react" | ||
import { render, unmountComponentAtNode } from "react-dom" | ||
|
||
import { Wizard } from "src/" | ||
|
||
describe("Wizard", () => { | ||
let node | ||
|
||
beforeEach(() => { | ||
node = document.createElement("div") | ||
}) | ||
|
||
afterEach(() => { | ||
unmountComponentAtNode(node) | ||
}) | ||
|
||
it("displays a welcome message", () => { | ||
render( | ||
<Wizard> | ||
<Wizard.Step>{() => <h2>>@rahsheen/React-Wizard</h2>}</Wizard.Step> | ||
</Wizard>, | ||
node, | ||
() => { | ||
expect(node.innerHTML).toContain("React-Wizard") | ||
} | ||
) | ||
}) | ||
|
||
it("does not render without children", () => { | ||
render(<Wizard />, node, () => { | ||
expect(node.children.length).toBe(0) | ||
}) | ||
}) | ||
|
||
it("does not render future steps", () => { | ||
const structure = ( | ||
<Wizard> | ||
<Wizard.Step>{() => <h2>>@rahsheen/React-Wizard</h2>}</Wizard.Step> | ||
<Wizard.Step>{() => <h2>Step 2</h2>}</Wizard.Step> | ||
</Wizard> | ||
) | ||
render(structure, node, () => { | ||
expect(node.innerHTML).toNotContain("Step 2") | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.