diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx index 7dd6806f5..89bf697c2 100644 --- a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx @@ -1,6 +1,6 @@ import { Field } from '@formily/core'; import { observer, useField, useFieldSchema } from '@formily/react'; -import React, { useMemo } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { useCollectionManager } from '../../../collection-manager'; import { AssociationFieldContext } from './context'; @@ -24,6 +24,38 @@ export const AssociationFieldProvider = observer((props) => { [fieldSchema['x-component-props']?.mode], ); + const [loading, setLoading] = useState(true); + + useEffect(() => { + setLoading(true); + if (!collectionField) { + setLoading(false); + return; + } + if (field.value !== null && field.value !== undefined) { + // Nester 子表单时,如果没数据初始化一个 [null] 的占位 + if (currentMode === 'Nester' && Array.isArray(field.value)) { + if (field.value.length === 0 && ['belongsToMany', 'hasMany'].includes(collectionField.type)) { + field.value = [null]; + } + } + setLoading(false); + return; + } + if (currentMode === 'Nester') { + if (['belongsTo', 'hasOne'].includes(collectionField.type)) { + field.value = {}; + } else if (['belongsToMany', 'hasMany'].includes(collectionField.type)) { + field.value = [null]; + } + } + setLoading(false); + }, [currentMode, collectionField, field.value]); + + if (loading) { + return null; + } + return collectionField ? ( { const form = useForm(); const fieldSchema = useFieldSchema(); const { options: collectionField, currentMode } = useAssociationFieldContext(); - const [loading, setLoading] = useState(true); - - useEffect(() => { - setLoading(true); - if (!collectionField) { - setLoading(false); - return; - } - if (field.value !== null && field.value !== undefined) { - // Nester 子表单时,如果没数据初始化一个 [null] 的占位 - if (currentMode === 'Nester' && Array.isArray(field.value)) { - if (field.value.length === 0 && ['belongsToMany', 'hasMany'].includes(collectionField.type)) { - field.value = [null]; - } - } - setLoading(false); - return; - } - if (currentMode === 'Nester') { - if (['belongsTo', 'hasOne'].includes(collectionField.type)) { - field.value = {}; - } else if (['belongsToMany', 'hasMany'].includes(collectionField.type)) { - field.value = [null]; - } - } - setLoading(false); - }, [currentMode, collectionField, field.value]); - - if (loading) { - return null; - } const useCreateActionProps = () => { const { onClick } = useCAP(); @@ -77,6 +46,7 @@ const EditableAssociationField = observer((props: any) => { }, }; }; + return ( {currentMode === 'Picker' && } diff --git a/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx b/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx index ea0c58d65..08b114dd6 100644 --- a/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx +++ b/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx @@ -175,8 +175,8 @@ FormItem.Designer = function Designer() { const fieldSchemaWithoutRequired = _.omit(fieldSchema, 'required'); - const isSubFormMode = fieldSchema['x-component-props'].mode === 'Nester'; - const isPickerMode = fieldSchema['x-component-props'].mode === 'Picker'; + const isSubFormMode = fieldSchema['x-component-props']?.mode === 'Nester'; + const isPickerMode = fieldSchema['x-component-props']?.mode === 'Picker'; const showFieldMode = isAssociationField && fieldModeOptions && !isTableField; const showModeSelect = showFieldMode && isPickerMode;