fix(variable): fix context error with 'current object' variable (#4901)

* fix(variable): fix context error with 'current object' variable

* test: add e2e test
This commit is contained in:
Zeke Zhang 2024-07-19 09:08:53 +08:00 committed by GitHub
parent 7d660f5a0f
commit 70c085532c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 611 additions and 93 deletions

View File

@ -0,0 +1,34 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { expect, test } from '@nocobase/test/e2e';
import { T4874 } from './templates';
test.describe('variable: current object', () => {
test('in sub table', async ({ page, mockPage }) => {
await mockPage(T4874).goto();
// 在子表格中,使用“当前对象”变量
await page.getByLabel('action-Action.Link-Edit-').click();
await page.getByRole('button', { name: 'Role name', exact: true }).hover();
await page
.getByRole('button', { name: 'designer-schema-settings-TableV2.Column-fieldSettings:TableColumn-roles' })
.hover();
await page.getByRole('menuitem', { name: 'Set default value' }).click();
await page.getByLabel('variable-button').click();
await page.getByRole('menuitemcheckbox', { name: 'Current object right' }).click();
await page.getByRole('menuitemcheckbox', { name: 'Role UID' }).click();
await page.getByRole('button', { name: 'OK', exact: true }).click();
await page.getByRole('button', { name: 'Add new' }).click();
await page.getByRole('row', { name: 'table-index-4 block-item-' }).getByRole('textbox').nth(1).fill('123456');
await expect(page.getByRole('row', { name: 'table-index-4 block-item-' }).getByRole('textbox').first()).toHaveValue(
'123456',
);
});
});

View File

@ -388,3 +388,478 @@ export const APIToken = {
'x-index': 1,
},
};
export const T4874 = {
pageSchema: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Page',
'x-app-version': '1.2.21-alpha',
properties: {
d5btnu6z239: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid',
'x-initializer': 'page:addBlock',
'x-app-version': '1.2.21-alpha',
properties: {
dqmrwgx3h5j: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid.Row',
'x-app-version': '1.2.21-alpha',
properties: {
'3q6wd3o9ve3': {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid.Col',
'x-app-version': '1.2.21-alpha',
properties: {
'1mw2svlh1t4': {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-decorator': 'TableBlockProvider',
'x-acl-action': 'users:list',
'x-use-decorator-props': 'useTableBlockDecoratorProps',
'x-decorator-props': {
collection: 'users',
dataSource: 'main',
action: 'list',
params: {
pageSize: 20,
},
rowKey: 'id',
showIndex: true,
dragSort: false,
},
'x-toolbar': 'BlockSchemaToolbar',
'x-settings': 'blockSettings:table',
'x-component': 'CardItem',
'x-filter-targets': [],
'x-app-version': '1.2.21-alpha',
properties: {
actions: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-initializer': 'table:configureActions',
'x-component': 'ActionBar',
'x-component-props': {
style: {
marginBottom: 'var(--nb-spacing)',
},
},
'x-app-version': '1.2.21-alpha',
'x-uid': '66gj8p6hj5f',
'x-async': false,
'x-index': 1,
},
no5z0g08zmh: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'array',
'x-initializer': 'table:configureColumns',
'x-component': 'TableV2',
'x-use-component-props': 'useTableBlockProps',
'x-component-props': {
rowKey: 'id',
rowSelection: {
type: 'checkbox',
},
},
'x-app-version': '1.2.21-alpha',
properties: {
actions: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
title: '{{ t("Actions") }}',
'x-action-column': 'actions',
'x-decorator': 'TableV2.Column.ActionBar',
'x-component': 'TableV2.Column',
'x-toolbar': 'TableColumnSchemaToolbar',
'x-initializer': 'table:configureItemActions',
'x-settings': 'fieldSettings:TableColumn',
'x-toolbar-props': {
initializer: 'table:configureItemActions',
},
'x-app-version': '1.2.21-alpha',
properties: {
'07oxu98m060': {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-decorator': 'DndContext',
'x-component': 'Space',
'x-component-props': {
split: '|',
},
'x-app-version': '1.2.21-alpha',
properties: {
'40ond6gt5sp': {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
title: '{{ t("Edit") }}',
'x-action': 'update',
'x-toolbar': 'ActionSchemaToolbar',
'x-settings': 'actionSettings:edit',
'x-component': 'Action.Link',
'x-component-props': {
openMode: 'drawer',
icon: 'EditOutlined',
},
'x-decorator': 'ACLActionProvider',
'x-designer-props': {
linkageAction: true,
},
properties: {
drawer: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
title: '{{ t("Edit record") }}',
'x-component': 'Action.Container',
'x-component-props': {
className: 'nb-action-popup',
},
properties: {
tabs: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Tabs',
'x-component-props': {},
'x-initializer': 'popup:addTab',
properties: {
tab1: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
title: '{{t("Edit")}}',
'x-component': 'Tabs.TabPane',
'x-designer': 'Tabs.Designer',
'x-component-props': {},
properties: {
grid: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid',
'x-initializer': 'popup:common:addBlock',
properties: {
xv1rqhlertx: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid.Row',
'x-app-version': '1.2.21-alpha',
properties: {
j7nzjmqyrt6: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid.Col',
'x-app-version': '1.2.21-alpha',
properties: {
te5nblgjx91: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-acl-action-props': {
skipScopeCheck: false,
},
'x-acl-action': 'users:update',
'x-decorator': 'FormBlockProvider',
'x-use-decorator-props':
'useEditFormBlockDecoratorProps',
'x-decorator-props': {
action: 'get',
dataSource: 'main',
collection: 'users',
},
'x-toolbar': 'BlockSchemaToolbar',
'x-settings': 'blockSettings:editForm',
'x-component': 'CardItem',
'x-app-version': '1.2.21-alpha',
properties: {
'1c7o18qoag1': {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'FormV2',
'x-use-component-props': 'useEditFormBlockProps',
'x-app-version': '1.2.21-alpha',
properties: {
grid: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid',
'x-initializer': 'form:configureFields',
'x-app-version': '1.2.21-alpha',
properties: {
'2k2z18gbdoy': {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid.Row',
'x-app-version': '1.2.21-alpha',
properties: {
c607566i7l3: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component': 'Grid.Col',
'x-app-version': '1.2.21-alpha',
properties: {
roles: {
'x-uid': 'nz02ten5fzf',
_isJSONSchemaObject: true,
version: '2.0',
type: 'string',
'x-toolbar':
'FormItemSchemaToolbar',
'x-settings':
'fieldSettings:FormItem',
'x-component': 'CollectionField',
'x-decorator': 'FormItem',
'x-collection-field': 'users.roles',
'x-component-props': {
fieldNames: {
label: 'name',
value: 'name',
},
mode: 'SubTable',
},
'x-app-version': '1.2.21-alpha',
properties: {
fj6qv4odhaa: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-component':
'AssociationField.SubTable',
'x-initializer':
'table:configureColumns',
'x-initializer-props': {
action: false,
},
'x-index': 1,
'x-app-version': '1.2.21-alpha',
properties: {
'5j2fwrcfzuu': {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-decorator':
'TableV2.Column.Decorator',
'x-toolbar':
'TableColumnSchemaToolbar',
'x-settings':
'fieldSettings:TableColumn',
'x-component':
'TableV2.Column',
'x-app-version':
'1.2.21-alpha',
properties: {
title: {
_isJSONSchemaObject:
true,
version: '2.0',
'x-collection-field':
'roles.title',
'x-component':
'CollectionField',
'x-component-props': {
ellipsis: true,
},
'x-decorator':
'FormItem',
'x-decorator-props': {
labelStyle: {
display: 'none',
},
},
'x-app-version':
'1.2.21-alpha',
'x-uid': '6hnbfgke4v9',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'xrfnjtprhgk',
'x-async': false,
'x-index': 1,
},
nm5lqvmdmol: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-decorator':
'TableV2.Column.Decorator',
'x-toolbar':
'TableColumnSchemaToolbar',
'x-settings':
'fieldSettings:TableColumn',
'x-component':
'TableV2.Column',
'x-app-version':
'1.2.21-alpha',
properties: {
name: {
_isJSONSchemaObject:
true,
version: '2.0',
'x-collection-field':
'roles.name',
'x-component':
'CollectionField',
'x-component-props': {
ellipsis: true,
},
'x-decorator':
'FormItem',
'x-decorator-props': {
labelStyle: {
display: 'none',
},
},
'x-app-version':
'1.2.21-alpha',
'x-uid': 'nug7nu8ebhi',
'x-async': false,
'x-index': 1,
},
},
'x-uid': '0esgluhnedb',
'x-async': false,
'x-index': 2,
},
},
'x-uid': 'xsx2rz70uwz',
'x-async': false,
},
},
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'r23ltwfb8by',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'tudt41ppj3p',
'x-async': false,
'x-index': 1,
},
},
'x-uid': '96z12k7x884',
'x-async': false,
'x-index': 1,
},
qq4upukd8xf: {
_isJSONSchemaObject: true,
version: '2.0',
type: 'void',
'x-initializer': 'editForm:configureActions',
'x-component': 'ActionBar',
'x-component-props': {
layout: 'one-column',
},
'x-app-version': '1.2.21-alpha',
'x-uid': 'wdmwmy60e2r',
'x-async': false,
'x-index': 2,
},
},
'x-uid': '33zsf8du7j2',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'vfpz90ab0qw',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'gujrsvgrmjj',
'x-async': false,
'x-index': 1,
},
},
'x-uid': '8xws8vfulhx',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'nl73uc805kr',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'izwg6n04xw3',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'p6wyw65zj1l',
'x-async': false,
'x-index': 1,
},
},
'x-uid': '7hlmuhqrtpk',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'yfxjokc1rxx',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'rriritgfmdw',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'liz6v1lau34',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'vh37l43zevq',
'x-async': false,
'x-index': 2,
},
},
'x-uid': 'wxsf1stb1oi',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'nnkwbb57n9f',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'dfonxd85q4n',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'uxb09qu50d0',
'x-async': false,
'x-index': 1,
},
},
'x-uid': 'kg5tkkrw3x4',
'x-async': true,
'x-index': 1,
},
};

View File

@ -14,7 +14,6 @@ import { observer, RecursionField, useFieldSchema } from '@formily/react';
import { action } from '@formily/reactive';
import { isArr } from '@formily/shared';
import { Button } from 'antd';
import { unionBy, uniqBy } from 'lodash';
import React, { useContext, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
@ -28,15 +27,66 @@ import { useCreateActionProps } from '../../../block-provider/hooks';
import { FormActiveFieldsProvider } from '../../../block-provider/hooks/useFormActiveFields';
import { TableSelectorParamsProvider } from '../../../block-provider/TableSelectorProvider';
import { CollectionProvider_deprecated } from '../../../collection-manager';
import { CollectionRecordProvider, useCollectionRecord } from '../../../data-source';
import { CollectionRecordProvider, useCollection, useCollectionRecord } from '../../../data-source';
import { markRecordAsNew } from '../../../data-source/collection-record/isNewRecord';
import { FlagProvider } from '../../../flag-provider';
import { useCompile } from '../../hooks';
import { ActionContextProvider } from '../action';
import { Table } from '../table-v2/Table';
import { useAssociationFieldContext, useFieldNames } from './hooks';
import { SubFormProvider, useAssociationFieldContext, useFieldNames } from './hooks';
import { useTableSelectorProps } from './InternalPicker';
import { getLabelFormatValue, useLabelUiSchema } from './util';
import { markRecordAsNew } from '../../../data-source/collection-record/isNewRecord';
const subTableContainer = css`
.ant-table-footer {
padding: 0 !important;
}
.ant-formily-item-error-help {
display: none;
}
.ant-description-textarea {
line-height: 34px;
}
.ant-table-cell .ant-formily-item-error-help {
display: block;
position: absolute;
font-size: 12px;
top: 100%;
background: #fff;
width: 100%;
margin-top: -15px;
padding: 3px;
z-index: 1;
border-radius: 3px;
box-shadow: 0 0 10px #eee;
animation: none;
transform: translateY(0);
opacity: 1;
}
`;
const tableClassName = css`
.ant-formily-item.ant-formily-item-feedback-layout-loose {
margin-bottom: 0px !important;
}
.ant-formily-editable {
vertical-align: sub;
}
.ant-table-footer {
display: flex;
}
`;
const addNewButtonClassName = css`
display: block;
border-radius: 0px;
border-right: 1px solid rgba(0, 0, 0, 0.06);
`;
const selectButtonClassName = css`
display: block;
border-radius: 0px;
`;
export const SubTable: any = observer(
(props: any) => {
@ -50,6 +100,7 @@ export const SubTable: any = observer(
const compile = useCompile();
const labelUiSchema = useLabelUiSchema(collectionField, fieldNames?.label || 'label');
const recordV2 = useCollectionRecord();
const collection = useCollection();
const move = (fromIndex: number, toIndex: number) => {
if (toIndex === undefined) return;
if (!isArr(field.value)) return;
@ -115,98 +166,56 @@ export const SubTable: any = observer(
return filter;
};
return (
<div
className={css`
.ant-table-footer {
padding: 0 !important;
}
.ant-formily-item-error-help {
display: none;
}
.ant-description-textarea {
line-height: 34px;
}
.ant-table-cell .ant-formily-item-error-help {
display: block;
position: absolute;
font-size: 12px;
top: 100%;
background: #fff;
width: 100%;
margin-top: -15px;
padding: 3px;
z-index: 1;
border-radius: 3px;
box-shadow: 0 0 10px #eee;
animation: none;
transform: translateY(0);
opacity: 1;
}
`}
>
<div className={subTableContainer}>
<FlagProvider isInSubTable>
<CollectionRecordProvider record={null} parentRecord={recordV2}>
<FormActiveFieldsProvider name="nester">
<Table
className={css`
.ant-formily-item.ant-formily-item-feedback-layout-loose {
margin-bottom: 0px !important;
{/* 在这里加,是为了让 “当前对象” 的配置显示正确 */}
<SubFormProvider value={{ value: null, collection }}>
<Table
className={tableClassName}
bordered
size={'small'}
field={field}
showIndex
dragSort={false}
showDel={field.editable}
pagination={false}
rowSelection={{ type: 'none', hideSelectAll: true }}
footer={() =>
field.editable && (
<>
{field.componentProps?.allowAddnew !== false && (
<Button
type={'text'}
block
className={addNewButtonClassName}
onClick={() => {
field.value = field.value || [];
field.value.push(markRecordAsNew({}));
}}
>
{t('Add new')}
</Button>
)}
{field.componentProps?.allowSelectExistingRecord && (
<Button
type={'text'}
block
className={selectButtonClassName}
onClick={() => {
setVisibleSelector(true);
}}
>
{t('Select')}
</Button>
)}
</>
)
}
.ant-formily-editable {
vertical-align: sub;
}
.ant-table-footer {
display: flex;
}
`}
bordered
size={'small'}
field={field}
showIndex
dragSort={false}
showDel={field.editable}
pagination={false}
rowSelection={{ type: 'none', hideSelectAll: true }}
footer={() =>
field.editable && (
<>
{field.componentProps?.allowAddnew !== false && (
<Button
type={'text'}
block
className={css`
display: block;
border-radius: 0px;
border-right: 1px solid rgba(0, 0, 0, 0.06);
`}
onClick={() => {
field.value = field.value || [];
field.value.push(markRecordAsNew({}));
}}
>
{t('Add new')}
</Button>
)}
{field.componentProps?.allowSelectExistingRecord && (
<Button
type={'text'}
block
className={css`
display: block;
border-radius: 0px;
`}
onClick={() => {
setVisibleSelector(true);
}}
>
{t('Select')}
</Button>
)}
</>
)
}
isSubTable={true}
/>
isSubTable={true}
/>
</SubFormProvider>
</FormActiveFieldsProvider>
</CollectionRecordProvider>
</FlagProvider>

View File

@ -10,11 +10,11 @@
import { Schema } from '@formily/json-schema';
import { useTranslation } from 'react-i18next';
import { CollectionFieldOptions_deprecated } from '../../../collection-manager';
import { useCollection } from '../../../data-source';
import { CollectionFieldOptions } from '../../../data-source/collection/Collection';
import { useFlag } from '../../../flag-provider';
import { useSubFormValue } from '../../../schema-component/antd/association-field/hooks';
import { useBaseVariable } from './useBaseVariable';
import { useCollection } from '../../../data-source';
/**
* @deprecated