fix: varibales as & &Iteration is not correct in record picker (#3399)

This commit is contained in:
katherinehhh 2024-01-17 11:16:19 +08:00 committed by GitHub
parent 6f72451d27
commit 8d06b73c48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 62 additions and 11 deletions

View File

@ -15,7 +15,7 @@ export const FormBlockContext = createContext<any>({});
const InternalFormBlockProvider = (props) => { const InternalFormBlockProvider = (props) => {
const ctx = useFormBlockContext(); const ctx = useFormBlockContext();
const { action, readPretty, params, association } = props; const { action, readPretty, params, association, collection } = props;
const field = useField(); const field = useField();
const form = useMemo( const form = useMemo(
() => () =>
@ -40,6 +40,7 @@ const InternalFormBlockProvider = (props) => {
resource, resource,
updateAssociationValues, updateAssociationValues,
formBlockRef, formBlockRef,
collectionName: collection,
}; };
}, [action, field, form, params, resource, service, updateAssociationValues]); }, [action, field, form, params, resource, service, updateAssociationValues]);

View File

@ -14,12 +14,13 @@ import {
TableSelectorParamsProvider, TableSelectorParamsProvider,
useTableSelectorProps as useTsp, useTableSelectorProps as useTsp,
} from '../../../block-provider/TableSelectorProvider'; } from '../../../block-provider/TableSelectorProvider';
import { CollectionProvider } from '../../../collection-manager'; import { CollectionProvider, useCollection } from '../../../collection-manager';
import { useCompile } from '../../hooks'; import { useCompile } from '../../hooks';
import { ActionContextProvider } from '../action'; import { ActionContextProvider } from '../action';
import { useAssociationFieldContext, useFieldNames, useInsertSchema } from './hooks'; import { useAssociationFieldContext, useFieldNames, useInsertSchema } from './hooks';
import schema from './schema'; import schema from './schema';
import { flatData, getLabelFormatValue, useLabelUiSchema } from './util'; import { flatData, getLabelFormatValue, useLabelUiSchema } from './util';
import { useFormBlockContext } from '../../../block-provider/FormBlockProvider';
export const useTableSelectorProps = () => { export const useTableSelectorProps = () => {
const field: any = useField(); const field: any = useField();
@ -69,6 +70,7 @@ export const InternalPicker = observer(
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const insertSelector = useInsertSchema('Selector'); const insertSelector = useInsertSchema('Selector');
const { options: collectionField } = useAssociationFieldContext(); const { options: collectionField } = useAssociationFieldContext();
const { collectionName } = useFormBlockContext();
const compile = useCompile(); const compile = useCompile();
const labelUiSchema = useLabelUiSchema(collectionField, fieldNames?.label || 'label'); const labelUiSchema = useLabelUiSchema(collectionField, fieldNames?.label || 'label');
const isAllowAddNew = fieldSchema['x-add-new']; const isAllowAddNew = fieldSchema['x-add-new'];
@ -99,6 +101,7 @@ export const InternalPicker = observer(
selectedRows, selectedRows,
setSelectedRows, setSelectedRows,
collectionField, collectionField,
currentFormCollection: collectionName,
}; };
const getValue = () => { const getValue = () => {

View File

@ -1,12 +1,14 @@
import { ArrayItems } from '@formily/antd-v5'; import { ArrayItems } from '@formily/antd-v5';
import { ISchema, useField, useFieldSchema } from '@formily/react'; import { ISchema, useField, useFieldSchema } from '@formily/react';
import { cloneDeep } from 'lodash'; import _ from 'lodash';
import React from 'react'; import React, { useContext } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useFormBlockContext, useTableSelectorContext } from '../../../block-provider'; import { useFormBlockContext, useTableSelectorContext } from '../../../block-provider';
import { recursiveParent } from '../../../block-provider/TableSelectorProvider'; import { recursiveParent } from '../../../block-provider/TableSelectorProvider';
import { useCollection, useCollectionManager } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { useSortFields } from '../../../collection-manager/action-hooks'; import { useSortFields } from '../../../collection-manager/action-hooks';
import { useRecord } from '../../../record-provider';
import { useLocalVariables, useVariables } from '../../../variables';
import { import {
GeneralSchemaDesigner, GeneralSchemaDesigner,
SchemaSettingsDataScope, SchemaSettingsDataScope,
@ -19,10 +21,13 @@ import {
import { useSchemaTemplate } from '../../../schema-templates'; import { useSchemaTemplate } from '../../../schema-templates';
import { useDesignable } from '../../hooks'; import { useDesignable } from '../../hooks';
import { removeNullCondition } from '../filter'; import { removeNullCondition } from '../filter';
import { VariableInput, getShouldChange } from '../../../schema-settings/VariableInput/VariableInput';
import { RecordPickerContext } from '../../antd/record-picker';
export const TableSelectorDesigner = () => { export const TableSelectorDesigner = () => {
const { name, title } = useCollection(); const { name, title } = useCollection();
const { getCollectionJoinField } = useCollectionManager(); const { getCollectionJoinField, getAllCollectionsInheritChain } = useCollectionManager();
const field = useField(); const field = useField();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const { form } = useFormBlockContext(); const { form } = useFormBlockContext();
@ -47,10 +52,13 @@ export const TableSelectorDesigner = () => {
const template = useSchemaTemplate(); const template = useSchemaTemplate();
const collection = useCollection(); const collection = useCollection();
const { dragSort } = field.decoratorProps; const { dragSort } = field.decoratorProps;
const record = useRecord();
const variables = useVariables();
const { currentFormCollection } = useContext(RecordPickerContext);
const localVariables = useLocalVariables({ collectionName: currentFormCollection });
return ( return (
<GeneralSchemaDesigner template={template} title={title || name} disableInitializer> <GeneralSchemaDesigner template={template} title={title || name} disableInitializer>
<SchemaSettingsDataScope <SchemaSettingsDataScope
collectionName={name}
defaultFilter={fieldSchema?.['x-decorator-props']?.params?.filter || {}} defaultFilter={fieldSchema?.['x-decorator-props']?.params?.filter || {}}
form={form} form={form}
onSubmit={({ filter }) => { onSubmit={({ filter }) => {
@ -59,7 +67,7 @@ export const TableSelectorDesigner = () => {
params.filter = filter; params.filter = filter;
field.decoratorProps.params = params; field.decoratorProps.params = params;
fieldSchema['x-decorator-props']['params'] = params; fieldSchema['x-decorator-props']['params'] = params;
let serviceFilter = cloneDeep(filter); let serviceFilter = _.cloneDeep(filter);
if (extraFilter) { if (extraFilter) {
if (serviceFilter) { if (serviceFilter) {
serviceFilter = { serviceFilter = {
@ -77,6 +85,25 @@ export const TableSelectorDesigner = () => {
}, },
}); });
}} }}
collectionName={name}
dynamicComponent={(props) => {
return (
<VariableInput
{...props}
form={form}
collectionField={props.collectionField}
record={record}
shouldChange={getShouldChange({
collectionField: props.collectionField,
variables,
localVariables,
getAllCollectionsInheritChain,
})}
currentFormCollectionName={currentFormCollection}
currentIterationCollectionName={collectionField.collectionName}
/>
);
}}
/> />
{collection?.tree && collectionField?.target === collectionField?.collectionName && ( {collection?.tree && collectionField?.target === collectionField?.collectionName && (
<SchemaSettingsSwitchItem <SchemaSettingsSwitchItem

View File

@ -41,6 +41,10 @@ type Props = {
style?: React.CSSProperties; style?: React.CSSProperties;
collectionField: CollectionFieldOptions; collectionField: CollectionFieldOptions;
contextCollectionName?: string; contextCollectionName?: string;
/**指定当前表单数据表 */
currentFormCollectionName?: string;
/**指定当前对象数据表 */
currentIterationCollectionName?: string;
/** /**
* `onChange` `onChange` * `onChange` `onChange`
* @param value `onChange` * @param value `onChange`
@ -82,12 +86,23 @@ export const VariableInput = (props: Props) => {
record, record,
returnScope = _.identity, returnScope = _.identity,
targetFieldSchema, targetFieldSchema,
currentFormCollectionName,
currentIterationCollectionName,
} = props; } = props;
const { name: blockCollectionName } = useBlockCollection(); const { name: blockCollectionName } = useBlockCollection();
const scope = useVariableScope(); const scope = useVariableScope();
const { operator, schema: uiSchema = collectionField?.uiSchema } = useValues(); const { operator, schema: uiSchema = collectionField?.uiSchema } = useValues();
const variableOptions = useVariableOptions({ collectionField, form, record, operator, uiSchema, targetFieldSchema }); const variableOptions = useVariableOptions({
collectionField,
form,
record,
operator,
uiSchema,
targetFieldSchema,
currentFormCollectionName,
currentIterationCollectionName,
});
const contextVariable = useContextAssociationFields({ schema, maxDepth: 2, contextCollectionName, collectionField }); const contextVariable = useContextAssociationFields({ schema, maxDepth: 2, contextCollectionName, collectionField });
const { compatOldVariables } = useCompatOldVariables({ const { compatOldVariables } = useCompatOldVariables({
collectionField, collectionField,
@ -115,7 +130,6 @@ export const VariableInput = (props: Props) => {
}, },
[onChange, shouldChange], [onChange, shouldChange],
); );
return ( return (
<Variable.Input <Variable.Input
className={className} className={className}

View File

@ -37,6 +37,10 @@ interface Props {
noDisabled?: boolean; noDisabled?: boolean;
/** 消费变量值的字段 */ /** 消费变量值的字段 */
targetFieldSchema?: Schema; targetFieldSchema?: Schema;
/**指定当前表单数据表 */
currentFormCollectionName?: string;
/**指定当前对象数据表 */
currentIterationCollectionName?: string;
} }
export const useVariableOptions = ({ export const useVariableOptions = ({
@ -47,6 +51,8 @@ export const useVariableOptions = ({
noDisabled, noDisabled,
targetFieldSchema, targetFieldSchema,
record, record,
currentFormCollectionName,
currentIterationCollectionName,
}: Props) => { }: Props) => {
const { name: blockCollectionName = record?.__collectionName } = useBlockCollection(); const { name: blockCollectionName = record?.__collectionName } = useBlockCollection();
const { isInSubForm, isInSubTable } = useFlag() || {}; const { isInSubForm, isInSubTable } = useFlag() || {};
@ -68,13 +74,13 @@ export const useVariableOptions = ({
const dateVariable = useDateVariable({ operator, schema: uiSchema, noDisabled }); const dateVariable = useDateVariable({ operator, schema: uiSchema, noDisabled });
const formVariable = useFormVariable({ const formVariable = useFormVariable({
schema: uiSchema, schema: uiSchema,
collectionName: blockCollectionName, collectionName: currentFormCollectionName || blockCollectionName,
collectionField, collectionField,
noDisabled, noDisabled,
targetFieldSchema, targetFieldSchema,
}); });
const iterationVariable = useIterationVariable({ const iterationVariable = useIterationVariable({
currentCollection: name, currentCollection: currentIterationCollectionName || name,
collectionField, collectionField,
schema: uiSchema, schema: uiSchema,
noDisabled, noDisabled,