Rewrite edit project - add dialog form, actions, remove old code
[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: "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: "24px"
35     }
36 });
37
38 interface DialogProjectProps {
39     cancelLabel?: string;
40     dialogTitle: string;
41     formFields: React.ComponentType<InjectedFormProps<any> & WithDialogProps<any>>;
42     submitLabel?: string;
43 }
44
45 export const FormDialog = withStyles(styles)((props: DialogProjectProps & WithDialogProps<{}> & InjectedFormProps<any> & WithStyles<CssRules>) =>
46     <Dialog
47         open={props.open}
48         onClose={props.closeDialog}
49         disableBackdropClick={props.submitting}
50         disableEscapeKeyDown={props.submitting}
51         fullWidth
52         maxWidth='sm'>
53         <form>
54             <DialogTitle className={props.classes.dialogTitle}>
55                 {props.dialogTitle}
56             </DialogTitle>
57             <DialogContent className={props.classes.formContainer}>
58                 <props.formFields {...props} />
59             </DialogContent>
60             <DialogActions className={props.classes.dialogActions}>
61                 <Button
62                     onClick={props.closeDialog}
63                     className={props.classes.button}
64                     color="primary"
65                     disabled={props.submitting}>
66                     {props.cancelLabel || 'Cancel'}
67                 </Button>
68                 <Button
69                     onClick={props.handleSubmit}
70                     className={props.classes.lastButton}
71                     color="primary"
72                     disabled={props.invalid || props.submitting || props.pristine}
73                     variant="contained">
74                     {props.submitLabel || 'Submit'}
75                     {props.submitting && <CircularProgress size={20} className={props.classes.progressIndicator} />}
76                 </Button>
77             </DialogActions>
78         </form>
79     </Dialog>
80 );
81
82