Merge branch '17119-support-filter-groups'
[arvados.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: "0",
20     },
21     formContainer: {
22         display: "flex",
23         flexDirection: "column",
24         paddingBottom: "0",
25     },
26     dialogTitle: {
27         paddingTop: theme.spacing.unit,
28         paddingBottom: theme.spacing.unit,
29     },
30     progressIndicator: {
31         position: "absolute",
32         minWidth: "20px",
33     },
34     dialogActions: {
35         marginBottom: theme.spacing.unit,
36         marginRight: theme.spacing.unit * 3,
37     }
38 });
39
40 interface DialogProjectDataProps {
41     cancelLabel?: string;
42     dialogTitle: string;
43     formFields: React.ComponentType<InjectedFormProps<any> & WithDialogProps<any>>;
44     submitLabel?: string;
45 }
46
47 type DialogProjectProps = DialogProjectDataProps & WithDialogProps<{}> & InjectedFormProps<any> & WithStyles<CssRules>;
48
49 export const FormDialog = withStyles(styles)((props: DialogProjectProps) =>
50     <Dialog
51         open={props.open}
52         onClose={props.closeDialog}
53         disableBackdropClick
54         disableEscapeKeyDown={props.submitting}
55         fullWidth
56         maxWidth='md'>
57         <form data-cy='form-dialog'>
58             <DialogTitle className={props.classes.dialogTitle}>
59                 {props.dialogTitle}
60             </DialogTitle>
61             <DialogContent className={props.classes.formContainer}>
62                 <props.formFields {...props} />
63             </DialogContent>
64             <DialogActions className={props.classes.dialogActions}>
65                 <Button
66                     data-cy='form-cancel-btn'
67                     onClick={props.closeDialog}
68                     className={props.classes.button}
69                     color="primary"
70                     disabled={props.submitting}>
71                     {props.cancelLabel || 'Cancel'}
72                 </Button>
73                 <Button
74                     data-cy='form-submit-btn'
75                     type="submit"
76                     onClick={props.handleSubmit}
77                     className={props.classes.lastButton}
78                     color="primary"
79                     disabled={props.invalid || props.submitting || props.pristine}
80                     variant="contained">
81                     {props.submitLabel || 'Submit'}
82                     {props.submitting && <CircularProgress size={20} className={props.classes.progressIndicator} />}
83                 </Button>
84             </DialogActions>
85         </form>
86     </Dialog>
87 );
88
89