index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. Breakpoint,
  3. Button,
  4. DialogActions,
  5. DialogContent,
  6. DialogProps,
  7. Typography,
  8. } from "@mui/material";
  9. import { t } from "i18next";
  10. import React from "react";
  11. import DialogIcon from "./DialogIcon";
  12. import DialogTitleWithCloseButton, {
  13. dialogCloseHandler,
  14. } from "./TitleWithCloseButton";
  15. import DialogBoxBase from "./base";
  16. import { DialogBoxAttributes } from "./types";
  17. type IProps = React.PropsWithChildren<
  18. Omit<DialogProps, "onClose" | "maxSize"> & {
  19. onClose: () => void;
  20. attributes: DialogBoxAttributes;
  21. size?: Breakpoint;
  22. titleCloseButton?: boolean;
  23. }
  24. >;
  25. export default function DialogBox({
  26. attributes,
  27. children,
  28. open,
  29. size,
  30. onClose,
  31. titleCloseButton,
  32. ...props
  33. }: IProps) {
  34. if (!attributes) {
  35. return <></>;
  36. }
  37. const handleClose = dialogCloseHandler({
  38. staticBackdrop: attributes.staticBackdrop,
  39. nonClosable: attributes.nonClosable,
  40. onClose: onClose,
  41. });
  42. return (
  43. <DialogBoxBase
  44. open={open}
  45. maxWidth={size}
  46. onClose={handleClose}
  47. {...props}
  48. >
  49. {attributes.icon && <DialogIcon icon={attributes.icon} />}
  50. {attributes.title && (
  51. <DialogTitleWithCloseButton
  52. onClose={
  53. titleCloseButton &&
  54. !attributes.nonClosable &&
  55. handleClose
  56. }
  57. >
  58. {attributes.title}
  59. </DialogTitleWithCloseButton>
  60. )}
  61. {(children || attributes?.content) && (
  62. <DialogContent>
  63. {children || (
  64. <Typography color="text.muted">
  65. {attributes.content}
  66. </Typography>
  67. )}
  68. </DialogContent>
  69. )}
  70. {(attributes.close || attributes.proceed) && (
  71. <DialogActions>
  72. <>
  73. {attributes.close && (
  74. <Button
  75. size="large"
  76. color={attributes.close?.variant ?? "secondary"}
  77. onClick={() => {
  78. attributes.close.action &&
  79. attributes.close?.action();
  80. onClose();
  81. }}
  82. >
  83. {attributes.close?.text ?? t("OK")}
  84. </Button>
  85. )}
  86. {attributes.proceed && (
  87. <Button
  88. size="large"
  89. color={attributes.proceed?.variant}
  90. onClick={() => {
  91. attributes.proceed.action();
  92. onClose();
  93. }}
  94. disabled={attributes.proceed.disabled}
  95. >
  96. {attributes.proceed.text}
  97. </Button>
  98. )}
  99. {attributes.secondary && (
  100. <Button
  101. size="large"
  102. color={attributes.secondary?.variant}
  103. onClick={() => {
  104. attributes.secondary.action();
  105. onClose();
  106. }}
  107. disabled={attributes.secondary.disabled}
  108. >
  109. {attributes.secondary.text}
  110. </Button>
  111. )}
  112. </>
  113. </DialogActions>
  114. )}
  115. </DialogBoxBase>
  116. );
  117. }