-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathstep-buttons.tsx
More file actions
158 lines (140 loc) · 3.9 KB
/
Copy pathstep-buttons.tsx
File metadata and controls
158 lines (140 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react';
import { styled } from '@mui/material/styles';
import clsx from 'clsx';
import { selectNext } from '@data-driven-forms/common';
import { FormSpy } from '@data-driven-forms/react-form-renderer';
import type { AnyObject, FormOptions } from '@data-driven-forms/react-form-renderer';
import { Button, Grid } from '@mui/material';
import type { GridProps } from '@mui/material';
const PREFIX = 'WizardStepButtons';
const classes = {
wizardBody: `${PREFIX}-wizardBody`,
buttons: `${PREFIX}-buttons`,
button: `${PREFIX}-button`,
buttonsContainer: `${PREFIX}-buttonsContainer`,
};
const StyledGrid = styled(Grid)(() => ({
[`& .${classes.wizardBody}`]: {
padding: 24,
margin: 0,
},
[`& .${classes.buttons}`]: {
display: 'flex',
justifyContent: 'flex-end',
},
[`& .${classes.button}`]: {
marginRight: 16,
},
[`&.${classes.buttonsContainer}`]: {
marginTop: 36,
},
}));
interface FormState {
values: AnyObject;
valid?: boolean;
validating?: boolean;
submitting?: boolean;
}
interface NextButtonProps {
nextStep?: any;
valid: boolean;
handleNext: (nextStep?: any) => void;
nextLabel: string;
getState: () => FormState;
handleSubmit: () => void;
submitLabel: string;
conditionalSubmitFlag?: any;
}
const NextButton: React.FC<NextButtonProps> = ({
nextStep,
valid,
handleNext,
nextLabel,
getState,
handleSubmit,
submitLabel,
conditionalSubmitFlag,
}) => {
const nextResult = nextStep ? selectNext(nextStep, getState) : nextStep;
const progressNext = nextResult !== conditionalSubmitFlag && nextStep;
return (
<Button
variant="contained"
color="primary"
disabled={!valid || getState().validating || getState().submitting}
onClick={() => (progressNext ? handleNext(nextResult) : handleSubmit())}
>
{progressNext ? nextLabel : submitLabel}
</Button>
);
};
interface ButtonLabels {
cancel: string;
submit: string;
back: string;
next: string;
}
interface CustomButtonsProps {
classes: typeof classes;
[key: string]: any;
}
interface WizardStepButtonsProps {
buttons?: React.ComponentType<CustomButtonsProps>;
disableBack: boolean;
handlePrev: () => void;
nextStep?: any;
handleNext: (nextStep?: any) => void;
buttonLabels: ButtonLabels;
formOptions: FormOptions;
ButtonContainerProps?: GridProps;
conditionalSubmitFlag?: any;
[key: string]: any;
}
const WizardStepButtons: React.FC<WizardStepButtonsProps> = ({ buttons: Buttons, ...props }) => {
if (Buttons) {
return <Buttons classes={classes} {...props} />;
}
const {
disableBack,
handlePrev,
nextStep,
handleNext,
buttonLabels: { cancel, submit, back, next },
formOptions,
ButtonContainerProps = {},
conditionalSubmitFlag,
} = props;
return (
<StyledGrid
container
direction="row"
justifyContent="space-evenly"
{...ButtonContainerProps}
className={clsx(classes.buttonsContainer, ButtonContainerProps.className)}
>
<FormSpy subscription={{ values: true, valid: true, validating: true, submitting: true }}>
{() => (
<React.Fragment>
<Grid item md={2} xs={2}>
<Button onClick={() => formOptions.onCancel(formOptions.getState().values)}>{cancel}</Button>
</Grid>
<Grid item md={10} xs={10} className={classes.buttons}>
<Button disabled={disableBack} onClick={() => handlePrev()} className={classes.button}>
{back}
</Button>
<NextButton
{...formOptions}
conditionalSubmitFlag={conditionalSubmitFlag}
handleNext={handleNext}
nextStep={nextStep}
nextLabel={next}
submitLabel={submit}
/>
</Grid>
</React.Fragment>
)}
</FormSpy>
</StyledGrid>
);
};
export default WizardStepButtons;