index.js 3.95 KB
Newer Older
李彥志's avatar
李彥志 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
import React from 'react';
import PropTypes from 'prop-types';
import {
  Modal,
  View,
  Text,
  ScrollView,
  TouchableOpacity
} from 'react-native';
import * as Animatable from 'react-native-animatable';
import styles from './styles';

class Dialog extends React.Component {
  static propTypes = {
    payload: PropTypes.object,
    show: PropTypes.bool,
    customDesign: PropTypes.array,
    title: PropTypes.string,
    content: PropTypes.string,
    buttonTxt: PropTypes.string,
    cancelTxt: PropTypes.string,
    confirmFun: PropTypes.func,
    cancelFun: PropTypes.func,
    showCancel: PropTypes.bool,
    contentStyle: PropTypes.object,
    titleStyle: PropTypes.object,
  };

  static defaultProps = {
    payload: {},
    show: false,
    customDesign: [],
    title: '',
    content: '',
    buttonTxt: '登出',
    cancelTxt: '取消',
    confirmFun: () => {},
    cancelFun: () => {},
    showCancel: false,
    contentStyle: {},
    titleStyle: {}
  };

  constructor(props) {
    super(props);
    const {
      show, title, content, buttonTxt, cancelTxt, showCancel, payload, customDesign
    } = this.props;
    this.state = {
      show,
      title,
      content,
      buttonTxt,
      cancelTxt,
      showCancel,
      customDesign
    };
    this.payload = payload;
    this.doConfirm = this.doConfirm.bind(this);
    this.doCancel = this.doCancel.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    const {
      show, title, content, payload
    } = nextProps;
    if (show !== this.props.show) {
      this.payload = payload;
      this.setState({ show, title, content });
    }
  }

  doConfirm() {
    this.setState({ show: false }, () => this.props.confirmFun(this.payload));
  }

  doCancel() {
    this.setState({ show: false }, () => this.props.cancelFun());
  }

  render() {
    const {
      showCancel, show, title, cancelTxt, customDesign
    } = this.state;
    const { contentStyle, titleStyle } = this.props;
    if (!this.props.show && !show) {
      return null;
    }
    const buttonStyle = (showCancel) ? { borderLeftWidth: 1, borderLeftColor: '#BCBBC1' } : {};
    return (
      <Modal
        transparent
        visible={this.state.show}
        onRequestClose={() => {
          this.doCancel();
        }}
      >
        <View style={styles.modalContainer}>
          {customDesign && customDesign.length > 0 ? customDesign : (
            <Animatable.View
              animation="bounceIn"
              duration={300}
              style={styles.modalContent}
              useNativeDriver
            >
              {title !== '' ? (
                <View style={styles.modalTitle}>
                  <Text style={[styles.modalTitle_txt, titleStyle]}>{title}</Text>
                </View>
              ) : []}
              <ScrollView keyboardShouldPersistTaps="always">
                <View style={styles.modalMessage}>
                  <Text style={[styles.modalMessage_txt, contentStyle]}>
                    {this.state.content}
                  </Text>
                </View>
              </ScrollView>
              <View style={styles.modalFooter}>
                {(showCancel)
                  ? (
                    <TouchableOpacity
                      style={styles.modalFooter_btn}
                      onPress={() => this.doCancel()}
                    >
                      <Text style={styles.modalFooter_txt}>
                        {cancelTxt}
                      </Text>
                    </TouchableOpacity>
                  ) : []
                    }
                <TouchableOpacity
                  style={[styles.modalFooter_btn, buttonStyle]}
                  onPress={() => this.doConfirm()}
                >
                  <Text style={[styles.modalFooter_txt, { fontWeight: 'bold' }]}>
                    {this.state.buttonTxt}
                  </Text>
                </TouchableOpacity>
              </View>
            </Animatable.View>)}
        </View>
      </Modal>
    );
  }
}

export default Dialog;