GeographicView.js 2.65 KB
Newer Older
1 2 3
import React, { PureComponent } from 'react';
import { Select, Spin } from 'antd';
import { connect } from 'dva';
4
import styles from './GeographicView.less';
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

const { Option } = Select;

const nullSlectItem = {
  label: '',
  key: '',
};

@connect(({ geographic }) => {
  const { province, isLoading, city } = geographic;
  return {
    province,
    city,
    isLoading,
  };
})
21
export default class GeographicView extends PureComponent {
22
  componentDidMount = () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
23 24
    const { dispatch } = this.props;
    dispatch({
25 26 27
      type: 'geographic/fetchProvince',
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
28

jim's avatar
jim committed
29
  componentDidUpdate(props) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30 31 32 33
    const { dispatch, value } = this.props;

    if (!props.value && !!value && !!value.province) {
      dispatch({
jim's avatar
jim committed
34
        type: 'geographic/fetchCity',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35
        payload: value.province.key,
jim's avatar
jim committed
36 37 38
      });
    }
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39

40
  getProvinceOption() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
41 42
    const { province } = this.props;
    return this.getOption(province);
43
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44

45
  getCityOption = () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46 47
    const { city } = this.props;
    return this.getOption(city);
48
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
49

jim's avatar
jim committed
50
  getOption = list => {
51 52 53 54 55 56 57
    if (!list || list.length < 1) {
      return (
        <Option key={0} value={0}>
          ζ²‘ζœ‰ζ‰Ύεˆ°ι€‰ι‘Ή
        </Option>
      );
    }
jim's avatar
jim committed
58
    return list.map(item => {
59 60 61 62 63 64 65
      return (
        <Option key={item.id} value={item.id}>
          {item.name}
        </Option>
      );
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
66

jim's avatar
jim committed
67
  selectProvinceItem = item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
68 69
    const { dispatch, onChange } = this.props;
    dispatch({
70 71 72
      type: 'geographic/fetchCity',
      payload: item.key,
    });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73
    onChange({
74 75 76 77
      province: item,
      city: nullSlectItem,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
78

jim's avatar
jim committed
79
  selectCityItem = item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80 81 82
    const { value, onChange } = this.props;
    onChange({
      province: value.province,
83 84 85
      city: item,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100
  conversionObject() {
    const { value } = this.props;
    if (!value) {
      return {
        province: nullSlectItem,
        city: nullSlectItem,
      };
    }
    const { province, city } = value;
    return {
      province: province || nullSlectItem,
      city: city || nullSlectItem,
    };
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
101

102 103
  render() {
    const { province, city } = this.conversionObject();
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
104
    const { isLoading } = this.props;
105
    return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
106
      <Spin spinning={isLoading} wrapperClassName={styles.row}>
107
        <Select
108
          className={styles.item}
109 110 111 112 113 114 115 116
          value={province}
          labelInValue
          showSearch
          onSelect={this.selectProvinceItem}
        >
          {this.getProvinceOption()}
        </Select>
        <Select
117
          className={styles.item}
118 119 120 121 122 123 124 125 126 127 128
          value={city}
          labelInValue
          showSearch
          onSelect={this.selectCityItem}
        >
          {this.getCityOption()}
        </Select>
      </Spin>
    );
  }
}