<Wizard.Page>
</Wizard.Page>
to mi wygląda na komponent dodatkowy
Wizard ma w swojej implementacji obiekt PAGE, i odwoładnie jest do obiektu Page- componentu Page znajdującego się w klasie wizard
class Wizard extends React.Component {
static Page = ({ children }) => children
class Wizard extends React.Component {
static Page = ({ children }) => children
state = {
currentPage: 0,
}
get isLastPage() {
const children = this.props.children;
const currentPage = this.state.currentPage;
return currentPage === React.Children.count(children) - 1;
}
get activePage() {
const currentPage = this.state.currentPage;
const children = this.props.children;
const activePage = React.Children.toArray(children)[currentPage];
return activePage;
}
handleNext = (values) => {
const children = this.props.children;
this.setState(prevState => ({
currentPage: Math.min(prevState.currentPage + 1, children.length - 1),
values,
}));
}
handlePrevious = () => {
this.setState(prevState => ({
currentPage: Math.max(prevState.currentPage - 1, 0),
}));
}
// Both validate and handleSubmit switching are implemented
// here because Redux Final Form does not accept changes to those
// functions once the form has been defined.
validate = (values) => {
const { props } = this.activePage;
return props.validate ? props.validate(values) : {};
}
handleSubmit = (values) => {
if (this.isLastPage) {
return this.props.onSubmit(values);
}
return this.handleNext(values);
}
render() {
const { currentPage, values: prevValues } = this.state;
return (
<Form
initialValues={prevValues}
validate={this.validate}
onSubmit={this.handleSubmit}
>
{({ handleSubmit, submitting }) => (
<form onSubmit={handleSubmit}>
{this.activePage}
<div>
<div className={styles.buttons}>
{currentPage > 0 && (
<Button
primary
iconOnLeft
type="button"
onClick={this.handlePrevious}
icon={angleArrowLeft}
>
Previous
</Button>
)}
{this.isLastPage ? (
<Button
primary
type="submit"
disabled={submitting}
icon={submitting ? spinnerIcon : checkMarkIcon}
>
{this.props.submitText}
</Button>
) : (
<React.Fragment>
<Button danger icon={crossIcon} onClick={this.props.onClose}>
Cancel
</Button>
<Button primary type="submit" icon={angleArrowRight}>
Continue
</Button>
</React.Fragment>
)}
</div>
</div>
</form>
)}
</Form>
);
}
}
Wizard.propTypes = {
children: PropTypes.node.isRequired,
onClose: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
submitText: PropTypes.string,
};
Wizard.defaultProps = {
submitText: 'Submit',
};
export default Wizard;