1
- import React , { useState , cloneElement } from 'react' ;
1
+ import React , { cloneElement } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import WizardStep from './wizard/wizard-step' ;
4
4
import { 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 ' ;
6
6
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 } /> ;
47
9
48
10
return (
49
11
< Grid container spacing = { 6 } >
@@ -53,7 +15,7 @@ const Wizard = ({ fields, title, description }) => {
53
15
< Typography component = "h5" > { `Step ${ prevSteps . length + 1 } ` } </ Typography >
54
16
</ Grid >
55
17
< Grid item xs = { 12 } >
56
- { cloneElement ( currentStep , {
18
+ { cloneElement ( step , {
57
19
handleNext,
58
20
handlePrev,
59
21
disableBack : prevSteps . length === 0
@@ -63,14 +25,12 @@ const Wizard = ({ fields, title, description }) => {
63
25
) ;
64
26
} ;
65
27
66
- Wizard . propTypes = {
28
+ WizardInternal . propTypes = {
67
29
title : PropTypes . node ,
68
30
description : PropTypes . node ,
69
- fields : PropTypes . arrayOf (
70
- PropTypes . shape ( {
71
- name : PropTypes . string
72
- } )
73
- ) . isRequired
31
+ ...wizardProps
74
32
} ;
75
33
76
- export default Wizard ;
34
+ const MuiWizard = ( props ) => < Wizard Wizard = { WizardInternal } { ...props } /> ;
35
+
36
+ export default MuiWizard ;
0 commit comments