React Hooks 二次封装省市区街道4级联动组件

import React, { useState } from "react";
import FormItemPro from 'components/FormItemPro';
import {
  FormControl, Row, Col, Label, Button, Select, Loading, Modal
} from "tinper-bee";
import request from "utils/request";
const Option = Select.Option;
const XxxDistpicker = ({ handleAddressChoose }) => {
  const [isShowDistpicker, setIsShowDistpicker] = useState(false);
  const [country, setCountry] = useState({ code: '0001', name: '中国' });       //保存国家状态
  const [province, setProvince] = useState({ code: "00010005", name: '广东' }); //保存省份状态
  const [city, setCity] = useState({ code: '00010102', name: '深圳' });         //保存城市状态
  const [district, setDistrict] = useState({ code: '0001010202', name: '南山' }); //保存行政区状态
  const [data, setData] = useState([])  //当前选择的下拉框所需要的数据

  // 发送请求获取下拉框所需的数据
  const indexedDist = ['country', 'province', 'city', 'district'];
  const handleFocus = async dist => {
    const index = indexedDist.findIndex(item => item === dist);
    const param = { parentCode: '0', level: '0' }
    if (index !== 0) {
      param.parentCode = eval(indexedDist[index - 1])['code'];
    }
    const response = await request(`${GROBAL_HTTP_BASE}/region/select`, {
      method: 'get',
      data: {},
      param
    });
    if (response && response.data && response.data.code && !Number(response.data.code.code)) {
      setData(response.data.bo);  //为下拉框输送数据
      // 清空当前区域之下的所有数据
      const index = indexedDist.findIndex(item=> item === dist);
      const distRest = indexedDist.slice(index+1);
      if(distRest.length) {
        distRest.map(item=>`set${item.charAt(0).toUpperCase() + item.slice(1)}`).forEach(item=> eval(item)({ code:'', name:'' }))
      }
    }
  }

  // 【确定】点击事件
  const handleOkAction = () => {
    setIsShowDistpicker(false);
    handleAddressChoose({country, province, city, district});
  }

  return (
    <div className="xxx-dist-picker">
        <FormControl
          className= "xxx-dist-picker-address"
          value={ country.name +province.name+ city.name+ district.name }
          onSearch={v => setIsShowDistpicker(true) }
          onChange={v => setIsShowDistpicker(true) }
          type="search"
        />
      <Modal show={isShowDistpicker} size="sm" onHide={() => setIsShowDistpicker(false)} backdrop={"static"}>
        <Modal.Header closeButton>
          <Modal.Title>地区选择器</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <Row className="form-panel">
            <Col lg={12} md={12} xs={12} sm={12}>
              <FormItemPro>
                <Label>国家/地区</Label>
                <Select
                  disabled={true}
                  placeholder='请选择国家/地区'
                  onFocus={() => handleFocus('country')}
                  onChange={value => value && setCountry(JSON.parse(value))}
                  allowClear={true}
                  optionFilterProp="children"
                  value={country.name}
                >
                  {
                    data.map((item) =>
                      <Option key={item.code} value={item.code} item={item} >
                        {item.desc3}
                      </Option>)
                  }
                </Select>
              </FormItemPro>
            </Col>
            <Col lg={12} md={12} xs={12} sm={12}>
              <FormItemPro>
                <Label>省份/</Label>
                <Select
                  showSearch
                  optionFilterProp="children"
                  placeholder='请先选择省份/州'
                  onFocus={() => handleFocus('province')}
                  onChange={value => value && setProvince(JSON.parse(value))}
                  value={province.name}
                >
                  {
                    data.map((item) =>
                      <Option key={item.code} value={JSON.stringify({ code: item.code, name: item.desc3 })} >
                        {item.desc3}
                      </Option>)
                  }
                </Select>
              </FormItemPro>
            </Col>
            <Col lg={12} md={12} xs={12} sm={12}>
              <FormItemPro>
                <Label>城市</Label>
                <Select
                  showSearch
                  optionFilterProp="children"
                  placeholder='请先选择城市'
                  onFocus={() => handleFocus('city')}
                  onChange={value => value && setCity(JSON.parse(value))}
                  value={city.name}
                >
                  {
                    data.map((item) =>
                      <Option key={item.code} value={JSON.stringify({ code: item.code, name: item.desc3 })} >
                        {item.desc3}
                      </Option>)
                  }
                </Select>
              </FormItemPro>
            </Col>
            <Col lg={12} md={12} xs={12} sm={12}>
              <FormItemPro>
                <Label></Label>
                <Select
                  showSearch
                  optionFilterProp="children"
                  placeholder='请先选择区'
                  onFocus={() => handleFocus('district')}
                  onChange={value => value && setDistrict(JSON.parse(value))}
                  value={district.name}
                >
                  {
                    data.map((item) =>
                      <Option key={item.code} value={JSON.stringify({ code: item.code, name: item.desc3 })} >
                        {item.desc3}
                      </Option>)
                  }
                </Select>
              </FormItemPro>
            </Col>
            <Col lg={12} md={12} xs={12} sm={12}>
              <FormItemPro>
                <Label>详细地址</Label>
                <FormControl value={ country.name +' ' +province.name+' ' + city.name+' ' + district.name } />
              </FormItemPro>
            </Col>
          </Row>
        </Modal.Body>
        <Modal.Footer className="text-center">
          <Button
            colors="primary"
            style={{ marginRight: 10 }}
            onClick={ handleOkAction }
          >
            确认
          </Button>
          <Button
            shape="border"
            colors="primary"
            onClick={() => setIsShowDistpicker(false)}
          >
            取消
          </Button>
        </Modal.Footer>
      </Modal>
      {/*<Loading container={this} show={this.state.showLoading} />*/}
    </div>
  );
};

export default XxxDistpicker;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用React Hooks对Axios进行二次封装的示例代码: ```javascript import { useState, useEffect } from 'react'; import axios from 'axios'; const useAxios = (url, method, body = null, headers = null) => { const [response, setResponse] = useState(null); const [error, setError] = useState(null); useEffect(() => { axios({ method: method, url: url, data: body, headers: headers }) .then(res => { setResponse(res.data); }) .catch(err => { setError(err); }); }, [url, method, body, headers]); return { response, error }; }; export default useAxios; ``` 在这个示例中,我们使用了useState和useEffect两个React Hooks。useState用于在组件中存储状态,而useEffect用于在组件渲染时执行副作用操作。我们将Axios请求封装在useAxios自定义钩子中,并将url,method,body和headers作为参数传递。在useEffect中,我们使用Axios发送请求,并根据响应设置状态。最后,我们返回一个包含响应和错误的对象。 使用示例: ```javascript import useAxios from './useAxios'; const MyComponent = () => { const { response, error } = useAxios('https://jsonplaceholder.typicode.com/posts', 'get'); if (error) return <div>Error: {error.message}</div>; if (!response) return <div>Loading...</div>; return ( <ul> {response.map(post => ( <li key={post.id}>{post.title}</li> ))} </ul> ); }; ``` 在这个示例中,我们使用useAxios钩子从https://jsonplaceholder.typicode.com/posts获取帖子列表。我们根据响应渲染帖子列表,如果出现错误,则显示错误消息,如果响应尚未返回,则显示加载消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值