1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import { Button } from '@material-ui/core' ;
4
- import NavigateNextIcon from '@material-ui/icons/NavigateNext' ;
5
- import NavigateBackIcon from '@material-ui/icons/NavigateBefore' ;
6
- import Send from '@material-ui/icons/Send' ;
7
- import { useFieldApi } from '@data-driven-forms/react-form-renderer' ;
3
+ import clsx from 'clsx' ;
8
4
9
- const SimpleNext = ( { next, valid, handleNext, submit, buttonLabels } ) => (
10
- < Button variant = "contained" type = "button" color = "primary" onClick = { ( ) => ( valid ? handleNext ( next ) : submit ( ) ) } >
11
- { buttonLabels . next }
12
- < NavigateNextIcon />
5
+ import selectNext from '@data-driven-forms/common/src/wizard/select-next' ;
6
+ import { FormSpy } from '@data-driven-forms/react-form-renderer' ;
7
+ import { Button , Grid } from '@material-ui/core' ;
8
+ import { makeStyles } from '@material-ui/core/styles' ;
9
+
10
+ const NextButton = ( { nextStep, valid, handleNext, nextLabel, getState, handleSubmit, submitLabel } ) => (
11
+ < Button
12
+ variant = "contained"
13
+ color = "primary"
14
+ disabled = { ! valid || getState ( ) . validating || getState ( ) . submitting }
15
+ onClick = { ( ) => ( nextStep ? handleNext ( selectNext ( nextStep , getState ) ) : handleSubmit ( ) ) }
16
+ >
17
+ { nextStep ? nextLabel : submitLabel }
13
18
</ Button >
14
19
) ;
15
20
16
- SimpleNext . propTypes = {
17
- next : PropTypes . string ,
21
+ NextButton . propTypes = {
22
+ nextStep : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . func , PropTypes . object ] ) ,
23
+ handleSubmit : PropTypes . func . isRequired ,
24
+ submitLabel : PropTypes . node . isRequired ,
18
25
valid : PropTypes . bool ,
19
26
handleNext : PropTypes . func . isRequired ,
20
- submit : PropTypes . func . isRequired ,
21
- buttonLabels : PropTypes . shape ( {
22
- next : PropTypes . node
23
- } )
24
- } ;
25
-
26
- const ConditionalNext = ( { nextStep, ...rest } ) => {
27
- const {
28
- input : { value }
29
- } = useFieldApi ( { name : nextStep . when , subscription : { value : true } } ) ;
30
- return < SimpleNext next = { nextStep . stepMapper [ value ] } { ...rest } /> ;
27
+ nextLabel : PropTypes . node . isRequired ,
28
+ getState : PropTypes . func . isRequired
31
29
} ;
32
30
33
- ConditionalNext . propTypes = {
34
- nextStep : PropTypes . shape ( {
35
- when : PropTypes . string . isRequired ,
36
- stepMapper : PropTypes . object . isRequired
37
- } ) . isRequired
38
- } ;
31
+ const useStyles = makeStyles ( ( ) => ( {
32
+ wizardBody : {
33
+ padding : 24 ,
34
+ margin : 0
35
+ } ,
36
+ buttons : {
37
+ display : 'flex' ,
38
+ justifyContent : 'flex-end'
39
+ } ,
40
+ button : {
41
+ marginRight : 16
42
+ } ,
43
+ buttonsContainer : {
44
+ marginTop : 36
45
+ }
46
+ } ) ) ;
39
47
40
- const submitButton = ( handleSubmit , label ) => (
41
- < Button type = "button" variant = "contained" color = "primary" onClick = { handleSubmit } >
42
- { label } < Send />
43
- </ Button >
44
- ) ;
48
+ const WizardStepButtons = ( { buttons : Buttons , ...props } ) => {
49
+ const classes = useStyles ( ) ;
45
50
46
- const renderNextButton = ( { nextStep, handleSubmit, ...rest } ) =>
47
- ! nextStep ? (
48
- submitButton ( handleSubmit , rest . buttonLabels . submit )
49
- ) : typeof nextStep === 'object' ? (
50
- < ConditionalNext nextStep = { nextStep } { ...rest } />
51
- ) : (
52
- < SimpleNext next = { nextStep } { ...rest } />
53
- ) ;
51
+ if ( Buttons ) {
52
+ return < Buttons classes = { classes } { ...props } /> ;
53
+ }
54
54
55
- const WizardStepButtons = ( { formOptions, disableBack, handlePrev, nextStep, handleNext, buttonLabels } ) => (
56
- < div >
57
- { formOptions . onCancel && (
58
- < Button type = "button" variant = "contained" color = "secondary" onClick = { formOptions . onCancel } >
59
- { buttonLabels . cancel }
60
- </ Button >
61
- ) }
55
+ const {
56
+ disableBack,
57
+ handlePrev,
58
+ nextStep,
59
+ handleNext,
60
+ buttonLabels : { cancel, submit, back, next } ,
61
+ formOptions,
62
+ ButtonContainerProps
63
+ } = props ;
62
64
63
- < Button type = "button" variant = "contained" disabled = { disableBack } onClick = { handlePrev } >
64
- < NavigateBackIcon />
65
- { buttonLabels . back }
66
- </ Button >
67
- { renderNextButton ( {
68
- ...formOptions ,
69
- handleNext,
70
- nextStep,
71
- buttonLabels
72
- } ) }
73
- </ div >
74
- ) ;
65
+ return (
66
+ < Grid
67
+ container
68
+ direction = "row"
69
+ justify = "space-evenly"
70
+ { ...ButtonContainerProps }
71
+ className = { clsx ( classes . buttonsContainer , ButtonContainerProps . className ) }
72
+ >
73
+ < FormSpy subscription = { { valid : true , validating : true , submitting : true } } >
74
+ { ( ) => (
75
+ < React . Fragment >
76
+ < Grid item md = { 2 } xs = { 2 } >
77
+ < Button onClick = { formOptions . onCancel } > { cancel } </ Button >
78
+ </ Grid >
79
+ < Grid item md = { 10 } xs = { 10 } className = { classes . buttons } >
80
+ < Button disabled = { disableBack } onClick = { handlePrev } className = { classes . button } >
81
+ { back }
82
+ </ Button >
83
+ < NextButton { ...formOptions } handleNext = { handleNext } nextStep = { nextStep } nextLabel = { next } submitLabel = { submit } />
84
+ </ Grid >
85
+ </ React . Fragment >
86
+ ) }
87
+ </ FormSpy >
88
+ </ Grid >
89
+ ) ;
90
+ } ;
75
91
76
92
WizardStepButtons . propTypes = {
77
- formOptions : PropTypes . shape ( {
78
- onCancel : PropTypes . func ,
79
- handleSubmit : PropTypes . func . isRequired
80
- } ) . isRequired ,
93
+ ButtonContainerProps : PropTypes . object ,
81
94
disableBack : PropTypes . bool ,
82
95
handlePrev : PropTypes . func . isRequired ,
83
96
handleNext : PropTypes . func . isRequired ,
@@ -86,14 +99,24 @@ WizardStepButtons.propTypes = {
86
99
PropTypes . shape ( {
87
100
when : PropTypes . string . isRequired ,
88
101
stepMapper : PropTypes . object . isRequired
89
- } )
102
+ } ) ,
103
+ PropTypes . func
90
104
] ) ,
91
105
buttonLabels : PropTypes . shape ( {
92
- submit : PropTypes . node ,
93
- cancel : PropTypes . node ,
94
- back : PropTypes . node ,
95
- next : PropTypes . node
106
+ submit : PropTypes . node . isRequired ,
107
+ cancel : PropTypes . node . isRequired ,
108
+ back : PropTypes . node . isRequired ,
109
+ next : PropTypes . node . isRequired
110
+ } ) . isRequired ,
111
+ buttons : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) ,
112
+ formOptions : PropTypes . shape ( {
113
+ getState : PropTypes . func . isRequired ,
114
+ onCancel : PropTypes . func . isRequired
96
115
} )
97
116
} ;
98
117
118
+ WizardStepButtons . defaultProps = {
119
+ ButtonContainerProps : { }
120
+ } ;
121
+
99
122
export default WizardStepButtons ;
0 commit comments