Dialog.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import React, { useEffect } from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. makeStyles,
  5. Theme,
  6. createStyles,
  7. Typography,
  8. Button,
  9. IconButton,
  10. Dialog,
  11. DialogActions,
  12. DialogContent,
  13. DialogTitle,
  14. } from '@material-ui/core';
  15. import CloseIcon from '@material-ui/icons/Close';
  16. import CachedIcon from '@material-ui/icons/Cached';
  17. import ConditionGroup from './ConditionGroup';
  18. import CopyBtn from './CopyButton';
  19. interface DialogProps {
  20. others?: object;
  21. open: boolean;
  22. onClose: () => void;
  23. onSubmit: (data: any) => void;
  24. onReset: () => void;
  25. onCancel: () => void;
  26. title: string;
  27. fields: Field[];
  28. handleConditions: any;
  29. conditions: any[];
  30. isLegal: boolean;
  31. expression: string;
  32. }
  33. interface Field {
  34. name: string;
  35. type: 'int' | 'float';
  36. }
  37. const AdvancedDialog = React.forwardRef((props: DialogProps, ref) => {
  38. const {
  39. open,
  40. onClose,
  41. onSubmit,
  42. onReset,
  43. onCancel,
  44. handleConditions,
  45. conditions: flatConditions,
  46. isLegal,
  47. expression: filterExpression,
  48. title,
  49. fields,
  50. ...others
  51. } = props;
  52. const { addCondition } = handleConditions;
  53. const classes = useStyles();
  54. useEffect(() => {
  55. flatConditions.length === 0 && addCondition();
  56. // eslint-disable-next-line react-hooks/exhaustive-deps
  57. }, []);
  58. // const childRef: any = useRef();
  59. return (
  60. <>
  61. <Dialog
  62. onClose={onClose}
  63. aria-labelledby="customized-dialog-title"
  64. open={open}
  65. maxWidth="xl"
  66. className={classes.wrapper}
  67. {...others}
  68. >
  69. <DialogTitle className={classes.dialogTitle} disableTypography>
  70. <Typography variant="h5" component="h2">
  71. {title}
  72. </Typography>
  73. <IconButton
  74. aria-label="close"
  75. className={classes.closeButton}
  76. onClick={onCancel}
  77. size="small"
  78. >
  79. <CloseIcon />
  80. </IconButton>
  81. </DialogTitle>
  82. <DialogContent>
  83. <div
  84. className={`${classes.expResult} ${
  85. !isLegal && 'disable-exp'
  86. } testcopy`}
  87. >
  88. {`${isLegal ? filterExpression : 'Filter Expression'}`}
  89. {isLegal && (<CopyBtn label="copy expression" value={filterExpression} />)}
  90. </div>
  91. <div className={classes.expWrapper}>
  92. <ConditionGroup
  93. // ref={childRef}
  94. fields={fields}
  95. handleConditions={handleConditions}
  96. conditions={flatConditions}
  97. />
  98. </div>
  99. </DialogContent>
  100. <DialogActions className={classes.dialogActions}>
  101. <Button
  102. onClick={onReset}
  103. color="primary"
  104. className={classes.resetBtn}
  105. size="small"
  106. >
  107. <CachedIcon />
  108. Reset
  109. </Button>
  110. <div>
  111. <Button
  112. autoFocus
  113. onClick={onCancel}
  114. color="primary"
  115. className={classes.cancelBtn}
  116. >
  117. Cancel
  118. </Button>
  119. <Button
  120. onClick={onSubmit}
  121. variant="contained"
  122. color="primary"
  123. className={classes.applyBtn}
  124. disabled={!isLegal}
  125. >
  126. Apply Filters
  127. </Button>
  128. </div>
  129. </DialogActions>
  130. </Dialog>
  131. </>
  132. );
  133. });
  134. AdvancedDialog.propTypes = {
  135. others: PropTypes.object,
  136. open: PropTypes.bool.isRequired,
  137. onClose: PropTypes.func.isRequired,
  138. onSubmit: PropTypes.func.isRequired,
  139. title: PropTypes.string.isRequired,
  140. onReset: PropTypes.func.isRequired,
  141. onCancel: PropTypes.func.isRequired,
  142. handleConditions: PropTypes.object.isRequired,
  143. conditions: PropTypes.array.isRequired,
  144. isLegal: PropTypes.bool.isRequired,
  145. expression: PropTypes.string.isRequired,
  146. };
  147. AdvancedDialog.defaultProps = {
  148. open: false,
  149. onClose: () => {},
  150. onSubmit: () => {},
  151. title: 'Advanced Filter',
  152. fields: [],
  153. onReset: () => {},
  154. onCancel: () => {},
  155. handleConditions: {},
  156. conditions: [],
  157. isLegal: false,
  158. expression: '',
  159. };
  160. AdvancedDialog.displayName = 'AdvancedDialog';
  161. const useStyles = makeStyles((theme: Theme) =>
  162. createStyles({
  163. root: {},
  164. wrapper: {
  165. '& .disable-exp': {
  166. userSelect: 'none',
  167. color: '#AEAEBB',
  168. },
  169. },
  170. closeButton: {
  171. color: 'black',
  172. },
  173. dialogTitle: {
  174. display: 'flex',
  175. alignItems: 'center',
  176. justifyContent: 'space-between',
  177. },
  178. dialogActions: {
  179. justifyContent: 'space-between',
  180. },
  181. resetBtn: {},
  182. cancelBtn: {
  183. marginRight: '32px',
  184. },
  185. applyBtn: {
  186. backgroundColor: '#06AFF2',
  187. color: 'white',
  188. },
  189. copyButton: {
  190. borderRadius: '0',
  191. },
  192. expResult: {
  193. background: '#F9F9F9',
  194. borderRadius: '8px',
  195. display: 'flex',
  196. justifyContent: 'space-between',
  197. alignItems: 'center',
  198. minHeight: '40px',
  199. marginBottom: '16px',
  200. padding: '0 16px',
  201. fontFamily: 'Source Code Pro',
  202. fontStyle: 'normal',
  203. fontWeight: 'normal',
  204. fontSize: '16px',
  205. lineHeight: '24px',
  206. },
  207. expWrapper: {
  208. background: '#F9F9F9',
  209. borderRadius: '8px',
  210. minWidth: '480px',
  211. minHeight: '104px',
  212. padding: '12px',
  213. },
  214. })
  215. );
  216. export default AdvancedDialog;