GeographicView.js 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103
import React, { PureComponent } from 'react';
import { Select, Spin } from 'antd';
import { connect } from 'dva';

const { Option } = Select;

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

@connect(({ geographic }) => {
  const { province, isLoading, city } = geographic;
  return {
    province,
    city,
    isLoading,
  };
})
export default class ProvinceSelect extends PureComponent {
  componentDidMount = () => {
    this.props.dispatch({
      type: 'geographic/fetchProvince',
    });
  };
  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 (
      <Spin spinning={this.props.isLoading}>
        <Select
          value={province}
          labelInValue
          showSearch
          onSelect={this.selectProvinceItem}
          style={{ width: 220, marginRight: 8 }}
        >
          {this.getProvinceOption()}
        </Select>
        <Select
          value={city}
          labelInValue
          showSearch
          onSelect={this.selectCityItem}
          style={{ width: 220 }}
        >
          {this.getCityOption()}
        </Select>
      </Spin>
    );
  }
}