GeographicView.js 2.49 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 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,
      });
    }
  }
35 36 37 38 39 40
  getProvinceOption() {
    return this.getOption(this.props.province);
  }
  getCityOption = () => {
    return this.getOption(this.props.city);
  };
jim's avatar
jim committed
41
  getOption = list => {
42 43 44 45 46 47 48
    if (!list || list.length < 1) {
      return (
        <Option key={0} value={0}>
          ζ²‘ζœ‰ζ‰Ύεˆ°ι€‰ι‘Ή
        </Option>
      );
    }
jim's avatar
jim committed
49
    return list.map(item => {
50 51 52 53 54 55 56
      return (
        <Option key={item.id} value={item.id}>
          {item.name}
        </Option>
      );
    });
  };
jim's avatar
jim committed
57
  selectProvinceItem = item => {
58 59 60 61 62 63 64 65 66
    this.props.dispatch({
      type: 'geographic/fetchCity',
      payload: item.key,
    });
    this.props.onChange({
      province: item,
      city: nullSlectItem,
    });
  };
jim's avatar
jim committed
67
  selectCityItem = item => {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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}>
91
        <Select
92
          className={styles.item}
93 94 95 96 97 98 99 100
          value={province}
          labelInValue
          showSearch
          onSelect={this.selectProvinceItem}
        >
          {this.getProvinceOption()}
        </Select>
        <Select
101
          className={styles.item}
102 103 104 105 106 107 108 109 110 111 112
          value={city}
          labelInValue
          showSearch
          onSelect={this.selectCityItem}
        >
          {this.getCityOption()}
        </Select>
      </Spin>
    );
  }
}