GeographicView.js 2.5 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 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 {
陈帅's avatar
陈帅 committed
22 23 24 25 26
  componentDidMount = () => {
    this.props.dispatch({
      type: 'geographic/fetchProvince',
    });
  };
jim's avatar
jim committed
27 28 29 30 31 32 33 34
  componentDidUpdate(props) {
    if (!props.value && !!this.props.value && !!this.props.value.province) {
      this.props.dispatch({
        type: 'geographic/fetchCity',
        payload: this.props.value.province.key,
      });
    }
  }
陈帅's avatar
陈帅 committed
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
  getProvinceOption() {
    return this.getOption(this.props.province);
  }
  getCityOption = () => {
    return this.getOption(this.props.city);
  };
  getOption = (list) => {
    if (!list || list.length < 1) {
      return (
        <Option key={0} value={0}>
          没有找到选项
        </Option>
      );
    }
    return list.map((item) => {
      return (
        <Option key={item.id} value={item.id}>
          {item.name}
        </Option>
      );
    });
  };
  selectProvinceItem = (item) => {
    this.props.dispatch({
      type: 'geographic/fetchCity',
      payload: item.key,
    });
    this.props.onChange({
      province: item,
      city: nullSlectItem,
    });
  };
  selectCityItem = (item) => {
    this.props.onChange({
      province: this.props.value.province,
      city: item,
    });
  };
  conversionObject() {
    const { value } = this.props;
    if (!value) {
      return {
        province: nullSlectItem,
        city: nullSlectItem,
      };
    }
    const { province, city } = value;
    return {
      province: province || nullSlectItem,
      city: city || nullSlectItem,
    };
  }
  render() {
    const { province, city } = this.conversionObject();
    return (
90
      <Spin spinning={this.props.isLoading} wrapperClassName={styles.row}>
陈帅's avatar
陈帅 committed
91
        <Select
92
          className={styles.item}
陈帅's avatar
陈帅 committed
93 94 95 96 97 98 99 100
          value={province}
          labelInValue
          showSearch
          onSelect={this.selectProvinceItem}
        >
          {this.getProvinceOption()}
        </Select>
        <Select
101
          className={styles.item}
陈帅's avatar
陈帅 committed
102 103 104 105 106 107 108 109 110 111 112
          value={city}
          labelInValue
          showSearch
          onSelect={this.selectCityItem}
        >
          {this.getCityOption()}
        </Select>
      </Spin>
    );
  }
}