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

const { Option } = Select;

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

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

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

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

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

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

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

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

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

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

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