CustomDialog.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { FC } from 'react';
  2. import {
  3. DialogActions,
  4. DialogContent,
  5. Dialog,
  6. makeStyles,
  7. Theme,
  8. createStyles,
  9. Typography,
  10. } from '@material-ui/core';
  11. import { CustomDialogType } from './Types';
  12. import { useTranslation } from 'react-i18next';
  13. import CustomButton from '../customButton/CustomButton';
  14. import CustomDialogTitle from './CustomDialogTitle';
  15. const useStyles = makeStyles((theme: Theme) =>
  16. createStyles({
  17. paper: {
  18. minWidth: 480,
  19. borderRadius: 8,
  20. padding: 0,
  21. backgroundColor: 'transparent',
  22. },
  23. noticePaper: {
  24. backgroundColor: '#fff',
  25. maxWidth: 480,
  26. },
  27. paperSm: {
  28. maxWidth: '80%',
  29. },
  30. dialogContent: {
  31. marginTop: theme.spacing(2),
  32. },
  33. title: {
  34. '& p': {
  35. fontWeight: 500,
  36. overflow: 'hidden',
  37. textOverflow: 'ellipsis',
  38. whiteSpace: 'nowrap',
  39. maxWidth: 300,
  40. fontSize: 20,
  41. },
  42. },
  43. padding: {
  44. padding: theme.spacing(3, 4, 4),
  45. },
  46. cancel: {
  47. color: theme.palette.common.black,
  48. opacity: 0.4,
  49. },
  50. })
  51. );
  52. const CustomDialog: FC<CustomDialogType> = props => {
  53. const { t } = useTranslation('btn');
  54. const classes = useStyles();
  55. const { open, type, params, onClose, containerClass = '' } = props;
  56. const {
  57. title,
  58. component,
  59. confirm,
  60. confirmLabel = t('confirm'),
  61. cancel,
  62. cancelLabel = t('cancel'),
  63. confirmClass = '',
  64. handleClose,
  65. } = params; // for notice type
  66. const { component: CustomComponent } = params; // for custom type
  67. const handleConfirm = async () => {
  68. if (confirm) {
  69. const res = await confirm();
  70. if (!res) {
  71. return;
  72. }
  73. }
  74. handleClose ? handleClose() : onClose();
  75. };
  76. const handleCancel = async () => {
  77. if (cancel) {
  78. const res = await cancel();
  79. if (!res) {
  80. return;
  81. }
  82. }
  83. handleClose ? handleClose() : onClose();
  84. };
  85. return (
  86. <Dialog
  87. classes={{
  88. paper: `${classes.paper} ${
  89. type === 'notice' ? classes.noticePaper : ''
  90. }`,
  91. paperWidthSm: type === 'notice' ? '' : classes.paperSm,
  92. container: `${containerClass}`,
  93. }}
  94. open={open}
  95. onClose={handleCancel}
  96. >
  97. {type === 'notice' ? (
  98. <>
  99. <CustomDialogTitle
  100. classes={{ root: classes.title }}
  101. onClose={handleCancel}
  102. >
  103. <Typography variant="body1">{title}</Typography>
  104. </CustomDialogTitle>
  105. {component && (
  106. <DialogContent classes={{ root: classes.dialogContent }}>
  107. {component}
  108. </DialogContent>
  109. )}
  110. <DialogActions classes={{ spacing: classes.padding }}>
  111. <CustomButton
  112. onClick={() => handleCancel()}
  113. className={classes.cancel}
  114. color="default"
  115. >
  116. {cancelLabel}
  117. </CustomButton>
  118. <CustomButton
  119. onClick={() => handleConfirm()}
  120. color="primary"
  121. variant="contained"
  122. className={confirmClass}
  123. >
  124. {confirmLabel}
  125. </CustomButton>
  126. </DialogActions>
  127. </>
  128. ) : (
  129. CustomComponent
  130. )}
  131. </Dialog>
  132. );
  133. };
  134. export default CustomDialog;