1
- import React , { useState } from 'react' ;
1
+ import React from 'react' ;
2
2
import WizardStep from './wizard/wizard-step' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import { Wizard as PfWizard , Modal , Icon } from 'patternfly-react' ;
5
- import handleEnter from '@data-driven-forms/common/src/wizard/enter-handler' ;
6
- import { useFormApi } from '@data-driven-forms/react-form-renderer' ;
5
+ import Wizard , { wizardProps } from '@data-driven-forms/common/src/wizard/wizard' ;
7
6
8
7
const defaultButtonLabels = {
9
8
cancel : 'Cancel' ,
@@ -12,37 +11,7 @@ const defaultButtonLabels = {
12
11
submit : 'Submit'
13
12
} ;
14
13
15
- const Wizard = ( { title, buttonLabels, stepsInfo, fields, inModal } ) => {
16
- const formOptions = useFormApi ( ) ;
17
-
18
- const [ activeStep , setActiveStep ] = useState ( fields [ 0 ] . name ) ;
19
- const [ prevSteps , setPrevSteps ] = useState ( [ ] ) ;
20
-
21
- const handleNext = ( nextStep ) => {
22
- setPrevSteps ( [ ...prevSteps , activeStep ] ) ;
23
- setActiveStep ( nextStep ) ;
24
- } ;
25
-
26
- const handlePrev = ( ) => {
27
- setActiveStep ( prevSteps [ prevSteps . length - 1 ] ) ;
28
-
29
- const newSteps = prevSteps ;
30
- newSteps . pop ( ) ;
31
- setPrevSteps ( newSteps ) ;
32
- } ;
33
-
34
- const findCurrentStep = ( activeStep ) => fields . find ( ( { name } ) => name === activeStep ) ;
35
-
36
- const findActiveFields = ( visitedSteps ) =>
37
- visitedSteps . map ( ( key ) => findCurrentStep ( key ) . fields . map ( ( { name } ) => name ) ) . reduce ( ( acc , curr ) => curr . concat ( acc . map ( ( item ) => item ) ) , [ ] ) ;
38
-
39
- const getValues = ( values , visitedSteps ) =>
40
- Object . keys ( values )
41
- . filter ( ( key ) => findActiveFields ( visitedSteps ) . includes ( key ) )
42
- . reduce ( ( acc , curr ) => ( { ...acc , [ curr ] : values [ curr ] } ) , { } ) ;
43
-
44
- const handleSubmit = ( ) => formOptions . onSubmit ( getValues ( formOptions . getState ( ) . values , [ ...prevSteps , activeStep ] ) ) ;
45
-
14
+ const WizardInternal = ( { title, buttonLabels, stepsInfo, inModal, onKeyDown, formOptions, handleNext, handlePrev, prevSteps, currentStep } ) => {
46
15
const renderSteps = ( ) =>
47
16
stepsInfo . map ( ( step , stepIndex ) => (
48
17
< PfWizard . Step
@@ -61,7 +30,7 @@ const Wizard = ({ title, buttonLabels, stepsInfo, fields, inModal }) => {
61
30
} ;
62
31
63
32
return (
64
- < div onKeyDown = { ( e ) => handleEnter ( e , formOptions , activeStep , findCurrentStep , handleNext , handleSubmit ) } >
33
+ < div onKeyDown = { onKeyDown } >
65
34
{ title && (
66
35
< Modal . Header >
67
36
{ inModal && (
@@ -78,32 +47,27 @@ const Wizard = ({ title, buttonLabels, stepsInfo, fields, inModal }) => {
78
47
handlePrev = { handlePrev }
79
48
disableBack = { prevSteps . length === 0 }
80
49
buttonLabels = { fullButtonLabels }
81
- { ...findCurrentStep ( activeStep ) }
82
- formOptions = { {
83
- ...formOptions ,
84
- handleSubmit
85
- } }
50
+ { ...currentStep }
51
+ formOptions = { formOptions }
86
52
/>
87
53
</ div >
88
54
) ;
89
55
} ;
90
56
91
- Wizard . propTypes = {
57
+ WizardInternal . propTypes = {
92
58
title : PropTypes . string ,
93
59
buttonLabels : PropTypes . object ,
94
60
stepsInfo : PropTypes . array ,
95
61
inModal : PropTypes . bool ,
96
- fields : PropTypes . arrayOf (
97
- PropTypes . shape ( {
98
- name : PropTypes . string
99
- } )
100
- ) . isRequired
62
+ ...wizardProps
101
63
} ;
102
64
103
- Wizard . defaultProps = {
65
+ WizardInternal . defaultProps = {
104
66
title : undefined ,
105
67
stepsInfo : undefined ,
106
68
inModal : false
107
69
} ;
108
70
109
- export default Wizard ;
71
+ const WizardFinal = ( props ) => < Wizard Wizard = { WizardInternal } { ...props } /> ;
72
+
73
+ export default WizardFinal ;
0 commit comments