| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /**
- * ModalPortal
- * @邠心vbe on 2022/02/28
- */
- import React, { Component } from 'react';
- import { StyleSheet, View } from 'react-native';
- import Modal from 'react-native-modal';
- let modal
- export default class ModalPortal extends Component {
- constructor(props) {
- super(props);
- this.state = {
- showDialog: false,
- showLoading: false,
- showIOSLoading: false,
- children: <></>,
- loadChildren: <></>,
- loadMessage: "...",
- };
- modal = this;
- this.isHide = true;
- }
- static show(children) {
- return modal.show(children);
- }
- static showLoading(children) {
- return modal.showLoading(children);
- }
- static dismiss() {
- modal.dismiss();
- }
- static dismissLoading() {
- modal.dismissLoading();
- }
- static dismissAll() {
- modal.dismissAll();
- }
- static isShowing() {
- return modal.isShowing();
- }
- show(children) {
- if (isIOS) {
- if (!this.isHide) {
- setTimeout(() => {
- this.show(children);
- }, 500);
- return;
- }
- this.setState({
- showDialog: true,
- children: children,
- }, () => {
- this.onModalShow();
- })
- } else {
- this.setState({
- showDialog: true,
- children: children
- })
- }
- }
- showLoading(children) {
- if (isIOS) {
- //console.log("showIOSLoading", children)
- this.setState({
- loadChildren: children,
- showIOSLoading: true
- })
- } else {
- this.setState({
- showLoading: true,
- loadChildren: children
- });
- }
- }
- dismiss() {
- this.setState({
- showDialog: false
- })
- }
- dismissLoading() {
- if (isIOS) {
- this.setState({
- showIOSLoading: false
- })
- } else {
- this.setState({
- showLoading: false
- })
- }
- }
- dismissAll() {
- this.setState({
- showDialog: false,
- showLoading: false,
- showIOSLoading: false
- })
- }
- isShowing() {
- //console.log("[ModalPortal] isShowing", this.state.showDialog);
- return this.state.showDialog;
- }
- onBackPress() {
- if (this.state.showLoading) {
- this.dismissLoading();
- } else if (this.state.showDialog) {
- this.dismiss();
- }
- }
- onModalShow() {
- //console.log('onModalShow', this.isHide);
- this.isHide = false;
- }
- onModalHide() {
- //console.log('onModalHide', this.isHide);
- this.isHide = true;
- }
- render() {
- return (
- <>
- <Modal
- style={{zIndex: 900}}
- isVisible={this.state.showDialog}
- avoidKeyboard={true}
- animationIn={"fadeIn"}
- animationOut={"fadeOut"}
- useNativeDriver={true}
- onBackButtonPress={() => this.onBackPress()}
- onModalHide={() => this.onModalHide()}
- >
- {this.state.children}
- </Modal>
- <Modal
- style={{zIndex: 900}}
- isVisible={this.state.showLoading}
- animationIn="fadeIn"
- animationOut="fadeOut"
- useNativeDriver={true}
- animationInTiming={80}
- animationOutTiming={100}
- onBackButtonPress={() => this.onBackPress()}
- //onModalHide={() => this.onModalHide()}
- >
- {this.state.loadChildren}
- </Modal>
- { this.state.showIOSLoading &&
- <View style={styles.iosLoadingView}>
- {this.state.loadChildren}
- </View>
- }
- </>
- );
- }
- }
- const styles = StyleSheet.create({
- iosLoadingView: {
- top: 0,
- left: 0,
- right: 0,
- bottom: 0,
- zIndex: 500,
- alignItems: 'center',
- position: 'absolute',
- justifyContent: 'center',
- backgroundColor: 'rgba(0,0,0,.7)'
- }
- })
|