ModalPortal.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * ModalPortal
  3. * @邠心vbe on 2022/02/28
  4. */
  5. import React, { Component } from 'react';
  6. import Modal from 'react-native-modal';
  7. let modal
  8. export default class ModalPortal extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. showDialog: false,
  13. showLoading: false,
  14. children: <></>,
  15. loadChildren: <></>
  16. };
  17. modal = this;
  18. this.afterHide = undefined;
  19. }
  20. static show(children) {
  21. return modal.show(children);
  22. }
  23. static showLoading(children) {
  24. return modal.showLoading(children);
  25. }
  26. static dismiss() {
  27. modal.dismiss();
  28. }
  29. static dismissLoading() {
  30. modal.dismissLoading();
  31. }
  32. static dismissAll() {
  33. modal.dismissAll();
  34. }
  35. show(children) {
  36. if (isIOS) {
  37. this.setState({
  38. showDialog: true,
  39. children: children,
  40. })
  41. } else {
  42. this.setState({
  43. showDialog: true,
  44. children: children
  45. })
  46. }
  47. }
  48. showLoading(children) {
  49. if (isIOS) {
  50. this.setState({
  51. showDialog: true,
  52. //showLoading: true,
  53. children: children,
  54. //loadChildren: children
  55. });
  56. } else {
  57. this.setState({
  58. showLoading: true,
  59. loadChildren: children
  60. });
  61. }
  62. }
  63. dismiss() {
  64. this.setState({
  65. showDialog: false
  66. })
  67. }
  68. dismissLoading() {
  69. if (isIOS) {
  70. this.setState({
  71. showDialog: false
  72. })
  73. } else {
  74. this.setState({
  75. showLoading: false
  76. })
  77. }
  78. }
  79. dismissAll() {
  80. this.setState({
  81. showDialog: false,
  82. showLoading: false
  83. })
  84. }
  85. onBackPress() {
  86. if (this.state.showLoading) {
  87. this.dismissLoading();
  88. } else if (this.state.showDialog) {
  89. this.dismiss();
  90. }
  91. }
  92. onModalHide() {
  93. console.log('onModalHide', this.afterHide);
  94. if (this.afterHide) {
  95. console.log('onModalHide');
  96. this.afterHide();
  97. this.afterHide = undefined;
  98. }
  99. }
  100. render() {
  101. return (
  102. <>
  103. <Modal
  104. style={{zIndex: 900}}
  105. isVisible={this.state.showDialog}
  106. avoidKeyboard={true}
  107. animationIn={"fadeIn"}
  108. animationOut={"fadeOut"}
  109. useNativeDriver={true}
  110. onBackButtonPress={() => this.onBackPress()}
  111. //onModalHide={() => this.onModalHide()}
  112. >
  113. {this.state.children}
  114. </Modal>
  115. <Modal
  116. style={{zIndex: 900}}
  117. isVisible={this.state.showLoading}
  118. animationIn="fadeIn"
  119. animationOut="fadeOut"
  120. useNativeDriver={true}
  121. animationInTiming={80}
  122. animationOutTiming={100}
  123. onBackButtonPress={() => this.onBackPress()}
  124. //onModalHide={() => this.onModalHide()}
  125. >
  126. {this.state.loadChildren}
  127. </Modal>
  128. </>
  129. );
  130. }
  131. }