SalesBatchSelect.jsx 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { Select } from 'antd';
  3. import apis from '../../services/apis';
  4. import request from '../../utils/request'
  5. const { Option } = Select;
  6. function usePrevious(props) {
  7. const ref = useRef();
  8. useEffect(() => {
  9. ref.current = props;
  10. });
  11. return ref.current;
  12. }
  13. /**
  14. *
  15. *
  16. * @param {*} props
  17. * @returns
  18. */
  19. const SalesBatchSelect = props => {
  20. const [data, setData] = useState([])
  21. const [value, setValue] = useState([])
  22. useEffect(() => {
  23. getList();
  24. }, [props.value])
  25. const getList = e => {
  26. request({ ...apis.house.apartmentList, params: { pageNum: 1, pageSize: 999, buildingId: props.buildingId} }).then(data => {
  27. setData(data.records)
  28. checkValue(data.records)
  29. // 默认选中第一个
  30. })
  31. }
  32. const checkValue = (data) => {
  33. if (props.value) {
  34. const tempData = data.filter(f => f.apartmentId == props.value)
  35. const va = (tempData.length > 0) ? props.value : '已删除,请重新选择'
  36. props.onChange(va)
  37. }
  38. }
  39. return (
  40. <Select
  41. showSearch
  42. value={props.value}
  43. style={{ width: '300px' }}
  44. placeholder="请选择批次"
  45. onChange={props.onChange}
  46. filterOption={(input, option) =>
  47. option.props.children && option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
  48. }>
  49. {data.map(apartment => (
  50. <Option key={apartment.apartmentId} value={apartment.apartmentId}>{apartment.apartmentName}</Option>
  51. ))}
  52. </Select>
  53. )
  54. }
  55. export default SalesBatchSelect