WA-232 Adding prefetch on each step initial props on Stepper component

This commit is contained in:
apanizo 2018-07-17 13:35:43 +02:00
parent 75fe910d08
commit 2049c39ff4
2 changed files with 17 additions and 4 deletions

View File

@ -30,6 +30,7 @@ type State = {
type PageProps = { type PageProps = {
children: Function, children: Function,
prepareNextInitialProps: (values: Object) => {},
} }
class GnoStepper extends React.PureComponent<Props, State> { class GnoStepper extends React.PureComponent<Props, State> {
@ -62,8 +63,10 @@ class GnoStepper extends React.PureComponent<Props, State> {
})) }))
} }
getPageProps = (pages: React$Node): PageProps => React.Children.toArray(pages)[this.state.page].props
getActivePageFrom = (pages: React$Node) => { getActivePageFrom = (pages: React$Node) => {
const activePageProps = React.Children.toArray(pages)[this.state.page].props const activePageProps = this.getPageProps(pages)
const { children, ...props } = activePageProps const { children, ...props } = activePageProps
return children(props) return children(props)
@ -76,11 +79,21 @@ class GnoStepper extends React.PureComponent<Props, State> {
return activePage.props.validate ? activePage.props.validate(values) : {} return activePage.props.validate ? activePage.props.validate(values) : {}
} }
next = (values: Object) => next = async (values: Object) => {
const activePageProps = this.getPageProps(this.props.children)
const { prepareNextInitialProps } = activePageProps
let pageInitialProps
if (prepareNextInitialProps) {
pageInitialProps = await prepareNextInitialProps(values)
}
const finalValues = { ...values, ...pageInitialProps }
this.setState(state => ({ this.setState(state => ({
page: Math.min(state.page + 1, React.Children.count(this.props.children) - 1), page: Math.min(state.page + 1, React.Children.count(this.props.children) - 1),
values, values: finalValues,
})) }))
}
previous = () => previous = () =>
this.setState(state => ({ this.setState(state => ({

View File

@ -1,6 +1,6 @@
// @flow // @flow
import * as React from 'react' import * as React from 'react'
import { Field } from 'react-final-form' import Field from '~/components/forms/Field'
import TextField from '~/components/forms/TextField' import TextField from '~/components/forms/TextField'
import { composeValidators, minValue, mustBeInteger, required } from '~/components/forms/validator' import { composeValidators, minValue, mustBeInteger, required } from '~/components/forms/validator'
import Block from '~/components/layout/Block' import Block from '~/components/layout/Block'