Commit 6b964f12 authored by 前端-许佳敏's avatar 前端-许佳敏

新增异步回填formily下拉框hook

parent 17a037df
import { createFormActions, FormEffectHooks, FormPath } from '@formily/antd'
import { useLinkageUtils } from '@/utils/formEffectUtils'
const { onFormInit$ } = FormEffectHooks
/**
* @description 处理异步请求的下拉选择
* @param name 待处理的表单路径
* @param service 触发的异步函数, 需返回一个{label: any, value: any}形式的数组
*/
export const useAsyncSelect = (name, service: () => Promise<any[]>) => {
const { dispatch, setFieldState } = createFormActions()
const linkage = useLinkageUtils()
onFormInit$().subscribe(() => {
setFieldState(name, state => {
FormPath.setIn(state, 'props.x-props.hasFeedback', true)
})
linkage.loading(name)
service().then(res => {
linkage.loaded(name)
linkage.enum(name, res)
//请求结束可以dispatch一个自定义事件收尾,方便后续针对该事件做联动
dispatch('requestAsyncSelect', {
name,
payload: res
})
})
})
}
\ No newline at end of file
......@@ -21,6 +21,7 @@ import EyePreview from '@/components/EyePreview'
import { findItemAndDelete } from '@/utils'
import { PublicApi } from '@/services/api'
import { useRowSelectionTable } from '@/hooks/useRowSelectionTable'
import { useAsyncSelect } from '@/formSchema/effects/useAsyncSelect'
const {Item}:any = Form
......@@ -43,6 +44,14 @@ const fetchProductList = async (params) => {
return res.data
}
const fetchWarehouseAll = async () => {
const { data } = await PublicApi.getWarehouseWarehouseAll()
return data.map(v => ({
value: v.id,
label: v.name
}))
}
const addSchemaAction = createFormActions()
const AddRepository:React.FC<{}> = (props) => {
......@@ -222,6 +231,9 @@ const AddRepository:React.FC<{}> = (props) => {
tableAddButton,
connectProduct
}}
effects={() => {
useAsyncSelect('warehouseId', fetchWarehouseAll)
}}
onSubmit={handleSubmit}
actions={addSchemaAction}
schema={repositDetailSchema}
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment