1- import React , { useState , cloneElement } from 'react' ;
1+ import React , { cloneElement } from 'react' ;
22import PropTypes from 'prop-types' ;
33import WizardStep from './wizard/wizard-step' ;
44import { Grid , Typography } from '@material-ui/core' ;
5- import { useFormApi } from '@data-driven-forms/react-form-renderer ' ;
5+ import Wizard , { wizardProps } from '@data-driven-forms/common/src/wizard/wizard ' ;
66
7- const Wizard = ( { fields, title, description } ) => {
8- const [ activeStep , setActiveStep ] = useState ( fields [ 0 ] . name ) ;
9- const [ prevSteps , setPrevSteps ] = useState ( [ ] ) ;
10-
11- const formOptions = useFormApi ( ) ;
12-
13- const handleNext = ( nextStep ) => {
14- setPrevSteps ( [ ...prevSteps , activeStep ] ) ;
15- setActiveStep ( nextStep ) ;
16- } ;
17-
18- const handlePrev = ( ) => {
19- setActiveStep ( prevSteps [ prevSteps . length - 1 ] ) ;
20-
21- const newSteps = prevSteps ;
22- newSteps . pop ( ) ;
23- setPrevSteps ( newSteps ) ;
24- } ;
25-
26- const findCurrentStep = ( activeStep ) => fields . find ( ( { name } ) => name === activeStep ) ;
27-
28- const findActiveFields = ( visitedSteps ) =>
29- visitedSteps . map ( ( key ) => findCurrentStep ( key ) . fields . map ( ( { name } ) => name ) ) . reduce ( ( acc , curr ) => curr . concat ( acc . map ( ( item ) => item ) ) , [ ] ) ;
30-
31- const getValues = ( values , visitedSteps ) =>
32- Object . keys ( values )
33- . filter ( ( key ) => findActiveFields ( visitedSteps ) . includes ( key ) )
34- . reduce ( ( acc , curr ) => ( { ...acc , [ curr ] : values [ curr ] } ) , { } ) ;
35-
36- const handleSubmit = ( ) => formOptions . onSubmit ( getValues ( formOptions . getState ( ) . values , [ ...prevSteps , activeStep ] ) ) ;
37-
38- const currentStep = (
39- < WizardStep
40- { ...findCurrentStep ( activeStep ) }
41- formOptions = { {
42- ...formOptions ,
43- handleSubmit
44- } }
45- />
46- ) ;
7+ const WizardInternal = ( { title, description, currentStep, formOptions, prevSteps, handleNext, handlePrev } ) => {
8+ const step = < WizardStep { ...currentStep } formOptions = { formOptions } /> ;
479
4810 return (
4911 < Grid container spacing = { 6 } >
@@ -53,7 +15,7 @@ const Wizard = ({ fields, title, description }) => {
5315 < Typography component = "h5" > { `Step ${ prevSteps . length + 1 } ` } </ Typography >
5416 </ Grid >
5517 < Grid item xs = { 12 } >
56- { cloneElement ( currentStep , {
18+ { cloneElement ( step , {
5719 handleNext,
5820 handlePrev,
5921 disableBack : prevSteps . length === 0
@@ -63,14 +25,12 @@ const Wizard = ({ fields, title, description }) => {
6325 ) ;
6426} ;
6527
66- Wizard . propTypes = {
28+ WizardInternal . propTypes = {
6729 title : PropTypes . node ,
6830 description : PropTypes . node ,
69- fields : PropTypes . arrayOf (
70- PropTypes . shape ( {
71- name : PropTypes . string
72- } )
73- ) . isRequired
31+ ...wizardProps
7432} ;
7533
76- export default Wizard ;
34+ const MuiWizard = ( props ) => < Wizard Wizard = { WizardInternal } { ...props } /> ;
35+
36+ export default MuiWizard ;
0 commit comments