11import React , { useReducer , useEffect } from 'react' ;
22import PropTypes from 'prop-types' ;
3- import { useFormApi } from '@data-driven-forms/react-form-renderer' ;
3+ import { useFormApi , WizardContext } from '@data-driven-forms/react-form-renderer' ;
44
55import get from 'lodash/get' ;
66import set from 'lodash/set' ;
77import flattenDeep from 'lodash/flattenDeep' ;
88import handleEnter from './enter-handler' ;
99import reducer , { DYNAMIC_WIZARD_TYPES , findCurrentStep } from './reducer' ;
10+ import selectNext from './select-next' ;
1011
11- const Wizard = ( { fields, isDynamic, crossroads, Wizard, component, ...props } ) => {
12+ const Wizard = ( { fields, isDynamic, crossroads, Wizard, component, initialState , ...props } ) => {
1213 const formOptions = useFormApi ( ) ;
1314
1415 const [ state , dispatch ] = useReducer ( reducer , {
1516 activeStep : fields [ 0 ] . name ,
1617 prevSteps : [ ] ,
1718 activeStepIndex : 0 ,
1819 maxStepIndex : 0 ,
20+ ...initialState ,
1921 isDynamic : isDynamic || fields . some ( ( { nextStep } ) => DYNAMIC_WIZARD_TYPES . includes ( typeof nextStep ) ) ,
2022 loading : true
2123 } ) ;
@@ -45,10 +47,13 @@ const Wizard = ({ fields, isDynamic, crossroads, Wizard, component, ...props })
4547 return finalObject ;
4648 } ;
4749
50+ const onCancel = ( ) => formOptions . onCancel ( state ) ;
51+
4852 const handleSubmit = ( ) =>
4953 formOptions . onSubmit (
5054 prepareValues ( formOptions . getState ( ) . values , [ ...state . prevSteps , state . activeStep ] , formOptions . getRegisteredFields ) ,
51- formOptions
55+ formOptions ,
56+ state
5257 ) ;
5358
5459 const jumpToStep = ( index , valid ) => dispatch ( { type : 'jumpToStep' , payload : { index, valid, fields, crossroads, formOptions } } ) ;
@@ -64,25 +69,30 @@ const Wizard = ({ fields, isDynamic, crossroads, Wizard, component, ...props })
6469 const onKeyDown = ( e ) => handleEnter ( e , formOptions , state . activeStep , findCurrentStepWrapped , handleNext , handleSubmit ) ;
6570
6671 return (
67- < Wizard
68- { ...props }
69- handleNext = { handleNext }
70- onKeyDown = { onKeyDown }
71- setPrevSteps = { setPrevSteps }
72- currentStep = { findCurrentStep ( state . activeStep , fields ) }
73- jumpToStep = { jumpToStep }
74- handlePrev = { handlePrev }
75- formOptions = { {
76- ...formOptions ,
77- handleSubmit
72+ < WizardContext . Provider
73+ value = { {
74+ handleNext,
75+ onKeyDown,
76+ setPrevSteps,
77+ currentStep : findCurrentStep ( state . activeStep , fields ) ,
78+ jumpToStep,
79+ handlePrev,
80+ formOptions : {
81+ ...formOptions ,
82+ onCancel,
83+ handleSubmit
84+ } ,
85+ navSchema : state . navSchema ,
86+ activeStepIndex : state . activeStepIndex ,
87+ maxStepIndex : state . maxStepIndex ,
88+ isDynamic : state . isDynamic ,
89+ crossroads,
90+ prevSteps : state . prevSteps ,
91+ selectNext
7892 } }
79- navSchema = { state . navSchema }
80- activeStepIndex = { state . activeStepIndex }
81- maxStepIndex = { state . maxStepIndex }
82- isDynamic = { state . isDynamic }
83- crossroads = { crossroads }
84- prevSteps = { state . prevSteps }
85- />
93+ >
94+ < Wizard { ...props } />
95+ </ WizardContext . Provider >
8696 ) ;
8797} ;
8898
@@ -95,7 +105,8 @@ Wizard.propTypes = {
95105 isDynamic : PropTypes . bool ,
96106 crossroads : PropTypes . arrayOf ( PropTypes . string ) ,
97107 Wizard : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) ,
98- component : PropTypes . any
108+ component : PropTypes . any ,
109+ initialState : PropTypes . object
99110} ;
100111
101112export default Wizard ;
0 commit comments