GeographicView.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { PureComponent } from 'react';
  2. import { Select, Spin } from 'antd';
  3. import { connect } from 'dva';
  4. import styles from './GeographicView.less';
  5. const { Option } = Select;
  6. const nullSlectItem = {
  7. label: '',
  8. key: '',
  9. };
  10. @connect(({ geographic }) => {
  11. const { province, isLoading, city } = geographic;
  12. return {
  13. province,
  14. city,
  15. isLoading,
  16. };
  17. })
  18. export default class GeographicView extends PureComponent {
  19. componentDidMount = () => {
  20. this.props.dispatch({
  21. type: 'geographic/fetchProvince',
  22. });
  23. };
  24. componentDidUpdate(props) {
  25. if (!props.value && !!this.props.value && !!this.props.value.province) {
  26. this.props.dispatch({
  27. type: 'geographic/fetchCity',
  28. payload: this.props.value.province.key,
  29. });
  30. }
  31. }
  32. getProvinceOption() {
  33. return this.getOption(this.props.province);
  34. }
  35. getCityOption = () => {
  36. return this.getOption(this.props.city);
  37. };
  38. getOption = (list) => {
  39. if (!list || list.length < 1) {
  40. return (
  41. <Option key={0} value={0}>
  42. 没有找到选项
  43. </Option>
  44. );
  45. }
  46. return list.map((item) => {
  47. return (
  48. <Option key={item.id} value={item.id}>
  49. {item.name}
  50. </Option>
  51. );
  52. });
  53. };
  54. selectProvinceItem = (item) => {
  55. this.props.dispatch({
  56. type: 'geographic/fetchCity',
  57. payload: item.key,
  58. });
  59. this.props.onChange({
  60. province: item,
  61. city: nullSlectItem,
  62. });
  63. };
  64. selectCityItem = (item) => {
  65. this.props.onChange({
  66. province: this.props.value.province,
  67. city: item,
  68. });
  69. };
  70. conversionObject() {
  71. const { value } = this.props;
  72. if (!value) {
  73. return {
  74. province: nullSlectItem,
  75. city: nullSlectItem,
  76. };
  77. }
  78. const { province, city } = value;
  79. return {
  80. province: province || nullSlectItem,
  81. city: city || nullSlectItem,
  82. };
  83. }
  84. render() {
  85. const { province, city } = this.conversionObject();
  86. return (
  87. <Spin spinning={this.props.isLoading} wrapperClassName={styles.row}>
  88. <Select
  89. className={styles.item}
  90. value={province}
  91. labelInValue
  92. showSearch
  93. onSelect={this.selectProvinceItem}
  94. >
  95. {this.getProvinceOption()}
  96. </Select>
  97. <Select
  98. className={styles.item}
  99. value={city}
  100. labelInValue
  101. showSearch
  102. onSelect={this.selectCityItem}
  103. >
  104. {this.getCityOption()}
  105. </Select>
  106. </Spin>
  107. );
  108. }
  109. }