Modal.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. |--------------------------------------------------
  3. | Modal base modal
  4. |--------------------------------------------------
  5. */
  6. import React from 'react'
  7. import {
  8. Modal,
  9. View,
  10. Text,
  11. TouchableOpacity,
  12. Dimensions,
  13. StyleSheet,
  14. } from 'react-native'
  15. export default class ModalEcx extends React.Component {
  16. constructor(props) {
  17. super(props)
  18. this.state = {
  19. show: this.props.show || false,
  20. }
  21. }
  22. componentWillReceiveProps(props) {
  23. if (props.hasOwnProperty('show') && this.state.show !== props.show) {
  24. this.setState({
  25. show: props.show,
  26. })
  27. }
  28. }
  29. _setModalVisible(va, cb) {
  30. this.setState({ show: va }, () => {
  31. cb && cb()
  32. if (!va) {
  33. this._onModalClose()
  34. }
  35. })
  36. this.props.syncState && this.props.syncState(va)
  37. }
  38. _onModalClose() {
  39. this.props.onModalClose && this.props.onModalClose()
  40. }
  41. _renderDefaultContent() {
  42. let height = 100,
  43. top = (Dimensions.get('window').height - height) / 2
  44. return (
  45. <View style={[styles.contentBox, { height: height, top: top }]}>
  46. <Text>Best Guy!</Text>
  47. </View>
  48. )
  49. }
  50. render() {
  51. return (
  52. <Modal
  53. animationType={this.props.animationType}
  54. transparent={true}
  55. visible={this.state.show}
  56. onRequestClose={() => {}}
  57. >
  58. <TouchableOpacity
  59. activeOpacity={1}
  60. style={styles.shandowBox}
  61. onPress={() => {
  62. this._setModalVisible(false)
  63. }}
  64. />
  65. {this.props.content ? this.props.content : this._renderDefaultContent()}
  66. </Modal>
  67. )
  68. }
  69. }
  70. const styles = StyleSheet.create({
  71. contentBox: {
  72. position: 'absolute',
  73. top: '25%',
  74. zIndex: 100,
  75. marginHorizontal: '10%',
  76. width: '80%',
  77. height: '50%',
  78. backgroundColor: '#fff',
  79. paddingHorizontal: 20,
  80. paddingVertical: 30,
  81. },
  82. shandowBox: {
  83. flex: 1,
  84. alignItems: 'center',
  85. justifyContent: 'center',
  86. backgroundColor: 'rgba(0,0,0,.5)',
  87. },
  88. })