@nocobase/client-v2 2.1.0-beta.30 → 2.1.0-beta.32
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/BaseApplication.d.ts +1 -0
- package/es/PluginManager.d.ts +1 -0
- package/es/components/form/DrawerFormLayout.d.ts +49 -0
- package/es/components/form/EnvVariableInput.d.ts +42 -0
- package/es/components/form/FileSizeInput.d.ts +27 -0
- package/es/components/form/createFormRegistry.d.ts +33 -0
- package/es/components/form/index.d.ts +13 -0
- package/es/components/index.d.ts +1 -1
- package/es/flow/components/FieldAssignRulesEditor.d.ts +1 -0
- package/es/flow/internal/utils/enumOptionsUtils.d.ts +5 -0
- package/es/flow/models/actions/AssociationActionUtils.d.ts +5 -0
- package/es/flow/models/blocks/filter-form/FilterFormBlockModel.d.ts +4 -0
- package/es/flow/models/blocks/filter-manager/FilterManager.d.ts +5 -1
- package/es/flow/models/blocks/table/TableSelectModel.d.ts +8 -0
- package/es/flow/models/fields/DisplayTitleFieldModel.d.ts +1 -1
- package/es/flow/models/utils/displayValueUtils.d.ts +12 -0
- package/es/flow-compat/passwordUtils.d.ts +1 -1
- package/es/index.mjs +87 -76
- package/es/utils/remotePlugins.d.ts +0 -4
- package/lib/index.js +97 -86
- package/package.json +6 -5
- package/src/BaseApplication.tsx +14 -8
- package/src/PluginManager.ts +1 -0
- package/src/__tests__/app.test.tsx +28 -1
- package/src/__tests__/remotePlugins.test.ts +29 -18
- package/src/components/form/DrawerFormLayout.tsx +103 -0
- package/src/components/form/EnvVariableInput.tsx +126 -0
- package/src/components/form/FileSizeInput.tsx +105 -0
- package/src/components/form/createFormRegistry.ts +60 -0
- package/src/components/form/index.tsx +14 -0
- package/src/components/index.ts +1 -1
- package/src/flow/actions/__tests__/dataScopeFilter.test.ts +92 -13
- package/src/flow/actions/__tests__/linkageRules.subFormSetFieldProps.test.ts +476 -1
- package/src/flow/actions/linkageRules.tsx +240 -258
- package/src/flow/actions/setTargetDataScope.tsx +32 -3
- package/src/flow/components/FieldAssignRulesEditor.tsx +2 -0
- package/src/flow/components/__tests__/FieldAssignRulesEditor.test.tsx +81 -4
- package/src/flow/components/filter/LinkageFilterItem.tsx +9 -2
- package/src/flow/components/filter/VariableFilterItem.tsx +2 -6
- package/src/flow/components/filter/__tests__/LinkageFilterItem.test.tsx +71 -0
- package/src/flow/components/filter/__tests__/VariableFilterItem.test.tsx +48 -0
- package/src/flow/internal/utils/__tests__/enumOptionsUtils.test.ts +10 -1
- package/src/flow/internal/utils/enumOptionsUtils.ts +29 -0
- package/src/flow/models/actions/AssociateActionModel.tsx +2 -2
- package/src/flow/models/actions/AssociationActionUtils.ts +14 -0
- package/src/flow/models/actions/__tests__/AssociationActionModel.test.ts +63 -0
- package/src/flow/models/base/CollectionBlockModel.tsx +7 -0
- package/src/flow/models/blocks/filter-form/FilterFormBlockModel.tsx +33 -9
- package/src/flow/models/blocks/filter-form/FilterFormItemModel.tsx +53 -13
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormItemModel.getFilterValue.test.ts +63 -3
- package/src/flow/models/blocks/filter-form/__tests__/defaultValues.wiring.test.ts +33 -1
- package/src/flow/models/blocks/filter-manager/FilterManager.ts +66 -2
- package/src/flow/models/blocks/filter-manager/__tests__/FilterManager.test.ts +270 -0
- package/src/flow/models/blocks/form/FormBlockModel.tsx +8 -5
- package/src/flow/models/blocks/form/__tests__/FormBlockModel.test.tsx +30 -0
- package/src/flow/models/blocks/form/value-runtime/rules.ts +6 -1
- package/src/flow/models/blocks/form/value-runtime/runtime.ts +6 -1
- package/src/flow/models/blocks/table/TableBlockModel.tsx +11 -6
- package/src/flow/models/blocks/table/TableColumnModel.tsx +3 -0
- package/src/flow/models/blocks/table/TableSelectModel.tsx +36 -26
- package/src/flow/models/blocks/table/__tests__/TableBlockModel.rowClick.test.ts +69 -0
- package/src/flow/models/blocks/table/__tests__/TableColumnModel.test.tsx +96 -1
- package/src/flow/models/blocks/table/__tests__/TableSelectModel.test.ts +41 -0
- package/src/flow/models/fields/ClickableFieldModel.tsx +9 -4
- package/src/flow/models/fields/DisplayTitleFieldModel.tsx +12 -4
- package/src/flow/models/fields/SelectFieldModel.tsx +31 -1
- package/src/flow/models/fields/__tests__/ClickableFieldModel.test.ts +23 -0
- package/src/flow/models/fields/mobile-components/MobileSelect.tsx +2 -1
- package/src/flow/models/fields/mobile-components/__tests__/MobileSelect.test.tsx +7 -0
- package/src/flow/models/utils/displayValueUtils.ts +57 -0
- package/src/utils/globalDeps.ts +2 -0
- package/src/utils/remotePlugins.ts +7 -27
|
@@ -14,6 +14,11 @@ import { FlowEngine, FlowModel, SingleRecordResource } from '@nocobase/flow-engi
|
|
|
14
14
|
// 直接从 models 聚合导入,避免局部文件相互引用顺序导致的循环依赖
|
|
15
15
|
import { FormBlockContent, FormBlockModel, FormComponent } from '../../../..';
|
|
16
16
|
import { Form } from 'antd';
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
vi.useRealTimers();
|
|
20
|
+
});
|
|
21
|
+
|
|
17
22
|
// -----------------------------
|
|
18
23
|
// Helpers
|
|
19
24
|
// -----------------------------
|
|
@@ -319,6 +324,31 @@ describe('FormBlockModel (form/formValues injection & server resolve anchors)',
|
|
|
319
324
|
expect(flows.has('eventSettings')).toBe(true);
|
|
320
325
|
});
|
|
321
326
|
|
|
327
|
+
it('replays linkage rules after the settings rerender tick', async () => {
|
|
328
|
+
vi.useFakeTimers();
|
|
329
|
+
const engine = new FlowEngine();
|
|
330
|
+
const TestFormModel = await createTestFormModelSubclass();
|
|
331
|
+
const model = new TestFormModel({ uid: 'form-linkage-save', flowEngine: engine } as any);
|
|
332
|
+
const applyFlow = vi.spyOn(model, 'applyFlow').mockResolvedValue(undefined as any);
|
|
333
|
+
const flow = model.getFlow('eventSettings') as any;
|
|
334
|
+
const afterParamsSave = flow?.steps?.linkageRules?.afterParamsSave;
|
|
335
|
+
const ctx: any = {
|
|
336
|
+
model,
|
|
337
|
+
form: {
|
|
338
|
+
getFieldsValue: vi.fn(() => ({ status: 'draft' })),
|
|
339
|
+
},
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
afterParamsSave(ctx);
|
|
343
|
+
|
|
344
|
+
expect(applyFlow).not.toHaveBeenCalled();
|
|
345
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
346
|
+
expect(applyFlow).toHaveBeenCalledWith('eventSettings', {
|
|
347
|
+
changedValues: {},
|
|
348
|
+
allValues: { status: 'draft' },
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
322
352
|
it('delegates layout/assignRules/linkageRules stepParams to grid model', async () => {
|
|
323
353
|
const model = await setupFormModel();
|
|
324
354
|
const engine = model.flowEngine as FlowEngine;
|
|
@@ -1508,16 +1508,21 @@ export class RuleEngine {
|
|
|
1508
1508
|
private getRowTargetKey(baseCtx: any, rowPath: NamePath): string | string[] {
|
|
1509
1509
|
let collection = this.getRootCollection() || this.getCollectionFromContext(baseCtx);
|
|
1510
1510
|
let field: any;
|
|
1511
|
+
let lastAssociationField: any;
|
|
1511
1512
|
for (const seg of rowPath) {
|
|
1512
1513
|
if (typeof seg === 'number') continue;
|
|
1513
1514
|
if (typeof seg !== 'string' || !seg || !collection?.getField) break;
|
|
1514
1515
|
|
|
1515
1516
|
field = collection?.getField?.(seg);
|
|
1516
1517
|
if (!field?.isAssociationField?.()) break;
|
|
1518
|
+
lastAssociationField = field;
|
|
1517
1519
|
collection = field?.targetCollection;
|
|
1518
1520
|
}
|
|
1519
1521
|
|
|
1520
|
-
const raw =
|
|
1522
|
+
const raw =
|
|
1523
|
+
lastAssociationField?.targetCollection?.filterTargetKey ??
|
|
1524
|
+
lastAssociationField?.targetCollection?.filterByTk ??
|
|
1525
|
+
lastAssociationField?.targetKey;
|
|
1521
1526
|
if (Array.isArray(raw)) {
|
|
1522
1527
|
const keys = raw.filter((key): key is string => typeof key === 'string' && !!key);
|
|
1523
1528
|
return keys.length ? keys : 'id';
|
|
@@ -394,16 +394,21 @@ export class FormValueRuntime {
|
|
|
394
394
|
private getArrayItemTargetKey(arrayPath?: NamePath): string | string[] {
|
|
395
395
|
let collection = this.model?.context?.collection;
|
|
396
396
|
let field: any;
|
|
397
|
+
let lastAssociationField: any;
|
|
397
398
|
for (const seg of arrayPath || []) {
|
|
398
399
|
if (typeof seg === 'number') continue;
|
|
399
400
|
if (typeof seg !== 'string' || !collection?.getField) break;
|
|
400
401
|
|
|
401
402
|
field = collection?.getField?.(seg);
|
|
402
403
|
if (!field?.isAssociationField?.()) break;
|
|
404
|
+
lastAssociationField = field;
|
|
403
405
|
collection = field?.targetCollection;
|
|
404
406
|
}
|
|
405
407
|
|
|
406
|
-
const raw =
|
|
408
|
+
const raw =
|
|
409
|
+
lastAssociationField?.targetCollection?.filterTargetKey ??
|
|
410
|
+
lastAssociationField?.targetCollection?.filterByTk ??
|
|
411
|
+
lastAssociationField?.targetKey;
|
|
407
412
|
if (Array.isArray(raw)) {
|
|
408
413
|
const keys = raw.filter((key): key is string => typeof key === 'string' && !!key);
|
|
409
414
|
return keys.length ? keys : 'id';
|
|
@@ -932,12 +932,12 @@ const HighPerformanceTable = React.memo(
|
|
|
932
932
|
|
|
933
933
|
return {
|
|
934
934
|
onClick: async (event) => {
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
935
|
+
const selected = highlightedRowKey !== rowKey;
|
|
936
|
+
defineClickedRowRecordVariable(model, selected ? record : null);
|
|
937
|
+
try {
|
|
938
|
+
await model.dispatchEvent('rowClick', { record, rowIndex, event, selected });
|
|
939
|
+
} finally {
|
|
938
940
|
removeClickedRowRecordVariable(model);
|
|
939
|
-
} else {
|
|
940
|
-
await model.dispatchEvent('rowClick', { record, rowIndex, event });
|
|
941
941
|
}
|
|
942
942
|
},
|
|
943
943
|
rowIndex,
|
|
@@ -1072,7 +1072,12 @@ TableBlockModel.registerEvents({
|
|
|
1072
1072
|
|
|
1073
1073
|
const model = ctx.model as TableBlockModel;
|
|
1074
1074
|
const rowKey = getRowKey(ctx.inputArgs.record, model.collection.filterTargetKey);
|
|
1075
|
-
|
|
1075
|
+
const selected = ctx.inputArgs.selected;
|
|
1076
|
+
if (selected === true) {
|
|
1077
|
+
model.highlightRow(ctx.inputArgs.record);
|
|
1078
|
+
} else if (selected === false) {
|
|
1079
|
+
model.clearHighlight();
|
|
1080
|
+
} else if (model.props.highlightedRowKey !== rowKey) {
|
|
1076
1081
|
model.highlightRow(ctx.inputArgs.record);
|
|
1077
1082
|
} else {
|
|
1078
1083
|
model.clearHighlight();
|
|
@@ -535,6 +535,9 @@ TableColumnModel.registerFlow({
|
|
|
535
535
|
fieldModel.setStepParams('fieldSettings', 'init', fieldSettingsInit);
|
|
536
536
|
await fieldModel.dispatchEvent('beforeRender', undefined, { useCache: false });
|
|
537
537
|
}
|
|
538
|
+
if (targetUse) {
|
|
539
|
+
ctx.model.setStepParams('tableColumnSettings', 'model', { use: targetUse });
|
|
540
|
+
}
|
|
538
541
|
ctx.model.setProps(targetCollectionField.getComponentProps());
|
|
539
542
|
},
|
|
540
543
|
defaultParams: (ctx: any) => {
|
|
@@ -12,45 +12,55 @@ import { castArray } from 'lodash';
|
|
|
12
12
|
import { BlockSceneEnum } from '../../base/BlockModel';
|
|
13
13
|
import { TableBlockModel } from './TableBlockModel';
|
|
14
14
|
|
|
15
|
+
export function getAssociationSelectForeignKeyFilter(collectionField: any) {
|
|
16
|
+
const isOToAny = ['oho', 'o2m'].includes(collectionField?.interface);
|
|
17
|
+
const foreignKey = collectionField?.foreignKey;
|
|
18
|
+
if (!isOToAny || !foreignKey) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
[foreignKey]: {
|
|
23
|
+
$is: null,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getAssociationSelectAssociatedRecordsFilter(collection: any, associatedRecords: any[] = []) {
|
|
29
|
+
const targetKey = collection?.filterTargetKey || 'id';
|
|
30
|
+
const filterKeys = associatedRecords
|
|
31
|
+
.map((record) => record?.[targetKey])
|
|
32
|
+
.filter((value) => value !== undefined && value !== null && value !== '');
|
|
33
|
+
|
|
34
|
+
if (!filterKeys.length) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
[`${targetKey}.$ne`]: filterKeys,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
15
43
|
export class TableSelectModel extends TableBlockModel {
|
|
16
44
|
static scene = BlockSceneEnum.select;
|
|
17
45
|
rowSelectionProps: any = observable.deep({});
|
|
18
46
|
onInit(options: any) {
|
|
19
47
|
super.onInit(options);
|
|
20
48
|
const collectionField = this.context.view.inputArgs.collectionField || {};
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
if (isOToAny) {
|
|
24
|
-
const foreignKey = collectionField.foreignKey;
|
|
49
|
+
const foreignKeyFilter = getAssociationSelectForeignKeyFilter(collectionField);
|
|
50
|
+
if (foreignKeyFilter) {
|
|
25
51
|
const filterGroupKey = `${this.uid}-${collectionField.name}`;
|
|
26
|
-
|
|
27
|
-
if (sourceId != null) {
|
|
28
|
-
this.resource.addFilterGroup(filterGroupKey, {
|
|
29
|
-
$or: [{ [foreignKey]: { $is: null } }, { [foreignKey]: { $eq: sourceId } }],
|
|
30
|
-
});
|
|
31
|
-
} else {
|
|
32
|
-
this.resource.addFilterGroup(filterGroupKey, {
|
|
33
|
-
[foreignKey]: {
|
|
34
|
-
$is: null,
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
}
|
|
52
|
+
this.resource.addFilterGroup(filterGroupKey, foreignKeyFilter);
|
|
39
53
|
}
|
|
40
54
|
|
|
41
55
|
Object.assign(this.rowSelectionProps, this.context.view.inputArgs.rowSelectionProps || {});
|
|
42
56
|
|
|
43
57
|
const getSelectedRows = this.rowSelectionProps?.defaultSelectedRows;
|
|
44
58
|
const selectData = typeof getSelectedRows === 'function' ? getSelectedRows() : getSelectedRows;
|
|
45
|
-
const data = (selectData
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
.filter(Boolean);
|
|
51
|
-
this.resource.addFilterGroup(`${this.uid}-select`, {
|
|
52
|
-
[`${this.collection.filterTargetKey}.$ne`]: filterKeys,
|
|
53
|
-
});
|
|
59
|
+
const data = [...castArray(selectData || []), ...castArray(this.context.view.inputArgs.associatedRecords || [])];
|
|
60
|
+
const associatedRecordsFilter = getAssociationSelectAssociatedRecordsFilter(this.collection, data);
|
|
61
|
+
if (associatedRecordsFilter) {
|
|
62
|
+
this.resource.addFilterGroup(`${this.uid}-select`, associatedRecordsFilter);
|
|
63
|
+
}
|
|
54
64
|
}
|
|
55
65
|
}
|
|
56
66
|
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { FlowEngine } from '@nocobase/flow-engine';
|
|
11
|
+
import { describe, expect, it } from 'vitest';
|
|
12
|
+
import '@nocobase/client';
|
|
13
|
+
import { TableBlockModel } from '../TableBlockModel';
|
|
14
|
+
|
|
15
|
+
function createTableModel() {
|
|
16
|
+
const engine = new FlowEngine();
|
|
17
|
+
engine.registerModels({ TableBlockModel });
|
|
18
|
+
|
|
19
|
+
const ds = engine.dataSourceManager.getDataSource('main');
|
|
20
|
+
ds.addCollection({
|
|
21
|
+
name: 'posts',
|
|
22
|
+
filterTargetKey: 'id',
|
|
23
|
+
fields: [
|
|
24
|
+
{ name: 'id', type: 'integer', interface: 'number' },
|
|
25
|
+
{ name: 'title', type: 'string', interface: 'input' },
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return engine.createModel<TableBlockModel>({
|
|
30
|
+
uid: 'posts-table',
|
|
31
|
+
use: 'TableBlockModel',
|
|
32
|
+
stepParams: {
|
|
33
|
+
resourceSettings: {
|
|
34
|
+
init: {
|
|
35
|
+
dataSourceKey: 'main',
|
|
36
|
+
collectionName: 'posts',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('TableBlockModel rowClick event', () => {
|
|
44
|
+
it('highlights the clicked row when selected is true', async () => {
|
|
45
|
+
const model = createTableModel();
|
|
46
|
+
const record = { id: 1, title: 'first post' };
|
|
47
|
+
const rowClick = model.getEvent('rowClick');
|
|
48
|
+
|
|
49
|
+
await rowClick?.handler({ model, inputArgs: { record, selected: true } } as any, {
|
|
50
|
+
condition: { logic: '$and', items: [] },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(model.props.highlightedRowKey).toBe(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('clears the highlighted row when selected is false', async () => {
|
|
57
|
+
const model = createTableModel();
|
|
58
|
+
const record = { id: 1, title: 'first post' };
|
|
59
|
+
const rowClick = model.getEvent('rowClick');
|
|
60
|
+
|
|
61
|
+
model.highlightRow(record);
|
|
62
|
+
|
|
63
|
+
await rowClick?.handler({ model, inputArgs: { record, selected: false } } as any, {
|
|
64
|
+
condition: { logic: '$and', items: [] },
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
expect(model.props.highlightedRowKey).toBeNull();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { FlowEngine } from '@nocobase/flow-engine';
|
|
11
|
-
import { describe, expect, it } from 'vitest';
|
|
11
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
12
12
|
import { TableColumnModel } from '../TableColumnModel';
|
|
13
13
|
|
|
14
14
|
describe('TableColumnModel sorter settings', () => {
|
|
@@ -69,4 +69,99 @@ describe('TableColumnModel sorter settings', () => {
|
|
|
69
69
|
|
|
70
70
|
expect(hidden).toBe(true);
|
|
71
71
|
});
|
|
72
|
+
|
|
73
|
+
it('updates field component setting when association title field changes', async () => {
|
|
74
|
+
const engine = new FlowEngine();
|
|
75
|
+
const model = new TableColumnModel({ uid: 'table-column-title-field-component', flowEngine: engine } as any);
|
|
76
|
+
const titleFieldStep = model.getFlow('tableColumnSettings')?.steps?.fieldNames as any;
|
|
77
|
+
const setStepParams = vi.fn();
|
|
78
|
+
const setProps = vi.fn();
|
|
79
|
+
const dispatchEvent = vi.fn();
|
|
80
|
+
const targetCollectionField = {
|
|
81
|
+
getComponentProps: () => ({}),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
await titleFieldStep.beforeParamsSave(
|
|
85
|
+
{
|
|
86
|
+
collectionField: {
|
|
87
|
+
isAssociationField: () => true,
|
|
88
|
+
targetCollection: {
|
|
89
|
+
name: 'departments',
|
|
90
|
+
getField: () => targetCollectionField,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
model: {
|
|
94
|
+
collectionField: {
|
|
95
|
+
dataSourceKey: 'main',
|
|
96
|
+
},
|
|
97
|
+
constructor: {
|
|
98
|
+
getDefaultBindingByField: () => ({
|
|
99
|
+
modelName: 'DisplayTextFieldModel',
|
|
100
|
+
}),
|
|
101
|
+
},
|
|
102
|
+
subModels: {
|
|
103
|
+
field: {
|
|
104
|
+
use: 'DisplayTextFieldModel',
|
|
105
|
+
setStepParams,
|
|
106
|
+
dispatchEvent,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
setStepParams,
|
|
110
|
+
setProps,
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
{ label: 'code' },
|
|
114
|
+
{ label: 'name' },
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(setStepParams).toHaveBeenCalledWith('tableColumnSettings', 'model', { use: 'DisplayTextFieldModel' });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('does not update field component setting when title field refresh fails', async () => {
|
|
121
|
+
const engine = new FlowEngine();
|
|
122
|
+
const model = new TableColumnModel({ uid: 'table-column-title-field-component-failed', flowEngine: engine } as any);
|
|
123
|
+
const titleFieldStep = model.getFlow('tableColumnSettings')?.steps?.fieldNames as any;
|
|
124
|
+
const setStepParams = vi.fn();
|
|
125
|
+
const setProps = vi.fn();
|
|
126
|
+
const targetCollectionField = {
|
|
127
|
+
getComponentProps: () => ({}),
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
await expect(
|
|
131
|
+
titleFieldStep.beforeParamsSave(
|
|
132
|
+
{
|
|
133
|
+
collectionField: {
|
|
134
|
+
isAssociationField: () => true,
|
|
135
|
+
targetCollection: {
|
|
136
|
+
name: 'departments',
|
|
137
|
+
getField: () => targetCollectionField,
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
model: {
|
|
141
|
+
collectionField: {
|
|
142
|
+
dataSourceKey: 'main',
|
|
143
|
+
},
|
|
144
|
+
constructor: {
|
|
145
|
+
getDefaultBindingByField: () => ({
|
|
146
|
+
modelName: 'DisplayTextFieldModel',
|
|
147
|
+
}),
|
|
148
|
+
},
|
|
149
|
+
subModels: {
|
|
150
|
+
field: {
|
|
151
|
+
use: 'DisplayTextFieldModel',
|
|
152
|
+
setStepParams: vi.fn(),
|
|
153
|
+
dispatchEvent: vi.fn().mockRejectedValue(new Error('beforeRender failed')),
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
setStepParams,
|
|
157
|
+
setProps,
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
{ label: 'code' },
|
|
161
|
+
{ label: 'name' },
|
|
162
|
+
),
|
|
163
|
+
).rejects.toThrow('beforeRender failed');
|
|
164
|
+
|
|
165
|
+
expect(setStepParams).not.toHaveBeenCalledWith('tableColumnSettings', 'model', expect.anything());
|
|
166
|
+
});
|
|
72
167
|
});
|
|
@@ -0,0 +1,41 @@
|
|
|
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 } from 'vitest';
|
|
11
|
+
import '@nocobase/client';
|
|
12
|
+
import { getAssociationSelectAssociatedRecordsFilter, getAssociationSelectForeignKeyFilter } from '../TableSelectModel';
|
|
13
|
+
|
|
14
|
+
describe('TableSelectModel', () => {
|
|
15
|
+
it('filters out already associated o2m records in association select table', () => {
|
|
16
|
+
expect(
|
|
17
|
+
getAssociationSelectForeignKeyFilter({
|
|
18
|
+
interface: 'o2m',
|
|
19
|
+
name: 'orders',
|
|
20
|
+
foreignKey: 'f_y2quq75zibi',
|
|
21
|
+
}),
|
|
22
|
+
).toEqual({
|
|
23
|
+
f_y2quq75zibi: {
|
|
24
|
+
$is: null,
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('filters out already associated m2m records in association select table', () => {
|
|
30
|
+
expect(
|
|
31
|
+
getAssociationSelectAssociatedRecordsFilter(
|
|
32
|
+
{
|
|
33
|
+
filterTargetKey: 'id',
|
|
34
|
+
},
|
|
35
|
+
[{ id: 11 }, { id: 12 }],
|
|
36
|
+
),
|
|
37
|
+
).toEqual({
|
|
38
|
+
'id.$ne': [11, 12],
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -14,6 +14,7 @@ import React from 'react';
|
|
|
14
14
|
import { EllipsisWithTooltip } from '../../components/EllipsisWithTooltip';
|
|
15
15
|
import { openViewFlow } from '../../flows/openViewFlow';
|
|
16
16
|
import { FieldModel } from '../base/FieldModel';
|
|
17
|
+
import { hasDisplayValue, normalizeDisplayValue } from '../utils/displayValueUtils';
|
|
17
18
|
|
|
18
19
|
export function transformNestedData(inputData) {
|
|
19
20
|
const resultArray = [];
|
|
@@ -150,11 +151,15 @@ export class ClickableFieldModel extends FieldModel {
|
|
|
150
151
|
|
|
151
152
|
renderInDisplayStyle(value, record?, isToMany?, wrap?) {
|
|
152
153
|
const { clickToOpen = false, displayStyle, titleField, overflowMode, disabled, ...restProps } = this.props;
|
|
153
|
-
|
|
154
|
+
const titleCollectionField = titleField
|
|
155
|
+
? this.context.collectionField?.targetCollection?.getField?.(titleField) || this.context.collectionField
|
|
156
|
+
: this.context.collectionField;
|
|
157
|
+
const displayValue = normalizeDisplayValue(value, { collectionField: titleCollectionField });
|
|
158
|
+
if (!hasDisplayValue(displayValue) && value && typeof value === 'object' && restProps.target) {
|
|
154
159
|
return;
|
|
155
160
|
}
|
|
156
|
-
const result = this.renderComponent(
|
|
157
|
-
const display = record ? (
|
|
161
|
+
const result = this.renderComponent(displayValue, wrap);
|
|
162
|
+
const display = record ? (hasDisplayValue(displayValue) ? result : 'N/A') : result;
|
|
158
163
|
const isTag = displayStyle === 'tag';
|
|
159
164
|
const handleClick = (e) => {
|
|
160
165
|
clickToOpen && this.onClick(e, record);
|
|
@@ -170,7 +175,7 @@ export class ClickableFieldModel extends FieldModel {
|
|
|
170
175
|
|
|
171
176
|
if (isTag) {
|
|
172
177
|
return (
|
|
173
|
-
|
|
178
|
+
hasDisplayValue(displayValue) && (
|
|
174
179
|
<Tag {...restProps} style={commonStyle} onClick={handleClick} className={restProps.className}>
|
|
175
180
|
{display}
|
|
176
181
|
</Tag>
|
|
@@ -12,7 +12,8 @@ import { Typography } from 'antd';
|
|
|
12
12
|
import { castArray } from 'lodash';
|
|
13
13
|
import { css } from '@emotion/css';
|
|
14
14
|
import React from 'react';
|
|
15
|
-
import { FieldModel } from '../base
|
|
15
|
+
import { FieldModel } from '../base';
|
|
16
|
+
import { hasDisplayValue, normalizeDisplayValue } from '../utils/displayValueUtils';
|
|
16
17
|
|
|
17
18
|
export class DisplayTitleFieldModel extends FieldModel {
|
|
18
19
|
get collectionField(): CollectionField {
|
|
@@ -55,13 +56,20 @@ export class DisplayTitleFieldModel extends FieldModel {
|
|
|
55
56
|
};
|
|
56
57
|
if (titleField) {
|
|
57
58
|
const result = castArray(value).flatMap((v, idx) => {
|
|
58
|
-
const
|
|
59
|
-
|
|
59
|
+
const titleCollectionField =
|
|
60
|
+
this.context.collectionField?.targetCollection?.getField?.(titleField) || this.context.collectionField;
|
|
61
|
+
const displayValue = normalizeDisplayValue(v?.[titleField], { collectionField: titleCollectionField });
|
|
62
|
+
const result = this.renderComponent(displayValue);
|
|
63
|
+
const node = hasDisplayValue(displayValue) ? result : 'N/A';
|
|
60
64
|
return idx === 0 ? [node] : [<span key={`sep-${idx}`}>, </span>, node];
|
|
61
65
|
});
|
|
62
66
|
return <Typography.Text {...typographyProps}>{result}</Typography.Text>;
|
|
63
67
|
} else {
|
|
64
|
-
const textContent =
|
|
68
|
+
const textContent = (
|
|
69
|
+
<Typography.Text {...typographyProps}>
|
|
70
|
+
{this.renderComponent(normalizeDisplayValue(value, { collectionField: this.context.collectionField }))}
|
|
71
|
+
</Typography.Text>
|
|
72
|
+
);
|
|
65
73
|
return textContent;
|
|
66
74
|
}
|
|
67
75
|
}
|
|
@@ -12,25 +12,55 @@ import { Select, Tag, Tooltip } from 'antd';
|
|
|
12
12
|
import React from 'react';
|
|
13
13
|
import { FieldModel } from '../base/FieldModel';
|
|
14
14
|
import { MobileSelect } from './mobile-components/MobileSelect';
|
|
15
|
+
import { enumToOptions, getSelectedEnumLabels } from '../../internal/utils/enumOptionsUtils';
|
|
16
|
+
|
|
17
|
+
const getOriginalEnumOptions = (model: SelectFieldModel) => {
|
|
18
|
+
const fromEnum = enumToOptions(model.context.collectionField?.uiSchema?.enum, (text) => text) || [];
|
|
19
|
+
if (fromEnum.length > 0) {
|
|
20
|
+
return fromEnum.map((option) => ({ ...option }));
|
|
21
|
+
}
|
|
22
|
+
const current = Array.isArray(model.props.options) ? model.props.options : [];
|
|
23
|
+
return current.map((option) => ({ ...option }));
|
|
24
|
+
};
|
|
15
25
|
|
|
16
26
|
export class SelectFieldModel extends FieldModel {
|
|
17
27
|
render() {
|
|
28
|
+
const fallbackOptions = getOriginalEnumOptions(this);
|
|
18
29
|
const options = this.props.options?.map((v) => {
|
|
19
30
|
return {
|
|
20
31
|
...v,
|
|
21
32
|
label: this.translate(v.label),
|
|
22
33
|
};
|
|
23
34
|
});
|
|
35
|
+
const selectedLabels = getSelectedEnumLabels(this.props.value, fallbackOptions).map((item) => ({
|
|
36
|
+
...item,
|
|
37
|
+
label: this.translate(item.label),
|
|
38
|
+
}));
|
|
39
|
+
const value = Array.isArray(this.props.value)
|
|
40
|
+
? selectedLabels
|
|
41
|
+
: selectedLabels.length > 0
|
|
42
|
+
? selectedLabels[0]
|
|
43
|
+
: this.props.value;
|
|
24
44
|
|
|
25
45
|
// TODO: 移动端相关的代码需迁移到单独的插件中
|
|
26
46
|
if (this.context.isMobileLayout) {
|
|
27
|
-
return <MobileSelect {...this.props} options={options} />;
|
|
47
|
+
return <MobileSelect {...this.props} options={options} displayValue={value} />;
|
|
28
48
|
}
|
|
29
49
|
|
|
30
50
|
return (
|
|
31
51
|
<Select
|
|
32
52
|
{...this.props}
|
|
53
|
+
value={value}
|
|
54
|
+
labelInValue
|
|
55
|
+
onChange={(nextValue) => {
|
|
56
|
+
if (Array.isArray(nextValue)) {
|
|
57
|
+
this.props.onChange?.(nextValue.map((item: any) => item?.value));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
this.props.onChange?.(nextValue?.value);
|
|
61
|
+
}}
|
|
33
62
|
options={options}
|
|
63
|
+
labelRender={(item) => item.label}
|
|
34
64
|
maxTagCount="responsive"
|
|
35
65
|
maxTagPlaceholder={(omittedValues) => (
|
|
36
66
|
<Tooltip
|
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
import { describe, expect, it, vi } from 'vitest';
|
|
11
11
|
import { FlowEngine } from '@nocobase/flow-engine';
|
|
12
|
+
import { render, screen } from '@testing-library/react';
|
|
12
13
|
import { ClickableFieldModel } from '../ClickableFieldModel';
|
|
14
|
+
import { DisplayTextFieldModel } from '../DisplayTextFieldModel';
|
|
13
15
|
|
|
14
16
|
function createRolesFieldModel(sourceRecord: Record<string, any>) {
|
|
15
17
|
const engine = new FlowEngine();
|
|
@@ -84,4 +86,25 @@ describe('ClickableFieldModel', () => {
|
|
|
84
86
|
{ debounce: true },
|
|
85
87
|
);
|
|
86
88
|
});
|
|
89
|
+
|
|
90
|
+
it('renders object title field values by configured target title field', () => {
|
|
91
|
+
const engine = new FlowEngine();
|
|
92
|
+
engine.registerModels({ DisplayTextFieldModel });
|
|
93
|
+
|
|
94
|
+
const model = engine.createModel<DisplayTextFieldModel>({
|
|
95
|
+
use: DisplayTextFieldModel,
|
|
96
|
+
uid: 'display-text-association-title',
|
|
97
|
+
});
|
|
98
|
+
model.context.defineProperty('collectionField', {
|
|
99
|
+
value: {
|
|
100
|
+
targetCollectionTitleFieldName: 'code',
|
|
101
|
+
isAssociationField: () => true,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
render(model.renderInDisplayStyle({ id: 2, name: 'Sales', code: 'S-001' }, { id: 1 }, false));
|
|
106
|
+
|
|
107
|
+
expect(screen.getByText('S-001')).toBeInTheDocument();
|
|
108
|
+
expect(screen.queryByText('Sales')).not.toBeInTheDocument();
|
|
109
|
+
});
|
|
87
110
|
});
|
|
@@ -13,7 +13,7 @@ import { Button, CheckList, Popup, SearchBar } from 'antd-mobile';
|
|
|
13
13
|
import React, { useEffect, useMemo, useState } from 'react';
|
|
14
14
|
|
|
15
15
|
export function MobileSelect(props) {
|
|
16
|
-
const { value, onChange, onChangeComplete, disabled, options = [], mode } = props;
|
|
16
|
+
const { value, displayValue, onChange, onChangeComplete, disabled, options = [], mode } = props;
|
|
17
17
|
const ctx = useFlowModelContext();
|
|
18
18
|
const t = ctx.t;
|
|
19
19
|
const [visible, setVisible] = useState(false);
|
|
@@ -44,6 +44,7 @@ export function MobileSelect(props) {
|
|
|
44
44
|
<div onClick={() => !disabled && setVisible(true)}>
|
|
45
45
|
<Select
|
|
46
46
|
{...props}
|
|
47
|
+
value={displayValue ?? value}
|
|
47
48
|
open={false}
|
|
48
49
|
dropdownStyle={{ display: 'none' }}
|
|
49
50
|
showSearch={false}
|
|
@@ -178,6 +178,13 @@ describe('MobileSelect', () => {
|
|
|
178
178
|
clickTrigger();
|
|
179
179
|
expect(screen.queryByTestId('popup')).not.toBeInTheDocument();
|
|
180
180
|
});
|
|
181
|
+
|
|
182
|
+
it('prefers displayValue for trigger rendering', () => {
|
|
183
|
+
const displayValue = [{ label: 'Published', value: 'published' }];
|
|
184
|
+
renderMobileSelect({ value: ['published'], displayValue, mode: 'multiple' });
|
|
185
|
+
|
|
186
|
+
expect(mockState.selectProps?.value).toEqual(displayValue);
|
|
187
|
+
});
|
|
181
188
|
});
|
|
182
189
|
|
|
183
190
|
function SubTableCellHarness({ value, onCommit, mode }: { value: any; onCommit: (value: any) => void; mode?: string }) {
|