DialogTemplate.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { FC, useEffect, useRef, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import {
  4. DialogContent,
  5. DialogActions,
  6. makeStyles,
  7. Theme,
  8. } from '@material-ui/core';
  9. import { DialogContainerProps } from './Types';
  10. import CustomDialogTitle from './CustomDialogTitle';
  11. import CustomButton from '../customButton/CustomButton';
  12. import CodeView from '../code/CodeView';
  13. const useStyles = makeStyles((theme: Theme) => ({
  14. wrapper: {
  15. display: 'flex',
  16. '& form': {
  17. display: 'flex',
  18. },
  19. '& .MuiDialogContent-root': {
  20. maxHeight: '60vh',
  21. },
  22. },
  23. block: {
  24. borderRadius: 8,
  25. backgroundColor: '#fff',
  26. },
  27. dialog: {
  28. minWidth: 480,
  29. },
  30. codeWrapper: {
  31. width: (props: { showCode: boolean }) => (props.showCode ? 480 : 0),
  32. transition: 'width 0.2s',
  33. },
  34. code: {
  35. height: '100%',
  36. // set code view padding 0 if not show
  37. padding: (props: { showCode: boolean }) =>
  38. props.showCode ? theme.spacing(4) : 0,
  39. },
  40. actions: {
  41. paddingTop: theme.spacing(2),
  42. justifyContent: 'space-between',
  43. },
  44. }));
  45. const DialogTemplate: FC<DialogContainerProps> = ({
  46. title,
  47. cancelLabel,
  48. handleClose,
  49. handleCancel,
  50. confirmLabel,
  51. handleConfirm,
  52. confirmDisabled,
  53. children,
  54. showActions = true,
  55. showCancel = true,
  56. showCloseIcon = true,
  57. leftActions,
  58. // needed for code mode
  59. showCode = false,
  60. codeBlocksData = [],
  61. dialogClass = '',
  62. }) => {
  63. const { t } = useTranslation('btn');
  64. const cancel = cancelLabel || t('cancel');
  65. const confirm = confirmLabel || t('confirm');
  66. const classes = useStyles({ showCode });
  67. const onCancel = handleCancel || handleClose;
  68. const dialogRef = useRef(null);
  69. const _handleConfirm = (event: React.FormEvent<HTMLFormElement>) => {
  70. handleConfirm();
  71. event.preventDefault();
  72. };
  73. return (
  74. <section className={classes.wrapper}>
  75. <form onSubmit={_handleConfirm}>
  76. <div
  77. ref={dialogRef}
  78. className={`${classes.dialog} ${classes.block} ${dialogClass}`}
  79. >
  80. <CustomDialogTitle
  81. onClose={handleClose}
  82. showCloseIcon={showCloseIcon}
  83. >
  84. {title}
  85. </CustomDialogTitle>
  86. <DialogContent>{children}</DialogContent>
  87. {showActions && (
  88. <DialogActions className={classes.actions}>
  89. <div>{leftActions}</div>
  90. <div>
  91. {showCancel && (
  92. <CustomButton
  93. onClick={onCancel}
  94. color="default"
  95. name="cancel"
  96. >
  97. {cancel}
  98. </CustomButton>
  99. )}
  100. <CustomButton
  101. type="submit"
  102. variant="contained"
  103. color="primary"
  104. disabled={confirmDisabled}
  105. name="confirm"
  106. >
  107. {confirm}
  108. </CustomButton>
  109. </div>
  110. </DialogActions>
  111. )}
  112. </div>
  113. <div className={`${classes.block} ${classes.codeWrapper}`}>
  114. {showCode && (
  115. <CodeView wrapperClass={classes.code} data={codeBlocksData} />
  116. )}
  117. </div>
  118. </form>
  119. </section>
  120. );
  121. };
  122. export default DialogTemplate;