GeographicView.tsx 3.64 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';
陈帅's avatar
陈帅 committed
4
import { Dispatch } from 'redux';
陈帅's avatar
陈帅 committed
5
import styles from './GeographicView.less';
陈帅's avatar
陈帅 committed
6
import { ProvinceData, CityData } from '../data';
陈帅's avatar
陈帅 committed
7

陈帅's avatar
陈帅 committed
8 9
const { Option } = Select;

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

陈帅's avatar
陈帅 committed
19
interface GeographicViewProps {
陈帅's avatar
陈帅 committed
20
  dispatch?: Dispatch<any>;
陈帅's avatar
陈帅 committed
21 22 23 24 25
  province?: ProvinceData[];
  city?: CityData[];
  value?: {
    province: SelectItem;
    city: SelectItem;
陈帅's avatar
陈帅 committed
26
  };
陈帅's avatar
陈帅 committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  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,
    };
陈帅's avatar
陈帅 committed
48
  },
陈帅's avatar
陈帅 committed
49 50
)
class GeographicView extends Component<GeographicViewProps> {
陈帅's avatar
陈帅 committed
51
  componentDidMount = () => {
陈帅's avatar
陈帅 committed
52
    const { dispatch } = this.props;
陈帅's avatar
陈帅 committed
53
    if (dispatch) {
陈帅's avatar
陈帅 committed
54 55 56
      dispatch({
        type: 'BLOCK_NAME_CAMEL_CASE/fetchProvince',
      });
陈帅's avatar
陈帅 committed
57
    }
陈帅's avatar
陈帅 committed
58
  };
陈帅's avatar
陈帅 committed
59

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

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

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

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

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

陈帅's avatar
陈帅 committed
104
  selectProvinceItem = (item: SelectItem) => {
陈帅's avatar
陈帅 committed
105
    const { dispatch, onChange } = this.props;
陈帅's avatar
陈帅 committed
106

陈帅's avatar
陈帅 committed
107
    if (dispatch) {
陈帅's avatar
陈帅 committed
108 109 110 111
      dispatch({
        type: 'BLOCK_NAME_CAMEL_CASE/fetchCity',
        payload: item.key,
      });
陈帅's avatar
陈帅 committed
112 113
    }
    if (onChange) {
陈帅's avatar
陈帅 committed
114 115 116 117
      onChange({
        province: item,
        city: nullSlectItem,
      });
陈帅's avatar
陈帅 committed
118
    }
陈帅's avatar
陈帅 committed
119
  };
陈帅's avatar
陈帅 committed
120

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

陈帅's avatar
陈帅 committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144
  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
145

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

export default GeographicView;