ModalPortal.js 3.7 KB

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