ModalPortal.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * ModalPortal
  3. * @邠心vbe on 2022/02/28
  4. */
  5. import React, { Component } from 'react';
  6. import { StyleSheet, View } from 'react-native';
  7. import Modal from 'react-native-modal';
  8. let modal
  9. export default class ModalPortal extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. showDialog: false,
  14. showLoading: false,
  15. showIOSLoading: false,
  16. children: <></>,
  17. loadChildren: <></>,
  18. loadMessage: "...",
  19. };
  20. modal = this;
  21. this.isHide = true;
  22. }
  23. static show(children) {
  24. return modal.show(children);
  25. }
  26. static showLoading(children) {
  27. return modal.showLoading(children);
  28. }
  29. static dismiss() {
  30. modal.dismiss();
  31. }
  32. static dismissLoading() {
  33. modal.dismissLoading();
  34. }
  35. static dismissAll() {
  36. modal.dismissAll();
  37. }
  38. static isShowing() {
  39. return modal.isShowing();
  40. }
  41. show(children) {
  42. if (isIOS) {
  43. if (!this.isHide) {
  44. setTimeout(() => {
  45. this.show(children);
  46. }, 500);
  47. return;
  48. }
  49. this.setState({
  50. showDialog: true,
  51. children: children,
  52. }, () => {
  53. this.onModalShow();
  54. })
  55. } else {
  56. this.setState({
  57. showDialog: true,
  58. children: children
  59. })
  60. }
  61. }
  62. showLoading(children) {
  63. if (isIOS) {
  64. //console.log("showIOSLoading", children)
  65. this.setState({
  66. loadChildren: children,
  67. showIOSLoading: true
  68. })
  69. } else {
  70. this.setState({
  71. showLoading: true,
  72. loadChildren: children
  73. });
  74. }
  75. }
  76. dismiss() {
  77. this.setState({
  78. showDialog: false
  79. })
  80. }
  81. dismissLoading() {
  82. if (isIOS) {
  83. this.setState({
  84. showIOSLoading: false
  85. })
  86. } else {
  87. this.setState({
  88. showLoading: false
  89. })
  90. }
  91. }
  92. dismissAll() {
  93. this.setState({
  94. showDialog: false,
  95. showLoading: false,
  96. showIOSLoading: false
  97. })
  98. }
  99. isShowing() {
  100. //console.log("[ModalPortal] isShowing", this.state.showDialog);
  101. return this.state.showDialog;
  102. }
  103. onBackPress() {
  104. if (this.state.showLoading) {
  105. this.dismissLoading();
  106. } else if (this.state.showDialog) {
  107. this.dismiss();
  108. }
  109. }
  110. onModalShow() {
  111. //console.log('onModalShow', this.isHide);
  112. this.isHide = false;
  113. }
  114. onModalHide() {
  115. //console.log('onModalHide', this.isHide);
  116. this.isHide = true;
  117. }
  118. render() {
  119. return (
  120. <>
  121. <Modal
  122. style={{zIndex: 900}}
  123. isVisible={this.state.showDialog}
  124. avoidKeyboard={true}
  125. animationIn={"fadeIn"}
  126. animationOut={"fadeOut"}
  127. useNativeDriver={true}
  128. onBackButtonPress={() => this.onBackPress()}
  129. onModalHide={() => this.onModalHide()}
  130. >
  131. {this.state.children}
  132. </Modal>
  133. <Modal
  134. style={{zIndex: 900}}
  135. isVisible={this.state.showLoading}
  136. animationIn="fadeIn"
  137. animationOut="fadeOut"
  138. useNativeDriver={true}
  139. animationInTiming={80}
  140. animationOutTiming={100}
  141. onBackButtonPress={() => this.onBackPress()}
  142. //onModalHide={() => this.onModalHide()}
  143. >
  144. {this.state.loadChildren}
  145. </Modal>
  146. { this.state.showIOSLoading &&
  147. <View style={styles.iosLoadingView}>
  148. {this.state.loadChildren}
  149. </View>
  150. }
  151. </>
  152. );
  153. }
  154. }
  155. const styles = StyleSheet.create({
  156. iosLoadingView: {
  157. top: 0,
  158. left: 0,
  159. right: 0,
  160. bottom: 0,
  161. zIndex: 500,
  162. alignItems: 'center',
  163. position: 'absolute',
  164. justifyContent: 'center',
  165. backgroundColor: 'rgba(0,0,0,.7)'
  166. }
  167. })