fix(variable): should have currentObject in sub-blocks (#2823)

* chore: avoid crashing

* fix(variable): should have currentObject in sub-blocks
This commit is contained in:
被雨水过滤的空气-Rain 2023-10-16 22:49:48 +08:00 committed by GitHub
parent 24d914b0ef
commit 9db9b9e7af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 68 deletions

View File

@ -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<Omit<FlagProviderProps, 'children'>>(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 <FlagContext.Provider value={value}>{props.children}</FlagContext.Provider>;
return <FlagContext.Provider value={props}>{props.children}</FlagContext.Provider>;
};

View File

@ -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 <ToOneNester {...props} />;
return (
<FlagProvider isInSubForm>
<ToOneNester {...props} />
</FlagProvider>
);
}
if (['hasMany', 'belongsToMany'].includes(options.type)) {
return <ToManyNester {...props} />;
return (
<FlagProvider isInSubForm>
<ToManyNester {...props} />
</FlagProvider>
);
}
return null;
};

View File

@ -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(
}
`}
>
<FormActiveFieldsProvider name="nester">
<Table
className={css`
.ant-formily-item.ant-formily-item-feedback-layout-loose {
margin-bottom: 0px !important;
<FlagProvider isInSubTable>
<FormActiveFieldsProvider name="nester">
<Table
className={css`
.ant-formily-item.ant-formily-item-feedback-layout-loose {
margin-bottom: 0px !important;
}
.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 && (
<Button
type={'text'}
block
className={css`
display: block;
`}
onClick={() => {
field.value = field.value || [];
field.value.push({});
field.onInput(field.value);
}}
icon={<PlusOutlined />}
>
{/* {t('Add new')} */}
</Button>
)
}
.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 && (
<Button
type={'text'}
block
className={css`
display: block;
`}
onClick={() => {
field.value = field.value || [];
field.value.push({});
field.onInput(field.value);
}}
icon={<PlusOutlined />}
>
{/* {t('Add new')} */}
</Button>
)
}
isSubTable={true}
/>
</FormActiveFieldsProvider>
isSubTable={true}
/>
</FormActiveFieldsProvider>
</FlagProvider>
</div>
);
},

View File

@ -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 (
<FlagProvider isInSetDefaultValueDialog={true}>
<FlagProvider isInSubForm={isInSubForm} isInSubTable={isInSubTable} isInSetDefaultValueDialog>
<VariableInput {...props} />
</FlagProvider>
);
@ -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) => (
<BaseVariableProvider {...props}>
<FlagProvider isInSubForm={isInSubForm} isInSubTable={isInSubTable}>
{props.children}
</FlagProvider>
</BaseVariableProvider>
),
'x-decorator-props': {
isDisabled,
},

View File

@ -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,
]);