GeographicView.tsx 3.59 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
2 3
import { Select, Spin } from 'antd';
import { connect } from 'dva';
4
import styles from './GeographicView.less';
陈帅's avatar
陈帅 committed
5 6
import { Dispatch } from 'redux';
import { ProvinceData, CityData } from '../data';
陈帅's avatar
陈帅 committed
7 8
const { Option } = Select;

陈帅's avatar
陈帅 committed
9 10 11 12 13
interface SelectItem {
  label: string;
  key: string;
}
const nullSlectItem: SelectItem = {
陈帅's avatar
陈帅 committed
14 15 16 17
  label: '',
  key: '',
};

陈帅's avatar
陈帅 committed
18 19 20 21 22 23 24
interface GeographicViewProps {
  dispatch?: Dispatch;
  province?: ProvinceData[];
  city?: CityData[];
  value?: {
    province: SelectItem;
    city: SelectItem;
陈帅's avatar
陈帅 committed
25
  };
陈帅's avatar
陈帅 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  loading?: boolean;
  onChange?: (value: { province: SelectItem; city: SelectItem }) => void;
}

@connect(
  ({
    BLOCK_NAME_CAMEL_CASE,
    loading,
  }: {
    BLOCK_NAME_CAMEL_CASE: {
      province: ProvinceData[];
      city: CityData[];
    };
    loading: any;
  }) => {
    const { province, city } = BLOCK_NAME_CAMEL_CASE;
    return {
      province,
      city,
      loading: loading.models.BLOCK_NAME_CAMEL_CASE,
    };
  }
)
class GeographicView extends Component<GeographicViewProps> {
陈帅's avatar
陈帅 committed
50
  componentDidMount = () => {
陈帅's avatar
陈帅 committed
51
    const { dispatch } = this.props;
陈帅's avatar
陈帅 committed
52 53 54 55
    dispatch &&
      dispatch({
        type: 'BLOCK_NAME_CAMEL_CASE/fetchProvince',
      });
陈帅's avatar
陈帅 committed
56
  };
陈帅's avatar
陈帅 committed
57

陈帅's avatar
陈帅 committed
58
  componentDidUpdate(props: GeographicViewProps) {
陈帅's avatar
陈帅 committed
59 60 61
    const { dispatch, value } = this.props;

    if (!props.value && !!value && !!value.province) {
陈帅's avatar
陈帅 committed
62 63 64 65 66
      dispatch &&
        dispatch({
          type: 'BLOCK_NAME_CAMEL_CASE/fetchCity',
          payload: value.province.key,
        });
jim's avatar
jim committed
67 68
    }
  }
陈帅's avatar
陈帅 committed
69

陈帅's avatar
陈帅 committed
70
  getProvinceOption() {
陈帅's avatar
陈帅 committed
71
    const { province } = this.props;
陈帅's avatar
陈帅 committed
72 73 74 75
    if (province) {
      return this.getOption(province);
    }
    return [];
陈帅's avatar
陈帅 committed
76
  }
陈帅's avatar
陈帅 committed
77

陈帅's avatar
陈帅 committed
78
  getCityOption = () => {
陈帅's avatar
陈帅 committed
79
    const { city } = this.props;
陈帅's avatar
陈帅 committed
80 81 82 83
    if (city) {
      return this.getOption(city);
    }
    return [];
陈帅's avatar
陈帅 committed
84
  };
陈帅's avatar
陈帅 committed
85

陈帅's avatar
陈帅 committed
86
  getOption = (list: CityData[] | ProvinceData[]) => {
陈帅's avatar
陈帅 committed
87 88 89 90 91 92 93
    if (!list || list.length < 1) {
      return (
        <Option key={0} value={0}>
          没有找到选项
        </Option>
      );
    }
陈帅's avatar
陈帅 committed
94
    return (list as CityData[]).map(item => (
95 96 97 98
      <Option key={item.id} value={item.id}>
        {item.name}
      </Option>
    ));
陈帅's avatar
陈帅 committed
99
  };
陈帅's avatar
陈帅 committed
100

陈帅's avatar
陈帅 committed
101
  selectProvinceItem = (item: SelectItem) => {
陈帅's avatar
陈帅 committed
102
    const { dispatch, onChange } = this.props;
陈帅's avatar
陈帅 committed
103 104 105 106 107 108 109 110 111 112 113 114

    dispatch &&
      dispatch({
        type: 'BLOCK_NAME_CAMEL_CASE/fetchCity',
        payload: item.key,
      });

    onChange &&
      onChange({
        province: item,
        city: nullSlectItem,
      });
陈帅's avatar
陈帅 committed
115
  };
陈帅's avatar
陈帅 committed
116

陈帅's avatar
陈帅 committed
117
  selectCityItem = (item: SelectItem) => {
陈帅's avatar
陈帅 committed
118
    const { value, onChange } = this.props;
陈帅's avatar
陈帅 committed
119 120 121 122 123 124
    if (value && onChange) {
      onChange({
        province: value.province,
        city: item,
      });
    }
陈帅's avatar
陈帅 committed
125
  };
陈帅's avatar
陈帅 committed
126

陈帅's avatar
陈帅 committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  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
141

陈帅's avatar
陈帅 committed
142 143
  render() {
    const { province, city } = this.conversionObject();
144
    const { loading } = this.props;
陈帅's avatar
陈帅 committed
145
    return (
146
      <Spin spinning={loading} wrapperClassName={styles.row}>
陈帅's avatar
陈帅 committed
147
        <Select
148
          className={styles.item}
陈帅's avatar
陈帅 committed
149 150 151 152 153 154 155 156
          value={province}
          labelInValue
          showSearch
          onSelect={this.selectProvinceItem}
        >
          {this.getProvinceOption()}
        </Select>
        <Select
157
          className={styles.item}
陈帅's avatar
陈帅 committed
158 159 160 161 162 163 164 165 166 167 168
          value={city}
          labelInValue
          showSearch
          onSelect={this.selectCityItem}
        >
          {this.getCityOption()}
        </Select>
      </Spin>
    );
  }
}
lijiehua's avatar
lijiehua committed
169 170

export default GeographicView;