Refactor data-explorer to make it background more customizable, add slot for custom...
[arvados-workbench2.git] / src / components / form-dialog / form-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { InjectedFormProps } from 'redux-form';
7 import { Dialog, DialogActions, DialogContent, DialogTitle } from '@material-ui/core/';
8 import { Button, StyleRulesCallback, WithStyles, withStyles, CircularProgress } from '@material-ui/core';
9 import { WithDialogProps } from '~/store/dialog/with-dialog';
10
11 type CssRules = "button" | "lastButton" | "formContainer" | "dialogTitle" | "progressIndicator" | "dialogActions";
12
13 const styles: StyleRulesCallback<CssRules> = theme => ({
14     button: {
15         marginLeft: theme.spacing.unit
16     },
17     lastButton: {
18         marginLeft: theme.spacing.unit,
19         marginRight: "20px",
20     },
21     formContainer: {
22         display: "flex",
23         flexDirection: "column",
24         marginTop: "20px",
25     },
26     dialogTitle: {
27         paddingBottom: "0"
28     },
29     progressIndicator: {
30         position: "absolute",
31         minWidth: "20px",
32     },
33     dialogActions: {
34         marginBottom: theme.spacing.unit * 3
35     }
36 });
37
38 interface DialogProjectDataProps {
39     cancelLabel?: string;
40     dialogTitle: string;
41     formFields: React.ComponentType<InjectedFormProps<any> & WithDialogProps<any>>;
42     submitLabel?: string;
43 }
44
45 type DialogProjectProps = DialogProjectDataProps & WithDialogProps<{}> & InjectedFormProps<any> & WithStyles<CssRules>;
46
47 export const FormDialog = withStyles(styles)((props: DialogProjectProps) =>
48     <Dialog
49         open={props.open}
50         onClose={props.closeDialog}
51         disableBackdropClick={props.submitting}
52         disableEscapeKeyDown={props.submitting}
53         fullWidth
54         maxWidth='sm'>
55         <form>
56             <DialogTitle className={props.classes.dialogTitle}>
57                 {props.dialogTitle}
58             </DialogTitle>
59             <DialogContent className={props.classes.formContainer}>
60                 <props.formFields {...props} />
61             </DialogContent>
62             <DialogActions className={props.classes.dialogActions}>
63                 <Button
64                     onClick={props.closeDialog}
65                     className={props.classes.button}
66                     color="primary"
67                     disabled={props.submitting}>
68                     {props.cancelLabel || 'Cancel'}
69                 </Button>
70                 <Button
71                     type="submit"
72                     onClick={props.handleSubmit}
73                     className={props.classes.lastButton}
74                     color="primary"
75                     disabled={props.invalid || props.submitting || props.pristine}
76                     variant="contained">
77                     {props.submitLabel || 'Submit'}
78                     {props.submitting && <CircularProgress size={20} className={props.classes.progressIndicator} />}
79                 </Button>
80             </DialogActions>
81         </form>
82     </Dialog>
83 );
84
85