BuildSelect.jsx 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 BuildingSelect = props => {
  20. const [data, setData] = useState([])
  21. const [value, setValue] = useState([])
  22. const type = props.type
  23. const salesBatchId = props.salesBatchId
  24. useEffect(() => {
  25. getBuildList();
  26. }, [props.salesBatchId])
  27. const getBuildList = e => {
  28. if (salesBatchId && salesBatchId != 'undefine'){
  29. request({ ...apis.house.buildingIdBySalesBatchId, urlData:{id: salesBatchId}, params: { pageNum: 1, pageSize: 999 } }).then(data => {
  30. setData(data)
  31. checkValue(data)
  32. // 默认选中第一个
  33. })
  34. }
  35. else{
  36. request({ ...apis.building.buildingSelect, params: { pageNum: 1, pageSize: 999 } }).then(data => {
  37. setData(data)
  38. checkValue(data)
  39. // 默认选中第一个
  40. })
  41. }
  42. }
  43. const checkValue = (data) => {
  44. if (props.value) {
  45. const tempData = data.filter(f => f.buildingId == props.value)
  46. const va = (tempData.length > 0) ? props.value : '项目已下线,请重新选择项目'
  47. props.onChange(va)
  48. }
  49. }
  50. return (
  51. <Select
  52. disabled={type == 'false'?false:true}
  53. showSearch
  54. value={props.value}
  55. style={{ width: '300px' }}
  56. placeholder="请选择项目"
  57. onChange={props.onChange}
  58. filterOption={(input, option) =>
  59. option.props.children && option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
  60. }>
  61. {data.map(building => (
  62. <Option key={building.buildingId} value={building.buildingId}>{building.buildingName}</Option>
  63. ))}
  64. </Select>
  65. )
  66. }
  67. export default BuildingSelect