|
|
@@ -0,0 +1,81 @@
|
|
|
+import { Select } from 'antd'
|
|
|
+import React, { useState } from 'react'
|
|
|
+import { useDebounceFn } from 'ahooks'
|
|
|
+import consts from '@/consts'
|
|
|
+import { queryRoadpricingListV2 } from '@/services/product'
|
|
|
+import { validateEnum } from '@/pages/Customer/Company/components/AddCompany/CompanyFormItem'
|
|
|
+
|
|
|
+const EnterpriseSearchSelect = ({ value, onChange, disabled, validateFn }) => {
|
|
|
+ const { Option } = Select
|
|
|
+ const [state, setState] = useState({
|
|
|
+ inputVal: value,
|
|
|
+ visible: false,
|
|
|
+ roadlist: null
|
|
|
+ })
|
|
|
+ const triggerChange = changedValue => {
|
|
|
+ onChange?.(changedValue)
|
|
|
+ }
|
|
|
+ const { visible, roadlist, inputVal } = state
|
|
|
+ const queryRoadList = async search => {
|
|
|
+ if (!search) return
|
|
|
+ const { code = -1, data } = await queryRoadpricingListV2({
|
|
|
+ current: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ search,
|
|
|
+ projectType: 1
|
|
|
+ })
|
|
|
+ if (code === consts.RET_CODE.SUCCESS) {
|
|
|
+ const { list = [] } = data
|
|
|
+ setState({ ...state, roadlist: list, visible: !!list?.length })
|
|
|
+ if (list?.length) {
|
|
|
+ validateFn(validateEnum.Success)
|
|
|
+ } else {
|
|
|
+ validateFn(validateEnum.Error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const { run: handleQueryList, cancel: cancelQuerying } = useDebounceFn(queryRoadList, {
|
|
|
+ wait: 600
|
|
|
+ })
|
|
|
+
|
|
|
+ const handleSearch = value => {
|
|
|
+ if (value) {
|
|
|
+ fetch(value, roadlist => setState({ ...state, roadlist }))
|
|
|
+ } else {
|
|
|
+ setState({ ...state, roadlist: [] })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const handleOnInputChange = async e => {
|
|
|
+ const val = e.target.value || ''
|
|
|
+ if (!val) {
|
|
|
+ cancelQuerying()
|
|
|
+ setState({ ...state, inputVal: val, roadlist: null, visible: false })
|
|
|
+ validateFn(validateEnum.Error)
|
|
|
+ } else {
|
|
|
+ setState({ ...state, inputVal: val })
|
|
|
+ validateFn(validateEnum.Validating)
|
|
|
+ handleQueryList(val)
|
|
|
+ }
|
|
|
+ triggerChange(val)
|
|
|
+ }
|
|
|
+ const options = roadlist?.map(item => <Option key={item.ID}>{{ value: item.realName }}</Option>)
|
|
|
+ console.log(options)
|
|
|
+ return (
|
|
|
+ <Select
|
|
|
+ showSearch
|
|
|
+ defaultActiveFirstOption={false}
|
|
|
+ showArrow={false}
|
|
|
+ filterOption={false}
|
|
|
+ placeholder="请输入企业名称"
|
|
|
+ value={inputVal}
|
|
|
+ disabled={disabled}
|
|
|
+ onSearch={handleSearch}
|
|
|
+ onChange={handleOnInputChange}
|
|
|
+ notFoundContent={null}>
|
|
|
+ {options}
|
|
|
+ </Select>
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+export default EnterpriseSearchSelect
|