EnterpriseSearchSelect.jsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Select } from 'antd'
  2. import React, { useState } from 'react'
  3. import { useDebounceFn } from 'ahooks'
  4. import consts from '@/consts'
  5. import { queryRoadpricingListV2 } from '@/services/product'
  6. import { validateEnum } from '@/pages/Customer/Company/components/AddCompany/CompanyFormItem'
  7. const EnterpriseSearchSelect = ({ value, onChange, disabled }) => {
  8. const { Option } = Select
  9. const [state, setState] = useState({
  10. inputVal: value,
  11. roadlist: null
  12. })
  13. const triggerChange = changedValue => {
  14. onChange?.(changedValue)
  15. }
  16. const { roadlist, inputVal } = state
  17. const queryRoadList = async search => {
  18. if (!search) return
  19. const { code = -1, data } = await queryRoadpricingListV2({
  20. current: 1,
  21. pageSize: 10,
  22. search,
  23. projectType: 1
  24. })
  25. if (code === consts.RET_CODE.SUCCESS) {
  26. const { list = [] } = data
  27. setState({ ...state, roadlist: list })
  28. }
  29. }
  30. const { run: handleQueryList, cancel: cancelQuerying } = useDebounceFn(queryRoadList, {
  31. wait: 250
  32. })
  33. const handleOnSearch = async value => {
  34. if (value) {
  35. await handleQueryList(value)
  36. setState({ ...state, inputVal: value })
  37. } else {
  38. setState({ ...state, inputVal: value, roadlist: null })
  39. }
  40. }
  41. const handleOnChange = async val => {
  42. setState({ ...state, inputVal: val })
  43. triggerChange(val)
  44. }
  45. const options = roadlist?.map(item => ({ label: item.realName, value: item.ID }))
  46. return (
  47. <Select
  48. showSearch
  49. defaultActiveFirstOption={false}
  50. showArrow={false}
  51. filterOption={false}
  52. placeholder="请输入企业名称"
  53. value={inputVal}
  54. options={options}
  55. disabled={disabled}
  56. allowClear
  57. onSearch={handleOnSearch}
  58. onChange={handleOnChange}
  59. notFoundContent={null}
  60. />
  61. )
  62. }
  63. export default EnterpriseSearchSelect