@nocobase/client-v2 2.4.0-alpha.5 → 2.4.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/components/form/ScanInput/useCodeScanner.d.ts +12 -2
- package/es/components/form/ScanInput/zxingWasmDecoder.d.ts +9 -0
- package/es/flow/components/FieldAssignValueInput.d.ts +2 -0
- package/es/flow/components/field-value-variable/FieldValueVariableInput.d.ts +1 -0
- package/es/flow/components/filter/VariableFilterItem.d.ts +7 -0
- package/es/index.mjs +20 -20
- package/lib/index.js +111 -111
- package/package.json +9 -8
- package/src/components/form/ScanInput/CodeScanner.tsx +3 -1
- package/src/components/form/ScanInput/__tests__/CodeScanner.test.tsx +7 -1
- package/src/components/form/ScanInput/__tests__/useCodeScanner.test.tsx +182 -8
- package/src/components/form/ScanInput/__tests__/zxingWasmDecoder.test.ts +78 -0
- package/src/components/form/ScanInput/useCodeScanner.ts +135 -20
- package/src/components/form/ScanInput/zxingWasmDecoder.ts +64 -0
- package/src/flow/actions/__tests__/validation.test.ts +48 -0
- package/src/flow/admin-shell/admin-layout/__tests__/TopbarActionsBar.test.tsx +39 -0
- package/src/flow/components/FieldAssignValueInput.tsx +4 -0
- package/src/flow/components/field-value-variable/FieldValueVariableInput.tsx +21 -12
- package/src/flow/components/field-value-variable/__tests__/FieldValueVariableInput.test.tsx +9 -0
- package/src/flow/components/filter/VariableFilterItem.tsx +11 -3
- package/src/flow/components/filter/__tests__/VariableFilterItem.rightMetaTree.test.tsx +158 -0
- package/src/flow/models/base/GridModel.tsx +0 -1
- package/src/flow/models/blocks/assign-form/AssignFormGridModel.tsx +22 -1
- package/src/flow/models/blocks/assign-form/AssignFormItemModel.tsx +14 -3
- package/src/flow/models/blocks/assign-form/__tests__/assignFieldValuesFlow.editor.test.tsx +140 -0
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormGridModel.toggleFormFieldsCollapse.test.ts +29 -0
- package/src/flow/models/blocks/filter-form/fields/FieldComponentProps.tsx +1 -1
- package/src/flow/models/blocks/filter-form/fields/__tests__/FieldComponentProps.options.test.tsx +61 -0
- package/src/flow/models/blocks/form/__tests__/popupLinkage.test.tsx +175 -0
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/PopupSubTableFieldModel.tsx +4 -3
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableField.tsx +5 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
let decoderPromise: Promise<typeof import('zxing-wasm/reader')> | undefined;
|
|
11
|
+
|
|
12
|
+
async function loadDecoder() {
|
|
13
|
+
if (!decoderPromise) {
|
|
14
|
+
decoderPromise = Promise.all([import('zxing-wasm/reader'), import('zxing-wasm/reader/zxing_reader.wasm')])
|
|
15
|
+
.then(async ([decoder, { default: wasmUrl }]) => {
|
|
16
|
+
try {
|
|
17
|
+
await decoder.prepareZXingModule({
|
|
18
|
+
fireImmediately: true,
|
|
19
|
+
overrides: { locateFile: () => wasmUrl },
|
|
20
|
+
});
|
|
21
|
+
} catch (error) {
|
|
22
|
+
decoder.purgeZXingModule();
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
return decoder;
|
|
26
|
+
})
|
|
27
|
+
.catch((error: unknown) => {
|
|
28
|
+
decoderPromise = undefined;
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return decoderPromise;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function decodeQrCodeWithZxingWasm(imageData: ImageData) {
|
|
36
|
+
const decoder = await loadDecoder();
|
|
37
|
+
const results = await decoder.readBarcodes(imageData, {
|
|
38
|
+
binarizer: 'GlobalHistogram',
|
|
39
|
+
downscaleThreshold: 300,
|
|
40
|
+
formats: ['QRCode'],
|
|
41
|
+
maxNumberOfSymbols: 1,
|
|
42
|
+
tryDenoise: true,
|
|
43
|
+
tryDownscale: true,
|
|
44
|
+
tryHarder: true,
|
|
45
|
+
tryInvert: true,
|
|
46
|
+
tryRotate: true,
|
|
47
|
+
});
|
|
48
|
+
if (results[0]?.text) {
|
|
49
|
+
return results[0].text;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const fallbackResults = await decoder.readBarcodes(imageData, {
|
|
53
|
+
binarizer: 'LocalAverage',
|
|
54
|
+
downscaleThreshold: 300,
|
|
55
|
+
formats: ['QRCode'],
|
|
56
|
+
maxNumberOfSymbols: 1,
|
|
57
|
+
tryDenoise: true,
|
|
58
|
+
tryDownscale: true,
|
|
59
|
+
tryHarder: true,
|
|
60
|
+
tryInvert: true,
|
|
61
|
+
tryRotate: true,
|
|
62
|
+
});
|
|
63
|
+
return fallbackResults[0]?.text;
|
|
64
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
11
|
+
import { validation } from '../validation';
|
|
12
|
+
|
|
13
|
+
vi.mock('../../../flow-compat', () => ({ FieldValidation: () => null }));
|
|
14
|
+
|
|
15
|
+
describe('validation action', () => {
|
|
16
|
+
it('rejects excess decimal places with a translated error and retains collection rules', async () => {
|
|
17
|
+
const setProps = vi.fn();
|
|
18
|
+
const collectionRule = { validator: vi.fn().mockResolvedValue(undefined) };
|
|
19
|
+
const t = vi.fn((key: string, options?: Record<string, unknown>) => {
|
|
20
|
+
return `${options?.label} 精度不能超过 ${options?.limit} 位小数`;
|
|
21
|
+
});
|
|
22
|
+
const ctx = {
|
|
23
|
+
model: {
|
|
24
|
+
props: { label: '工龄' },
|
|
25
|
+
collectionField: { getComponentProps: () => ({ rules: [collectionRule] }) },
|
|
26
|
+
setProps,
|
|
27
|
+
},
|
|
28
|
+
t,
|
|
29
|
+
} as unknown as Parameters<typeof validation.handler>[0];
|
|
30
|
+
|
|
31
|
+
await validation.handler(ctx, {
|
|
32
|
+
validation: { type: 'number', rules: [{ name: 'precision', args: { limit: 2 } }] },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const { rules } = setProps.mock.calls[0][0] as {
|
|
36
|
+
rules: { validator: (rule: unknown, value: unknown) => Promise<void> }[];
|
|
37
|
+
};
|
|
38
|
+
expect(rules).toHaveLength(2);
|
|
39
|
+
expect(rules[0]).toBe(collectionRule);
|
|
40
|
+
await expect(rules[1].validator({}, 39.2234)).rejects.toBe('工龄 精度不能超过 2 位小数');
|
|
41
|
+
expect(t).toHaveBeenCalledWith(
|
|
42
|
+
'number.precision',
|
|
43
|
+
expect.objectContaining({ ns: 'data-source-main', label: '工龄', limit: 2 }),
|
|
44
|
+
);
|
|
45
|
+
await expect(rules[1].validator({}, 39.22)).resolves.toBeUndefined();
|
|
46
|
+
await expect(rules[1].validator({}, null)).resolves.toBeUndefined();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -17,6 +17,7 @@ const { allowMock, appMock, flowModelRendererSpy } = vi.hoisted(() => {
|
|
|
17
17
|
allowMock: vi.fn(),
|
|
18
18
|
appMock: {
|
|
19
19
|
current: {
|
|
20
|
+
name: 'main',
|
|
20
21
|
router: {
|
|
21
22
|
getBasename: () => '/nocobase/v',
|
|
22
23
|
},
|
|
@@ -86,6 +87,7 @@ describe('TopbarActionsBar helpers', () => {
|
|
|
86
87
|
allowMock.mockReset();
|
|
87
88
|
flowModelRendererSpy.mockClear();
|
|
88
89
|
appMock.current = {
|
|
90
|
+
name: 'main',
|
|
89
91
|
router: {
|
|
90
92
|
getBasename: () => '/nocobase/v',
|
|
91
93
|
},
|
|
@@ -305,6 +307,43 @@ describe('TopbarActionsBar helpers', () => {
|
|
|
305
307
|
expect(link).toHaveAttribute('target', '_blank');
|
|
306
308
|
expect(link).toHaveAttribute('rel', expect.stringContaining('noopener'));
|
|
307
309
|
});
|
|
310
|
+
it.each(['/nocobase/v', '/v', '/nocobase/v/apps/jhb20'])(
|
|
311
|
+
'should open standalone sub-app settings with basename %s',
|
|
312
|
+
(basename) => {
|
|
313
|
+
appMock.current.name = 'jhb20';
|
|
314
|
+
appMock.current.router.getBasename = () => basename;
|
|
315
|
+
const items = getTopbarPluginSettingsItems({
|
|
316
|
+
canManagePlugins: false,
|
|
317
|
+
t: (key) => key,
|
|
318
|
+
settings: [
|
|
319
|
+
{
|
|
320
|
+
key: 'ai',
|
|
321
|
+
name: 'ai',
|
|
322
|
+
title: 'AI employees',
|
|
323
|
+
path: '/admin/settings/ai',
|
|
324
|
+
icon: null,
|
|
325
|
+
componentLoader: async () => null,
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
});
|
|
329
|
+
const item = items[0];
|
|
330
|
+
if (!item || !('label' in item)) {
|
|
331
|
+
throw new Error('Expected settings menu item');
|
|
332
|
+
}
|
|
333
|
+
const appBase = basename.endsWith('/apps/jhb20') ? basename : `${basename}/apps/jhb20`;
|
|
334
|
+
const targetHref = `${basename === '/v' ? '' : '/nocobase'}/settings/apps/jhb20/ai`;
|
|
335
|
+
render(
|
|
336
|
+
<MemoryRouter basename={basename} initialEntries={[`${appBase}/admin/a3pq1t1773a`]}>
|
|
337
|
+
{item.label}
|
|
338
|
+
</MemoryRouter>,
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
const link = screen.getByRole('link', { name: 'AI employees' });
|
|
342
|
+
expect(link).toHaveAttribute('href', targetHref);
|
|
343
|
+
expect(link).toHaveAttribute('target', '_blank');
|
|
344
|
+
expect(link).toHaveAttribute('rel', expect.stringContaining('noopener'));
|
|
345
|
+
},
|
|
346
|
+
);
|
|
308
347
|
|
|
309
348
|
it('should not treat admin-like paths as admin runtime', () => {
|
|
310
349
|
const items = getTopbarPluginSettingsItems({
|
|
@@ -71,6 +71,8 @@ interface Props {
|
|
|
71
71
|
enableDateVariableAsConstant?: boolean;
|
|
72
72
|
/** 是否允许在变量选择器中使用 RunJS。默认 true,保持历史行为。 */
|
|
73
73
|
allowRunJS?: boolean;
|
|
74
|
+
/** 是否允许在变量选择器中使用内置日期变量。默认 true。 */
|
|
75
|
+
allowDateVariables?: boolean;
|
|
74
76
|
maxAssociationFieldDepth?: number;
|
|
75
77
|
disabled?: boolean;
|
|
76
78
|
variableConverters?: VariableInputProps['converters'];
|
|
@@ -444,6 +446,7 @@ export const FieldAssignValueInput: React.FC<Props> = ({
|
|
|
444
446
|
preferFormItemFieldModel,
|
|
445
447
|
associationFieldNamesOverride,
|
|
446
448
|
allowRunJS = true,
|
|
449
|
+
allowDateVariables = true,
|
|
447
450
|
maxAssociationFieldDepth = 2,
|
|
448
451
|
disabled = false,
|
|
449
452
|
variableConverters,
|
|
@@ -972,6 +975,7 @@ export const FieldAssignValueInput: React.FC<Props> = ({
|
|
|
972
975
|
style={{ width: '100%' }}
|
|
973
976
|
clearValue={''}
|
|
974
977
|
allowRunJS={allowRunJS}
|
|
978
|
+
allowDateVariables={allowDateVariables}
|
|
975
979
|
disabled={disabled}
|
|
976
980
|
converters={variableConverters}
|
|
977
981
|
/>
|
|
@@ -71,6 +71,7 @@ export type FieldValueVariableInputProps = Omit<
|
|
|
71
71
|
isDateLikeField: boolean;
|
|
72
72
|
dateComponentProps: DateVariableComponentProps;
|
|
73
73
|
allowRunJS?: boolean;
|
|
74
|
+
allowDateVariables?: boolean;
|
|
74
75
|
converters?: VariableInputProps['converters'];
|
|
75
76
|
};
|
|
76
77
|
|
|
@@ -149,6 +150,7 @@ export const FieldValueVariableInput: React.FC<FieldValueVariableInputProps> = (
|
|
|
149
150
|
isDateLikeField,
|
|
150
151
|
dateComponentProps,
|
|
151
152
|
allowRunJS = true,
|
|
153
|
+
allowDateVariables = true,
|
|
152
154
|
converters,
|
|
153
155
|
clearValue = '',
|
|
154
156
|
disabled = false,
|
|
@@ -168,7 +170,7 @@ export const FieldValueVariableInput: React.FC<FieldValueVariableInputProps> = (
|
|
|
168
170
|
return Component;
|
|
169
171
|
}, [dateComponentProps, isDateLikeField]);
|
|
170
172
|
|
|
171
|
-
const parsedDateConfig = parseCtxDateExpressionConfig(value);
|
|
173
|
+
const parsedDateConfig = allowDateVariables ? parseCtxDateExpressionConfig(value) : undefined;
|
|
172
174
|
const restoreLegacyNowForPureDate =
|
|
173
175
|
dateComponentProps.exactNormalizeMode === 'date' &&
|
|
174
176
|
parsedDateConfig?.kind === 'preset' &&
|
|
@@ -230,14 +232,18 @@ export const FieldValueVariableInput: React.FC<FieldValueVariableInputProps> = (
|
|
|
230
232
|
paths: ['null'],
|
|
231
233
|
render: (props) => <NullComponent {...props} />,
|
|
232
234
|
},
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
235
|
+
...(allowDateVariables
|
|
236
|
+
? [
|
|
237
|
+
{
|
|
238
|
+
title: tExpr('Date'),
|
|
239
|
+
name: 'date',
|
|
240
|
+
type: 'date',
|
|
241
|
+
paths: ['date'],
|
|
242
|
+
selectable: false,
|
|
243
|
+
children: dateChildren,
|
|
244
|
+
} satisfies MetaTreeNode,
|
|
245
|
+
]
|
|
246
|
+
: []),
|
|
241
247
|
...(allowRunJS
|
|
242
248
|
? [
|
|
243
249
|
{
|
|
@@ -257,6 +263,7 @@ export const FieldValueVariableInput: React.FC<FieldValueVariableInputProps> = (
|
|
|
257
263
|
DateEditor,
|
|
258
264
|
NullComponent,
|
|
259
265
|
RunJSComponent,
|
|
266
|
+
allowDateVariables,
|
|
260
267
|
allowRunJS,
|
|
261
268
|
baseMetaTree,
|
|
262
269
|
dateComponentProps.exactNormalizeMode,
|
|
@@ -294,7 +301,7 @@ export const FieldValueVariableInput: React.FC<FieldValueVariableInputProps> = (
|
|
|
294
301
|
const firstPath = meta?.paths?.[0];
|
|
295
302
|
if (firstPath === 'constant') return ConstantComponent;
|
|
296
303
|
if (firstPath === 'null') return NullComponent;
|
|
297
|
-
if (firstPath === 'date') return DateEditor;
|
|
304
|
+
if (allowDateVariables && firstPath === 'date') return DateEditor;
|
|
298
305
|
if (allowRunJS && firstPath === 'runjs') return RunJSComponent;
|
|
299
306
|
return null;
|
|
300
307
|
},
|
|
@@ -304,7 +311,7 @@ export const FieldValueVariableInput: React.FC<FieldValueVariableInputProps> = (
|
|
|
304
311
|
const firstPath = item?.paths?.[0];
|
|
305
312
|
if (firstPath === 'constant') return '';
|
|
306
313
|
if (firstPath === 'null') return null;
|
|
307
|
-
if (firstPath === 'date') {
|
|
314
|
+
if (allowDateVariables && firstPath === 'date') {
|
|
308
315
|
return createInitialDateConfig(item.paths[1], isDateLikeField, dateComponentProps);
|
|
309
316
|
}
|
|
310
317
|
if (allowRunJS && firstPath === 'runjs') return { code: '', version: 'v2' };
|
|
@@ -315,7 +322,9 @@ export const FieldValueVariableInput: React.FC<FieldValueVariableInputProps> = (
|
|
|
315
322
|
if (external !== undefined) return external;
|
|
316
323
|
if (currentValue === null) return ['null'];
|
|
317
324
|
if (allowRunJS && isRunJSValue(currentValue)) return ['runjs'];
|
|
318
|
-
if (isDateVariableEditConfig(currentValue))
|
|
325
|
+
if (allowDateVariables && isDateVariableEditConfig(currentValue)) {
|
|
326
|
+
return ['date', getDateNodeName(currentValue)];
|
|
327
|
+
}
|
|
319
328
|
return typeof currentValue === 'string' && isVariableExpression(currentValue)
|
|
320
329
|
? parseValueToPath(currentValue)
|
|
321
330
|
: ['constant'];
|
|
@@ -50,6 +50,7 @@ function renderInput(options?: {
|
|
|
50
50
|
value?: unknown;
|
|
51
51
|
isDateLikeField?: boolean;
|
|
52
52
|
dateComponentProps?: DateVariableComponentProps;
|
|
53
|
+
allowDateVariables?: boolean;
|
|
53
54
|
}) {
|
|
54
55
|
const onChange = vi.fn();
|
|
55
56
|
render(
|
|
@@ -62,6 +63,7 @@ function renderInput(options?: {
|
|
|
62
63
|
runJSComponent={RunJSComponent}
|
|
63
64
|
isDateLikeField={options?.isDateLikeField ?? false}
|
|
64
65
|
dateComponentProps={options?.dateComponentProps ?? DEFAULT_DATE_VARIABLE_COMPONENT_PROPS}
|
|
66
|
+
allowDateVariables={options?.allowDateVariables}
|
|
65
67
|
/>,
|
|
66
68
|
);
|
|
67
69
|
return onChange;
|
|
@@ -110,6 +112,13 @@ describe('FieldValueVariableInput', () => {
|
|
|
110
112
|
expect(tree[4].name).toBe('currentUser');
|
|
111
113
|
});
|
|
112
114
|
|
|
115
|
+
it('omits the built-in Date variables when they are disabled', async () => {
|
|
116
|
+
renderInput({ isDateLikeField: true, allowDateVariables: false });
|
|
117
|
+
|
|
118
|
+
const tree = await resolveMetaTree();
|
|
119
|
+
expect(tree.map((node) => node.name)).toEqual(['constant', 'null', 'runjs', 'currentUser']);
|
|
120
|
+
});
|
|
121
|
+
|
|
113
122
|
it('does not allow Now for pure date fields', async () => {
|
|
114
123
|
const dateComponentProps: DateVariableComponentProps = {
|
|
115
124
|
...DEFAULT_DATE_VARIABLE_COMPONENT_PROPS,
|
|
@@ -139,9 +139,16 @@ export interface VariableFilterItemProps {
|
|
|
139
139
|
rightVariableConverters?: Pick<Converters, 'resolvePathFromValue' | 'resolveValueFromPath'>;
|
|
140
140
|
ignoreFieldNames?: string[];
|
|
141
141
|
maxAssociationFieldDepth?: number;
|
|
142
|
+
/**
|
|
143
|
+
* 右侧变量树的关联层级上限,默认与 `maxAssociationFieldDepth` 一致。
|
|
144
|
+
* 传 `null` 表示不限制:右侧是变量树而非集合字段树,其深度可能已由调用方约束
|
|
145
|
+
* (例如工作流的变量树由触发器的「预加载关系数据」决定),此时再按左侧的层级上限裁剪
|
|
146
|
+
* 会让已配置好的深层变量选不到。
|
|
147
|
+
*/
|
|
148
|
+
rightMaxAssociationFieldDepth?: number | null;
|
|
142
149
|
}
|
|
143
150
|
|
|
144
|
-
function limitMetaTreeIfNeeded(nodes: MetaTreeNode[], maxAssociationFieldDepth?: number) {
|
|
151
|
+
function limitMetaTreeIfNeeded(nodes: MetaTreeNode[], maxAssociationFieldDepth?: number | null) {
|
|
145
152
|
if (typeof maxAssociationFieldDepth !== 'number') {
|
|
146
153
|
return nodes;
|
|
147
154
|
}
|
|
@@ -367,6 +374,7 @@ export const VariableFilterItem: React.FC<VariableFilterItemProps> = observer(
|
|
|
367
374
|
rightVariableConverters,
|
|
368
375
|
ignoreFieldNames,
|
|
369
376
|
maxAssociationFieldDepth,
|
|
377
|
+
rightMaxAssociationFieldDepth = maxAssociationFieldDepth,
|
|
370
378
|
}) => {
|
|
371
379
|
// 使用 View 上下文,确保可访问 ctx.view 的异步子树
|
|
372
380
|
const ctx = useFlowViewContext();
|
|
@@ -685,10 +693,10 @@ export const VariableFilterItem: React.FC<VariableFilterItemProps> = observer(
|
|
|
685
693
|
{ title: t('Null'), name: 'null', type: 'object', paths: ['null'], render: NullComponent },
|
|
686
694
|
...nodes,
|
|
687
695
|
],
|
|
688
|
-
|
|
696
|
+
rightMaxAssociationFieldDepth,
|
|
689
697
|
);
|
|
690
698
|
};
|
|
691
|
-
}, [rightMetaTree, ctx, staticInputRenderer, NullComponent, t,
|
|
699
|
+
}, [rightMetaTree, ctx, staticInputRenderer, NullComponent, t, rightMaxAssociationFieldDepth]);
|
|
692
700
|
|
|
693
701
|
// 当启用右侧变量输入时,构造 VariableInput 的 converters:
|
|
694
702
|
// - 变量模式:返回 null 让 VariableInput 渲染 VariableTag
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import React from 'react';
|
|
11
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
12
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
13
|
+
import type { MetaTreeNode } from '@nocobase/flow-engine';
|
|
14
|
+
import { FlowEngine, FlowModel } from '@nocobase/flow-engine';
|
|
15
|
+
import { VariableFilterItem } from '../VariableFilterItem';
|
|
16
|
+
import { createMockFlowApp, TestCollectionFieldInterface } from '../../../__tests__/helpers/mockFlowApp';
|
|
17
|
+
|
|
18
|
+
const captured = vi.hoisted(() => ({ metaTrees: [] as any[] }));
|
|
19
|
+
|
|
20
|
+
vi.mock('@nocobase/flow-engine', async () => {
|
|
21
|
+
const actual = await vi.importActual<any>('@nocobase/flow-engine');
|
|
22
|
+
const MockVariableInput = ({ onChange, metaTree }: any) => {
|
|
23
|
+
if (metaTree) {
|
|
24
|
+
captured.metaTrees.push(metaTree);
|
|
25
|
+
}
|
|
26
|
+
return (
|
|
27
|
+
<button
|
|
28
|
+
type="button"
|
|
29
|
+
data-testid="variable-input"
|
|
30
|
+
onClick={() =>
|
|
31
|
+
onChange?.('title', {
|
|
32
|
+
interface: 'input',
|
|
33
|
+
uiSchema: { 'x-component': 'Input' },
|
|
34
|
+
paths: ['collection', 'title'],
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
>
|
|
38
|
+
mock-variable-input
|
|
39
|
+
</button>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
return { ...actual, VariableInput: MockVariableInput };
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// $context.data (not an association) -> category (m2o) -> book (m2o) -> createdBy (not counted) -> departments (m2m).
|
|
46
|
+
// Mirrors a workflow collection-event trigger that preloads `category.book.createdBy.departments`.
|
|
47
|
+
function buildRightMetaTree(): MetaTreeNode[] {
|
|
48
|
+
const departments: MetaTreeNode = {
|
|
49
|
+
name: 'departments',
|
|
50
|
+
title: 'Departments',
|
|
51
|
+
type: 'object',
|
|
52
|
+
interface: 'm2m',
|
|
53
|
+
paths: ['$context', 'data', 'category', 'book', 'createdBy', 'departments'],
|
|
54
|
+
};
|
|
55
|
+
const createdBy: MetaTreeNode = {
|
|
56
|
+
name: 'createdBy',
|
|
57
|
+
title: 'Created by',
|
|
58
|
+
type: 'object',
|
|
59
|
+
interface: 'createdBy',
|
|
60
|
+
paths: ['$context', 'data', 'category', 'book', 'createdBy'],
|
|
61
|
+
children: async () => [departments],
|
|
62
|
+
};
|
|
63
|
+
const book: MetaTreeNode = {
|
|
64
|
+
name: 'book',
|
|
65
|
+
title: 'Book',
|
|
66
|
+
type: 'object',
|
|
67
|
+
interface: 'obo',
|
|
68
|
+
paths: ['$context', 'data', 'category', 'book'],
|
|
69
|
+
children: async () => [createdBy],
|
|
70
|
+
};
|
|
71
|
+
const category: MetaTreeNode = {
|
|
72
|
+
name: 'category',
|
|
73
|
+
title: 'Category',
|
|
74
|
+
type: 'object',
|
|
75
|
+
interface: 'm2o',
|
|
76
|
+
paths: ['$context', 'data', 'category'],
|
|
77
|
+
children: async () => [book],
|
|
78
|
+
};
|
|
79
|
+
const data: MetaTreeNode = {
|
|
80
|
+
name: 'data',
|
|
81
|
+
title: 'Trigger data',
|
|
82
|
+
type: 'object',
|
|
83
|
+
paths: ['$context', 'data'],
|
|
84
|
+
children: async () => [category],
|
|
85
|
+
};
|
|
86
|
+
return [{ name: '$context', title: 'Trigger variables', type: '', paths: ['$context'], children: [data] }];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function resolveChildren(node: MetaTreeNode | undefined) {
|
|
90
|
+
const children = node?.children;
|
|
91
|
+
if (typeof children === 'function') {
|
|
92
|
+
return (await (children as () => Promise<MetaTreeNode[]>)()) ?? [];
|
|
93
|
+
}
|
|
94
|
+
return Array.isArray(children) ? children : [];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function walkToCreatedByChildren(metaTree: any) {
|
|
98
|
+
const roots: MetaTreeNode[] = typeof metaTree === 'function' ? await metaTree() : metaTree;
|
|
99
|
+
const context = roots.find((node) => node.name === '$context');
|
|
100
|
+
const data = (await resolveChildren(context)).find((node) => node.name === 'data');
|
|
101
|
+
const category = (await resolveChildren(data)).find((node) => node.name === 'category');
|
|
102
|
+
const book = (await resolveChildren(category)).find((node) => node.name === 'book');
|
|
103
|
+
const createdBy = (await resolveChildren(book)).find((node) => node.name === 'createdBy');
|
|
104
|
+
return (await resolveChildren(createdBy)).map((node) => node.name);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function createModel() {
|
|
108
|
+
const engine = new FlowEngine();
|
|
109
|
+
const model = new FlowModel({ uid: 'm-variable-filter-right', flowEngine: engine });
|
|
110
|
+
const app = createMockFlowApp();
|
|
111
|
+
model.context.defineProperty('app', { value: app });
|
|
112
|
+
|
|
113
|
+
class InputInterface extends TestCollectionFieldInterface {
|
|
114
|
+
name = 'input';
|
|
115
|
+
group = 'basic';
|
|
116
|
+
filterable = { operators: [{ value: '$eq', label: 'Equals' }] };
|
|
117
|
+
}
|
|
118
|
+
app.addFieldInterfaces([InputInterface]);
|
|
119
|
+
|
|
120
|
+
const ds = engine.dataSourceManager.getDataSource('main');
|
|
121
|
+
ds.addCollection({
|
|
122
|
+
name: 'posts',
|
|
123
|
+
fields: [{ name: 'title', type: 'string', interface: 'input', uiSchema: { 'x-component': 'Input' } }],
|
|
124
|
+
});
|
|
125
|
+
model.context.defineProperty('collection', { get: () => ds.getCollection('posts') });
|
|
126
|
+
|
|
127
|
+
return model as any;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function renderItem(props: Record<string, unknown>) {
|
|
131
|
+
const value = { path: 'title', operator: '$eq', value: '' } as any;
|
|
132
|
+
const model = createModel();
|
|
133
|
+
render(
|
|
134
|
+
<VariableFilterItem value={value} model={model} rightAsVariable rightMetaTree={buildRightMetaTree()} {...props} />,
|
|
135
|
+
);
|
|
136
|
+
fireEvent.click(screen.getAllByTestId('variable-input')[0]);
|
|
137
|
+
return captured.metaTrees.at(-1);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
describe('VariableFilterItem right-side association depth', () => {
|
|
141
|
+
beforeEach(() => {
|
|
142
|
+
document.body.innerHTML = '';
|
|
143
|
+
captured.metaTrees = [];
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('applies maxAssociationFieldDepth to the right tree by default', async () => {
|
|
147
|
+
const metaTree = renderItem({ maxAssociationFieldDepth: 2 });
|
|
148
|
+
// `departments` is the third association down the path, so the default cap removes it.
|
|
149
|
+
expect(await walkToCreatedByChildren(metaTree)).toEqual([]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// The right side is a variable tree whose depth is decided by its provider (in workflow, by the trigger's
|
|
153
|
+
// "Preload associations" config). Capping it by the left-side field-picker limit hid preloaded variables.
|
|
154
|
+
it('leaves the right tree untouched when rightMaxAssociationFieldDepth is null', async () => {
|
|
155
|
+
const metaTree = renderItem({ maxAssociationFieldDepth: 2, rightMaxAssociationFieldDepth: null });
|
|
156
|
+
expect(await walkToCreatedByChildren(metaTree)).toEqual(['departments']);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -922,7 +922,6 @@ export class GridModel<T extends { subModels: { items: FlowModel[] } } = Default
|
|
|
922
922
|
const baseLayout = this.context.isMobileLayout
|
|
923
923
|
? normalizeGridLayout({
|
|
924
924
|
rows: transformRowsToSingleColumn(projectLayoutToLegacyRows(rawLayout).rows),
|
|
925
|
-
itemUids: this.getItemUids(),
|
|
926
925
|
})
|
|
927
926
|
: rawLayout;
|
|
928
927
|
const baseProjection = projectLayoutToLegacyRows(baseLayout);
|
|
@@ -115,7 +115,28 @@ export class AssignFormGridModel extends FormGridModel {
|
|
|
115
115
|
(existing as any).assignValue = value;
|
|
116
116
|
return;
|
|
117
117
|
}
|
|
118
|
-
|
|
118
|
+
if (!collection) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const field = (collection.getFields?.() || []).find((f: any) => f.name === fieldName);
|
|
122
|
+
|
|
123
|
+
if (!field) {
|
|
124
|
+
const created = this.addSubModel('items', {
|
|
125
|
+
use: 'AssignFormItemModel',
|
|
126
|
+
stepParams: {
|
|
127
|
+
fieldSettings: {
|
|
128
|
+
init: {
|
|
129
|
+
dataSourceKey: collection?.dataSourceKey,
|
|
130
|
+
collectionName: collection?.name,
|
|
131
|
+
fieldPath: fieldName,
|
|
132
|
+
},
|
|
133
|
+
assignValue: { value },
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
created['assignValue'] = value;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
119
140
|
|
|
120
141
|
const binding = EditableItemModel.getDefaultBindingByField(this.context, field);
|
|
121
142
|
if (!binding) {
|
|
@@ -10,7 +10,14 @@
|
|
|
10
10
|
import React from 'react';
|
|
11
11
|
import { Input } from 'antd';
|
|
12
12
|
import { define, observable } from '@formily/reactive';
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
EditableItemModel,
|
|
15
|
+
FieldDeletePlaceholder,
|
|
16
|
+
FlowModelRenderer,
|
|
17
|
+
FormItem,
|
|
18
|
+
jioToJoiSchema,
|
|
19
|
+
tExpr,
|
|
20
|
+
} from '@nocobase/flow-engine';
|
|
14
21
|
// 无需类型导入(避免未使用的类型)
|
|
15
22
|
import { FormItemModel } from '../form/FormItemModel';
|
|
16
23
|
import { EditFormModel } from '../form/EditFormModel';
|
|
@@ -160,11 +167,15 @@ export class AssignFormItemModel extends FormItemModel {
|
|
|
160
167
|
|
|
161
168
|
getAssignedEntry(): [string, any] | null {
|
|
162
169
|
const name = this.fieldPath;
|
|
163
|
-
if (!name) return null;
|
|
170
|
+
if (!name || !this.collectionField) return null;
|
|
164
171
|
return [name, this.assignValue];
|
|
165
172
|
}
|
|
166
173
|
|
|
167
174
|
render() {
|
|
175
|
+
if (!this.collectionField) {
|
|
176
|
+
return <FieldDeletePlaceholder />;
|
|
177
|
+
}
|
|
178
|
+
|
|
168
179
|
// 与 FormItemModel.render 结构保持一致,仅替换内部渲染为 VariableInput + 常量编辑器
|
|
169
180
|
const ctx: any = this.context;
|
|
170
181
|
const collection = ctx.collection;
|
|
@@ -392,7 +403,7 @@ AssignFormItemModel.registerFlow({
|
|
|
392
403
|
},
|
|
393
404
|
defaultParams: (ctx) => {
|
|
394
405
|
return {
|
|
395
|
-
label: (ctx.model as any).collectionField.
|
|
406
|
+
label: (ctx.model as any).collectionField?.title || (ctx.model as any).fieldPath,
|
|
396
407
|
};
|
|
397
408
|
},
|
|
398
409
|
handler(ctx, params) {
|