From d9e7ba6e8d56a246e8ad02b10a9f04246aacf5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B0=94-Rain?= <958414905@qq.com> Date: Fri, 10 Nov 2023 15:27:53 +0800 Subject: [PATCH] fix: relational data should be loaded correctly on first render (#3016) * fix: subform should lazy load data * fix: relational data should be loaded correctly on first render --- .../client/src/block-provider/BlockProvider.tsx | 14 +++++++++----- .../core/client/src/block-provider/hooks/index.ts | 8 ++++---- .../client/src/variables/VariablesProvider.tsx | 6 ++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index f1ed19a2e..95a8b2193 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -301,14 +301,18 @@ export const BlockProvider = (props: { }) => { const { collection, association, name } = props; const resource = useResource(props); - const params = useMemo(() => ({ ...props.params }), [props.params]); const { appends, updateAssociationValues } = useAssociationNames(); + const params = useMemo(() => { + if (!props.params) { + return props.params; + } + if (!props.params['appends']) { + return { ...props.params, appends }; + } + return { ...props.params }; + }, [appends, props.params]); const blockValue = useMemo(() => ({ name }), [name]); - if (!Object.keys(params).includes('appends')) { - params['appends'] = appends; - } - return ( diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index 3ef2ec37c..eda790bf0 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -1278,10 +1278,10 @@ export const useAssociationNames = () => { const getAssociationAppends = (schema, str) => { schema.reduceProperties((pre, s) => { const prefix = pre || str; - const collectionfield = s['x-collection-field'] && getCollectionJoinField(s['x-collection-field']); + const collectionField = s['x-collection-field'] && getCollectionJoinField(s['x-collection-field']); const isAssociationSubfield = s.name.includes('.'); const isAssociationField = - collectionfield && ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(collectionfield.type); + collectionField && ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(collectionField.type); // 根据联动规则中条件的字段获取一些 appends if (s['x-linkage-rules']) { @@ -1301,8 +1301,8 @@ export const useAssociationNames = () => { }); } - const isTreeCollection = isAssociationField && getCollection(collectionfield.target)?.template === 'tree'; - if (collectionfield && (isAssociationField || isAssociationSubfield) && s['x-component'] !== 'TableField') { + const isTreeCollection = isAssociationField && getCollection(collectionField.target)?.template === 'tree'; + if (collectionField && (isAssociationField || isAssociationSubfield) && s['x-component'] !== 'TableField') { const fieldPath = !isAssociationField && isAssociationSubfield ? getAssociationPath(s.name) : s.name; const path = prefix === '' || !prefix ? fieldPath : prefix + '.' + fieldPath; if (isTreeCollection) { diff --git a/packages/core/client/src/variables/VariablesProvider.tsx b/packages/core/client/src/variables/VariablesProvider.tsx index 3b7d51d25..0dda9362a 100644 --- a/packages/core/client/src/variables/VariablesProvider.tsx +++ b/packages/core/client/src/variables/VariablesProvider.tsx @@ -287,5 +287,11 @@ export default VariablesProvider; * @returns */ function shouldToRequest(value) { + // fix https://nocobase.height.app/T-2502 + // 兼容对多子表单子表格字段的情况 + if (JSON.stringify(value) === '[{}]') { + return true; + } + return value === undefined; }