1- import React , { useState } from 'react' ;
1+ import React from 'react' ;
22import WizardStep from './wizard/wizard-step' ;
33import PropTypes from 'prop-types' ;
44import { 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' ;
76
87const defaultButtonLabels = {
98 cancel : 'Cancel' ,
@@ -12,37 +11,7 @@ const defaultButtonLabels = {
1211 submit : 'Submit'
1312} ;
1413
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 } ) => {
4615 const renderSteps = ( ) =>
4716 stepsInfo . map ( ( step , stepIndex ) => (
4817 < PfWizard . Step
@@ -61,7 +30,7 @@ const Wizard = ({ title, buttonLabels, stepsInfo, fields, inModal }) => {
6130 } ;
6231
6332 return (
64- < div onKeyDown = { ( e ) => handleEnter ( e , formOptions , activeStep , findCurrentStep , handleNext , handleSubmit ) } >
33+ < div onKeyDown = { onKeyDown } >
6534 { title && (
6635 < Modal . Header >
6736 { inModal && (
@@ -78,32 +47,27 @@ const Wizard = ({ title, buttonLabels, stepsInfo, fields, inModal }) => {
7847 handlePrev = { handlePrev }
7948 disableBack = { prevSteps . length === 0 }
8049 buttonLabels = { fullButtonLabels }
81- { ...findCurrentStep ( activeStep ) }
82- formOptions = { {
83- ...formOptions ,
84- handleSubmit
85- } }
50+ { ...currentStep }
51+ formOptions = { formOptions }
8652 />
8753 </ div >
8854 ) ;
8955} ;
9056
91- Wizard . propTypes = {
57+ WizardInternal . propTypes = {
9258 title : PropTypes . string ,
9359 buttonLabels : PropTypes . object ,
9460 stepsInfo : PropTypes . array ,
9561 inModal : PropTypes . bool ,
96- fields : PropTypes . arrayOf (
97- PropTypes . shape ( {
98- name : PropTypes . string
99- } )
100- ) . isRequired
62+ ...wizardProps
10163} ;
10264
103- Wizard . defaultProps = {
65+ WizardInternal . defaultProps = {
10466 title : undefined ,
10567 stepsInfo : undefined ,
10668 inModal : false
10769} ;
10870
109- export default Wizard ;
71+ const WizardFinal = ( props ) => < Wizard Wizard = { WizardInternal } { ...props } /> ;
72+
73+ export default WizardFinal ;
0 commit comments