Reputation: 1727
I have add comment button when user clicks on add comment button he should be able to add and save comment. For comment save I have used Modal. Now I want to close Modal when user clicks outside Modal how can I do this is React Native ?
I am referring this -> Close react native modal by clicking on overlay? but the solution is not working in my case.
Note: I have added button to close modal but I want to close the modal by clicking outside it.
Code:
<View>
<Modal
animationType="slide"
transparent={true}
visible={this.state.modalVisible}
onRequestClose={() => {this.setModalVisible(false)}}>
<TouchableOpacity
activeOpacity={1}
onPressOut={() => {this.setModalVisible(true)}}
>
<ScrollView
directionalLockEnabled={true}
contentContainerStyle={styles.scrollModal}
>
<TouchableWithoutFeedback>
<View style={styles.commentModal}>
<View style={{marginLeft: 20, marginRight: 20}}>
<Text style={styles.addCommentModal}>Add a Comment</Text>
<View style={styles.textInputModal}>
<TextInput
editable = {true}
maxLength = {40}
multiline = {true}
onChangeText={(text) => this.setState({text})}
value={this.state.comment_text}
onChangeText={(text) => this.setState({comment_text: text})}
style={{borderRadius: 1}}
/>
</View>
<View style={styles.modalBtnContainer}>
<TouchableOpacity onPress={() => this.addComment(this.state.comment_text, logged_in_user.id, marketingPlanId)}>
<Text style={styles.saveCommentModal}>Save Comment</Text>
</TouchableOpacity>
</View>
<View style={styles.modalBtnContainer}>
<TouchableOpacity onPress={() => this.setModalVisible(false)}>
<Text style={styles.closeCommentModal}>Close</Text>
</TouchableOpacity>
</View>
</View>
</View>
</TouchableWithoutFeedback>
</ScrollView>
</TouchableOpacity>
</Modal>
</View>
CSS:
commentModal: {
position: 'absolute',
bottom: 0,
backgroundColor: '#fff',
height: height/2,
width: width
},
addCommentModal: {
fontSize: 18,
color: '#333',
marginTop: 20,
fontFamily: 'bold',
fontWeight: '500'
},
Upvotes: 0
Views: 2973
Reputation: 2280
notice that in the link you posted they are using onPressOut
for <TouchableOpacity/>
component and you are using onPress
, he is also using this at the very beginning if (!this.state.modalVisible) return null
Upvotes: 1
Reputation: 2528
You can add button in modal to close it. Try
<Modal>
....
<TouchableOpacity onPress={() =>this.setState({modalVisible:false}))}>
<Text style={styles.saveCommentModal}>Close</Text>
</TouchableOpacity>
....
</Modal>
Upvotes: 1