GeographicView.js 2.62 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>
      );
    }
58 59 60 61 62
    return list.map(item => (
      <Option key={item.id} value={item.id}>
        {item.name}
      </Option>
    ));
63
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64

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

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

85 86 87 88 89 90 91 92 93 94 95 96 97 98
  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
99

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