点击模式外部以关闭模式(react-native-modal)

hel*_*eer 4 reactjs react-native react-modal

有人有实施经验react-native-modal吗?当我使用它时,当我点击模态之外时,模态不会关闭。

这是我尝试过的

  • 添加 onBackdropPress(() => {this.props.hideModal()})
  • 在组件内部和外部添加 TouchableWithoutFeedback
  • 和许多其他方法...

这是我想要显示模式的屏幕。

render() {
    return (
        <View style={{flex: 1}}>
            <ScrollView>
               // CONTENT HERE
               {this._renderModal()} //rendering modal here
               <FABs onFABsPress={this._showModal} /> // I open Modal when I press the FABs button
            </ScrollView>
        </View>
    )
);

_renderModal = () => {
    return (
      <CameraImageSelectModal
        hideModal={this._hideModal}
        isModalVisible={this.state.isModalVisible}
        navigation={this.props.navigation}
      />
    )
}
Run Code Online (Sandbox Code Playgroud)

这是模态组件:CameraImageSelectModal.js

render() {
    let { isModalVisible } = this.props;
    return (
      <View>
        <Modal
          isVisible={isModalVisible}
          onBackdropPress={() => {console.log('hey')}}>
          transparent={true}>
          <View style={styles.modalContainer}>
            <View style={styles.modalTitleTextContainer}>
              <Text style={styles.modalTitleText}>Hello World</Text>
            </View>
            <View style={styles.modalContentTextContainer}>
              <Text style={styles.modalContentText}></Text>
            </View>
            <View style={styles.modalButtonContainer}>
              <Button transparent onPress={this._handleCameraPress}>
                <Text style={[styles.modalText, styles.black]}>Camera</Text>
              </Button>
              <Button transparent onPress={this._handleAlbumPress}>
                <Text style={styles.modalText}>Album</Text>
              </Button>
            </View>
          </View>
        </Modal>
      </View>
Run Code Online (Sandbox Code Playgroud)

谢谢!!

wiz*_*loc 5

我不认为模态具有内置功能,但您可以创建自己的组件。这是一个快速实现。您可能需要调整填充和边距才能达到您想要的效果,但这将允许在按下外部时关闭模式。

import React, { Component } from "react"
import { Modal, StyleSheet, View, TouchableHighlight } from "react-native"

const styles = StyleSheet.create({
  container: {
    zIndex: 1,
    margin: 25,
    backgroundColor: "white"
  },
  background: {
    flex: 1
  },
  outerContainer: {
    position: "absolute", 
    top: 0, 
    left: 0, 
    right: 0, 
    bottom: 0, 
    justifyContent: "center"
  }
})

const MyModal = props => (
  <Modal transparent={true} animationType={"slide"} visible={props.visible} onRequestClose={() => props.onRequestClose()}>
    <TouchableHighlight style={styles.background} onPress={() => props.onRequestClose()} underlayColor={"transparent"}>
      <View />
    </TouchableHighlight>
    <View style={ styles.outerContainer }>
      <View style={styles.container}>
        {props.children}
      </View>
    </View>
  </Modal>
)

export { MyModal }
Run Code Online (Sandbox Code Playgroud)