index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from 'react';
  2. import constants from 'utils/strings/constants';
  3. import {
  4. Breakpoint,
  5. Button,
  6. Dialog,
  7. DialogActions,
  8. DialogContent,
  9. DialogProps,
  10. } from '@mui/material';
  11. import DialogTitleWithCloseButton from './titleWithCloseButton';
  12. import MessageText from './messageText';
  13. import DialogBoxBase from './base';
  14. import { DialogBoxAttributes } from 'types/dialogBox';
  15. type IProps = React.PropsWithChildren<
  16. Omit<DialogProps, 'onClose' | 'maxSize'> & {
  17. onClose: () => void;
  18. attributes: DialogBoxAttributes;
  19. size?: Breakpoint;
  20. titleCloseButton?: boolean;
  21. }
  22. >;
  23. export default function DialogBox({ attributes, children, ...props }: IProps) {
  24. if (!attributes) {
  25. return <Dialog open={false} />;
  26. }
  27. const handleClose: DialogProps['onClose'] = (_, reason) => {
  28. if (attributes?.nonClosable) {
  29. // no-op
  30. } else if (attributes?.staticBackdrop && reason === 'backdropClick') {
  31. // no-op
  32. } else {
  33. props.onClose();
  34. }
  35. };
  36. return (
  37. <DialogBoxBase
  38. open={props.open}
  39. maxWidth={props.size}
  40. onClose={handleClose}
  41. {...props}>
  42. {attributes.title && (
  43. <DialogTitleWithCloseButton
  44. onClose={
  45. !attributes?.nonClosable &&
  46. props.titleCloseButton &&
  47. handleClose
  48. }>
  49. {attributes.title}
  50. </DialogTitleWithCloseButton>
  51. )}
  52. {(children || attributes?.content) && (
  53. <DialogContent>
  54. {children || (
  55. <MessageText>{attributes.content}</MessageText>
  56. )}
  57. </DialogContent>
  58. )}
  59. {(attributes.close || attributes.proceed) && (
  60. <DialogActions>
  61. <>
  62. {attributes.close && (
  63. <Button
  64. color={attributes.close?.variant ?? 'secondary'}
  65. onClick={() => {
  66. attributes.close.action &&
  67. attributes.close?.action();
  68. props.onClose();
  69. }}>
  70. {attributes.close?.text ?? constants.OK}
  71. </Button>
  72. )}
  73. {attributes.proceed && (
  74. <Button
  75. color={attributes.proceed?.variant}
  76. onClick={() => {
  77. attributes.proceed.action();
  78. props.onClose();
  79. }}
  80. disabled={attributes.proceed.disabled}>
  81. {attributes.proceed.text}
  82. </Button>
  83. )}
  84. </>
  85. </DialogActions>
  86. )}
  87. </DialogBoxBase>
  88. );
  89. }