index.jsx 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Component } from "react";
  2. import Modal from "@mui/material/Modal";
  3. import Fade from "@mui/material/Fade";
  4. import { ClickAwayListener } from "@mui/base/ClickAwayListener";
  5. import Grow from "@mui/material/Grow";
  6. import styles from "./styles.module.scss";
  7. import MainButton from "@components/Buttons/MainButton";
  8. const i18n = global.i18n;
  9. class MsgBox extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. isShow: false,
  14. title: "",
  15. content: '',
  16. sureLabel: '',
  17. cancelLabel: '',
  18. onlySure: false,
  19. isLockClose: false,
  20. };
  21. }
  22. show({ title = '', content = '', sureCB = null, cancelCB = null, sureLabel = '', cancelLabel = '', onlySure = false, isLockClose = false }) {
  23. if (title === '') title = i18n.t('msgbox.title');
  24. if (sureLabel === '') sureLabel = i18n.t('msgbox.sure_label');
  25. if (cancelLabel === '') cancelLabel = i18n.t('msgbox.cancel_label');
  26. this.sureCB = sureCB;
  27. this.cancelCB = cancelCB;
  28. this.setState({
  29. isShow: true,
  30. title: title,
  31. content: content,
  32. sureLabel: sureLabel,
  33. cancelLabel: cancelLabel,
  34. onlySure: onlySure,
  35. isLockClose: isLockClose
  36. })
  37. }
  38. hide() {
  39. this.setState({
  40. isShow: false,
  41. })
  42. }
  43. onClickBtn(type) {
  44. if (type === 1) {
  45. if (this.sureCB != null) this.sureCB();
  46. this.hide();
  47. }
  48. else {
  49. if (this.cancelCB != null) this.cancelCB(type === 0);
  50. if (!this.state.isLockClose) this.hide();
  51. }
  52. }
  53. closeMsgBox() {
  54. this.onClickBtn(-1);
  55. }
  56. onClickSure() {
  57. this.onClickBtn(1);
  58. }
  59. onClickCancel() {
  60. this.onClickBtn(0);
  61. }
  62. render() {
  63. return (
  64. <Modal open={this.state.isShow} onClose={this.closeMsgBox.bind(this)} closeAfterTransition>
  65. <Fade in={this.state.isShow} timeout={500}>
  66. <div className="popup-container">
  67. <ClickAwayListener onClickAway={this.closeMsgBox.bind(this)}>
  68. <Grow in={this.state.isShow} timeout={500}>
  69. <div className={`popup card card-padded ${styles.msg_box}`}>
  70. <div className={styles.msg_content}>
  71. <div className={styles.title}>{this.state.title}</div>
  72. <div className={styles.content}>{this.state.content}</div>
  73. <div className={styles.msg_btn}>
  74. {
  75. this.state.onlySure ? (<></>) : (
  76. <MainButton grey onClick={this.onClickCancel.bind(this)} size="md">
  77. {this.state.cancelLabel}
  78. </MainButton>
  79. )
  80. }
  81. <MainButton onClick={this.onClickSure.bind(this)} size="md">
  82. {this.state.sureLabel}
  83. </MainButton>
  84. </div>
  85. </div>
  86. </div>
  87. </Grow>
  88. </ClickAwayListener>
  89. </div>
  90. </Fade>
  91. </Modal>
  92. )
  93. }
  94. }
  95. export default MsgBox;