fix(association-field): display sub-details by default

This commit is contained in:
chenos 2023-05-26 17:01:34 +08:00
parent ea6f7accc3
commit 1e870cf5ef
3 changed files with 37 additions and 35 deletions

View File

@ -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 ? (
<AssociationFieldContext.Provider
value={{ options: collectionField, field, allowMultiple, allowDissociate, currentMode }}

View File

@ -1,5 +1,5 @@
import { observer, useField, useFieldSchema, useForm } from '@formily/react';
import React, { useEffect, useState } from 'react';
import React from 'react';
import { SchemaComponentOptions } from '../../';
import { useAssociationCreateActionProps as useCAP } from '../../../block-provider/hooks';
import { AssociationFieldProvider } from './AssociationFieldProvider';
@ -17,37 +17,6 @@ const EditableAssociationField = observer((props: any) => {
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 (
<SchemaComponentOptions scope={{ useCreateActionProps }} components={{ CreateRecordAction }}>
{currentMode === 'Picker' && <InternalPicker {...props} />}

View File

@ -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;