ModalUtil.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Created by wuyunqiang on 2017/8/2.
  3. */
  4. import React, { Component, PropTypes } from 'react';
  5. import {
  6. Text,
  7. View,
  8. TouchableOpacity,
  9. StyleSheet,
  10. Platform,
  11. Dimensions,
  12. Image,
  13. Modal,
  14. TextInput,
  15. NativeModules,
  16. InteractionManager,
  17. AppState
  18. } from 'react-native';
  19. import ModalAndroid from '../components/modol';
  20. export default class ModalView extends Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. update: false,
  25. visible: this.props.visible
  26. };
  27. }
  28. componentDidMount() {
  29. AppState.addEventListener('change', this._handleAppStateChange);
  30. }
  31. componentWillReceiveProps(props) {
  32. this.setState({ visible: props.visible });
  33. }
  34. componentWillUnmount() {
  35. AppState.removeEventListener('change', this._handleAppStateChange);
  36. }
  37. _handleAppStateChange = (AppState) => {
  38. console.log('AppState', AppState);
  39. console.log('AppState _handleAppStateChange', this.state.visible);
  40. this.setState({
  41. update: !this.state.update,
  42. visible: this.state.visible
  43. });
  44. };
  45. close = () => {
  46. requestAnimationFrame(() => {
  47. if (this.props.close) {
  48. // console.log("close","执行了父组件的close方法")
  49. this.props.close();
  50. } else {
  51. // console.log("close","执行本组件方法")
  52. this.setState({ visible: false });
  53. }
  54. });
  55. };
  56. renderContent = () => this.props.contentView();
  57. render() {
  58. if (Platform.OS === 'ios') {
  59. return (
  60. <Modal
  61. animationType={this.props.animation ? this.props.animation : 'slide'} // 进场动画 fade
  62. onRequestClose={() => this.close()}
  63. visible={this.state.visible} // 是否可见
  64. transparent
  65. >
  66. <TouchableOpacity style={{ flex: 1 }} activeOpacity={1} onPress={this.close}>
  67. <View style={[ styles.container, this.props.customerlayout ]}>{this.renderContent()}</View>
  68. </TouchableOpacity>
  69. </Modal>
  70. );
  71. }
  72. return (
  73. <ModalAndroid
  74. style={{ width: 0, height: 0 }} //避免显示空白
  75. ref={(modalAndroid) => {
  76. this.modalAndroid = modalAndroid;
  77. }}
  78. visible={[ this.state.visible, this.state.update ]}
  79. >
  80. <TouchableOpacity activeOpacity={1} onPress={this.close}>
  81. <View style={[ styles.container, this.props.customerlayout ]}>{this.renderContent()}</View>
  82. </TouchableOpacity>
  83. </ModalAndroid>
  84. );
  85. }
  86. }
  87. const styles = StyleSheet.create({
  88. container: {
  89. flex: 1,
  90. position: 'absolute',
  91. top: 0,
  92. bottom: 0,
  93. left: 0,
  94. right: 0,
  95. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  96. justifyContent: 'center',
  97. alignItems: 'center'
  98. }
  99. });