GeographicView.js 2.62 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2 3
import React, { PureComponent } from 'react';
import { Select, Spin } from 'antd';
import { connect } from 'dva';
4
import styles from './GeographicView.less';
陈帅's avatar
陈帅 committed
5 6 7 8 9 10 11 12

const { Option } = Select;

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

afc163's avatar
afc163 committed
13
export default
陈帅's avatar
陈帅 committed
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 {
陈帅's avatar
陈帅 committed
23
  componentDidMount = () => {
陈帅's avatar
陈帅 committed
24 25
    const { dispatch } = this.props;
    dispatch({
陈帅's avatar
陈帅 committed
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

陈帅's avatar
陈帅 committed
41
  getProvinceOption() {
陈帅's avatar
陈帅 committed
42 43
    const { province } = this.props;
    return this.getOption(province);
陈帅's avatar
陈帅 committed
44
  }
陈帅's avatar
陈帅 committed
45

陈帅's avatar
陈帅 committed
46
  getCityOption = () => {
陈帅's avatar
陈帅 committed
47 48
    const { city } = this.props;
    return this.getOption(city);
陈帅's avatar
陈帅 committed
49
  };
陈帅's avatar
陈帅 committed
50

jim's avatar
jim committed
51
  getOption = list => {
陈帅's avatar
陈帅 committed
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>
    ));
陈帅's avatar
陈帅 committed
64
  };
陈帅's avatar
陈帅 committed
65

jim's avatar
jim committed
66
  selectProvinceItem = item => {
陈帅's avatar
陈帅 committed
67 68
    const { dispatch, onChange } = this.props;
    dispatch({
陈帅's avatar
陈帅 committed
69 70 71
      type: 'geographic/fetchCity',
      payload: item.key,
    });
陈帅's avatar
陈帅 committed
72
    onChange({
陈帅's avatar
陈帅 committed
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,
陈帅's avatar
陈帅 committed
82 83 84
      city: item,
    });
  };
陈帅's avatar
陈帅 committed
85

陈帅's avatar
陈帅 committed
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

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