1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { Button } from '@patternfly/react-core' ;
4
+ import get from 'lodash/get' ;
5
+
6
+ export const selectNext = ( nextStep , getState ) => {
7
+ if ( typeof nextStep === 'string' ) {
8
+ return nextStep ;
9
+ }
10
+
11
+ if ( typeof nextStep === 'function' ) {
12
+ return nextStep ( { values : getState ( ) . values } ) ;
13
+ }
14
+
15
+ const selectedValue = get ( getState ( ) . values , nextStep . when ) ;
16
+
17
+ return nextStep . stepMapper [ selectedValue ] ;
18
+ } ;
4
19
5
20
const SimpleNext = ( {
6
- next ,
21
+ nextStep ,
7
22
valid,
8
23
handleNext,
9
24
submit,
10
25
nextLabel,
11
- } ) => (
26
+ getState,
27
+ } ) => (
12
28
< Button
13
29
variant = "primary"
14
30
type = "button"
15
31
isDisabled = { ! valid }
16
- onClick = { ( ) => valid ? handleNext ( next ) : submit ( ) }
32
+ onClick = { ( ) => valid ? handleNext ( selectNext ( nextStep , getState ) ) : submit ( ) }
17
33
>
18
34
{ nextLabel }
19
35
</ Button >
@@ -25,37 +41,16 @@ SimpleNext.propTypes = {
25
41
handleNext : PropTypes . func . isRequired ,
26
42
submit : PropTypes . func . isRequired ,
27
43
nextLabel : PropTypes . string . isRequired ,
28
- } ;
29
-
30
- const ConditionalNext = ( {
31
- nextStep,
32
- FieldProvider,
33
- ...rest
34
- } ) => (
35
- < FieldProvider
36
- name = { nextStep . when }
37
- subscription = { { value : true } }
38
- >
39
- { ( { input : { value } } ) => < SimpleNext next = { nextStep . stepMapper [ value ] } { ...rest } /> }
40
- </ FieldProvider >
41
- ) ;
42
-
43
- ConditionalNext . propTypes = {
44
- nextStep : PropTypes . shape ( {
45
- when : PropTypes . string . isRequired ,
46
- stepMapper : PropTypes . object . isRequired ,
47
- } ) . isRequired ,
48
- FieldProvider : PropTypes . func . isRequired ,
44
+ getState : PropTypes . func . isRequired ,
45
+ nextStep : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) . isRequired ,
49
46
} ;
50
47
51
48
const SubmitButton = ( { handleSubmit, submitLabel } ) => < Button type = "button" variant = "primary" onClick = { handleSubmit } > { submitLabel } </ Button > ;
52
49
53
50
const renderNextButton = ( { nextStep, handleSubmit, submitLabel, ...rest } ) =>
54
- ! nextStep
55
- ? < SubmitButton handleSubmit = { handleSubmit } submitLabel = { submitLabel } />
56
- : typeof nextStep === 'object'
57
- ? < ConditionalNext nextStep = { nextStep } { ...rest } />
58
- : < SimpleNext next = { nextStep } { ...rest } /> ;
51
+ nextStep
52
+ ? < SimpleNext nextStep = { nextStep } { ...rest } />
53
+ : < SubmitButton handleSubmit = { handleSubmit } submitLabel = { submitLabel } /> ;
59
54
60
55
const WizardStepButtons = ( {
61
56
buttons : Buttons ,
@@ -74,7 +69,7 @@ const WizardStepButtons = ({
74
69
} } ) =>
75
70
< footer className = { `pf-c-wizard__footer ${ buttonsClassName ? buttonsClassName : '' } ` } >
76
71
{ Buttons ? < Buttons
77
- ConditionalNext = { ConditionalNext }
72
+ ConditionalNext = { SimpleNext }
78
73
SubmitButton = { SubmitButton }
79
74
SimpleNext = { SimpleNext }
80
75
formOptions = { formOptions }
@@ -89,18 +84,17 @@ const WizardStepButtons = ({
89
84
...formOptions ,
90
85
handleNext,
91
86
nextStep,
92
- FieldProvider,
93
87
nextLabel : next ,
94
88
submitLabel : submit ,
95
89
...args ,
96
90
} ) }
91
+ selectNext = { selectNext }
97
92
/>
98
93
: < React . Fragment >
99
94
{ renderNextButton ( {
100
95
...formOptions ,
101
96
handleNext,
102
97
nextStep,
103
- FieldProvider,
104
98
nextLabel : next ,
105
99
submitLabel : submit ,
106
100
} ) }
@@ -124,7 +118,7 @@ WizardStepButtons.propTypes = {
124
118
stepMapper : PropTypes . object . isRequired ,
125
119
} ) ,
126
120
] ) ,
127
- FieldProvider : PropTypes . func . isRequired ,
121
+ FieldProvider : PropTypes . func ,
128
122
buttonLabels : PropTypes . shape ( {
129
123
submit : PropTypes . string . isRequired ,
130
124
cancel : PropTypes . string . isRequired ,
0 commit comments