From 9db9b9e7afb87ac42f02bd205e6094e5252b5a9c 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: Mon, 16 Oct 2023 22:49:48 +0800 Subject: [PATCH] fix(variable): should have currentObject in sub-blocks (#2823) * chore: avoid crashing * fix(variable): should have currentObject in sub-blocks --- .../client/src/flag-provider/FlagProvider.tsx | 20 ++--- .../antd/association-field/Nester.tsx | 13 ++- .../antd/association-field/SubTable.tsx | 81 ++++++++++--------- .../src/schema-settings/SchemaSettings.tsx | 14 +++- .../VariableInput/hooks/useVariableOptions.ts | 21 ++--- 5 files changed, 81 insertions(+), 68 deletions(-) diff --git a/packages/core/client/src/flag-provider/FlagProvider.tsx b/packages/core/client/src/flag-provider/FlagProvider.tsx index 229bfb5dd..b6cedae68 100644 --- a/packages/core/client/src/flag-provider/FlagProvider.tsx +++ b/packages/core/client/src/flag-provider/FlagProvider.tsx @@ -1,4 +1,4 @@ -import React, { useMemo } from 'react'; +import React from 'react'; export interface FlagProviderProps { /** @@ -13,19 +13,19 @@ export interface FlagProviderProps { * 是否存在于 `表单数据模板` 中 */ isInFormDataTemplate?: boolean; + /** + * 是否存在于 `子表格` 中 + */ + isInSubTable?: boolean; + /** + * 是否存在于 `子表单` 中 + */ + isInSubForm?: boolean; children: any; } export const FlagContext = React.createContext>(null); export const FlagProvider = (props: FlagProviderProps) => { - const value = useMemo(() => { - return { - isInAssignFieldValues: props.isInAssignFieldValues, - isInSetDefaultValueDialog: props.isInSetDefaultValueDialog, - isInFormDataTemplate: props.isInFormDataTemplate, - }; - }, [props.isInAssignFieldValues, props.isInFormDataTemplate, props.isInSetDefaultValueDialog]); - - return {props.children}; + return {props.children}; }; diff --git a/packages/core/client/src/schema-component/antd/association-field/Nester.tsx b/packages/core/client/src/schema-component/antd/association-field/Nester.tsx index 802bf6f29..8c85bfe86 100644 --- a/packages/core/client/src/schema-component/antd/association-field/Nester.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/Nester.tsx @@ -9,6 +9,7 @@ import { Button, Card, Divider, Tooltip } from 'antd'; import React, { useCallback, useContext } from 'react'; import { useTranslation } from 'react-i18next'; import { FormActiveFieldsProvider } from '../../../block-provider'; +import { FlagProvider } from '../../../flag-provider'; import { RecordIndexProvider, RecordProvider } from '../../../record-provider'; import { isPatternDisabled, isSystemField } from '../../../schema-settings'; import { @@ -23,10 +24,18 @@ import { useAssociationFieldContext } from './hooks'; export const Nester = (props) => { const { options } = useContext(AssociationFieldContext); if (['hasOne', 'belongsTo'].includes(options.type)) { - return ; + return ( + + + + ); } if (['hasMany', 'belongsToMany'].includes(options.type)) { - return ; + return ( + + + + ); } return null; }; diff --git a/packages/core/client/src/schema-component/antd/association-field/SubTable.tsx b/packages/core/client/src/schema-component/antd/association-field/SubTable.tsx index 199eabe8c..0fd73eadc 100644 --- a/packages/core/client/src/schema-component/antd/association-field/SubTable.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/SubTable.tsx @@ -8,6 +8,7 @@ import { isArr } from '@formily/shared'; import { Button } from 'antd'; import React from 'react'; import { FormActiveFieldsProvider } from '../../../block-provider'; +import { FlagProvider } from '../../../flag-provider'; import { useSubTableSpecialCase } from '../form-item/hooks/useSpecialCase'; import { Table } from '../table-v2/Table'; import { useAssociationFieldContext } from './hooks'; @@ -62,46 +63,48 @@ export const SubTable: any = observer( } `} > - - + +
+ field.editable && ( + + ) } - .ant-formily-editable { - vertical-align: sub; - } - `} - bordered - size={'small'} - field={field} - showIndex - dragSort={field.editable} - showDel={field.editable} - pagination={false} - rowSelection={{ type: 'none', hideSelectAll: true }} - footer={() => - field.editable && ( - - ) - } - isSubTable={true} - /> - + isSubTable={true} + /> + + ); }, diff --git a/packages/core/client/src/schema-settings/SchemaSettings.tsx b/packages/core/client/src/schema-settings/SchemaSettings.tsx index bb063cfc8..3bc328b6d 100644 --- a/packages/core/client/src/schema-settings/SchemaSettings.tsx +++ b/packages/core/client/src/schema-settings/SchemaSettings.tsx @@ -78,7 +78,7 @@ import { isSameCollection, useSupportedBlocks, } from '../filter-provider/utils'; -import { FlagProvider } from '../flag-provider'; +import { FlagProvider, useFlag } from '../flag-provider'; import { useCollectMenuItem, useCollectMenuItems, useMenuItem } from '../hooks/useMenuItem'; import { getTargetKey } from '../schema-component/antd/association-filter/utilts'; import { DynamicComponentProps } from '../schema-component/antd/filter/DynamicComponent'; @@ -1555,6 +1555,7 @@ SchemaSettings.DefaultValue = function DefaultValueConfigure(props: { fieldSchem const record = useRecord(); const { form } = useFormBlockContext(); const currentFormFields = useCollectionFilterOptions(collection); + const { isInSubForm, isInSubTable } = useFlag() || {}; const { name } = collection; const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']); @@ -1596,7 +1597,7 @@ SchemaSettings.DefaultValue = function DefaultValueConfigure(props: { fieldSchem FormLayout, VariableInput: (props) => { return ( - + ); @@ -1825,6 +1826,7 @@ SchemaSettings.DataScope = function DataScopeConfigure(props: DataScopeProps) { const variables = useVariables(); const localVariables = useLocalVariables(); const { getAllCollectionsInheritChain } = useCollectionManager(); + const { isInSubForm, isInSubTable } = useFlag() || {}; const dynamicComponent = (props: DynamicComponentProps) => { return ( @@ -1855,7 +1857,13 @@ SchemaSettings.DataScope = function DataScopeConfigure(props: DataScopeProps) { properties: { filter: { enum: props.collectionFilterOption || options, - 'x-decorator': BaseVariableProvider, + 'x-decorator': (props) => ( + + + {props.children} + + + ), 'x-decorator-props': { isDisabled, }, diff --git a/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts b/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts index 3017f1848..192349ec5 100644 --- a/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts +++ b/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts @@ -2,7 +2,7 @@ import { Form } from '@formily/core'; import { ISchema, Schema } from '@formily/react'; import { useMemo } from 'react'; import { useFormBlockType } from '../../../block-provider/FormBlockProvider'; -import { CollectionFieldOptions } from '../../../collection-manager'; +import { CollectionFieldOptions, useCollection } from '../../../collection-manager'; import { useFlag } from '../../../flag-provider'; import { useBlockCollection } from './useBlockCollection'; import { useDateVariable } from './useDateVariable'; @@ -46,10 +46,9 @@ export const useVariableOptions = ({ targetFieldSchema, }: Props) => { const { name: blockCollectionName } = useBlockCollection(); - const { isInSetDefaultValueDialog } = useFlag() || {}; + const { isInSubForm, isInSubTable } = useFlag() || {}; const { type: formBlockType } = useFormBlockType(); - - const fieldCollectionName = collectionField?.collectionName; + const { name } = useCollection(); const userVariable = useUserVariable({ maxDepth: 3, uiSchema: uiSchema, @@ -66,7 +65,7 @@ export const useVariableOptions = ({ targetFieldSchema, }); const iterationVariable = useIterationVariable({ - currentCollection: fieldCollectionName, + currentCollection: name, collectionField, schema: uiSchema, noDisabled, @@ -85,12 +84,7 @@ export const useVariableOptions = ({ userVariable, dateVariable, form && !form.readPretty && formVariable, - form && - fieldCollectionName && - blockCollectionName && - fieldCollectionName !== blockCollectionName && - isInSetDefaultValueDialog && - iterationVariable, + (isInSubForm || isInSubTable) && iterationVariable, formBlockType === 'update' && currentRecordVariable, ].filter(Boolean); }, [ @@ -98,9 +92,8 @@ export const useVariableOptions = ({ dateVariable, form, formVariable, - fieldCollectionName, - blockCollectionName, - isInSetDefaultValueDialog, + isInSubForm, + isInSubTable, iterationVariable, currentRecordVariable, ]);