@nocobase/client-v2 2.2.0-beta.15 → 2.2.0-beta.17

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.
Files changed (93) hide show
  1. package/es/components/form/ScanInput/useCodeScanner.d.ts +2 -2
  2. package/es/flow/actions/linkageRules.d.ts +25 -0
  3. package/es/flow/components/FieldAssignValueInput.d.ts +4 -30
  4. package/es/flow/components/FlowRoute.d.ts +2 -2
  5. package/es/flow/components/field-value-variable/DateVariableEditor.d.ts +24 -0
  6. package/es/flow/components/field-value-variable/FieldValueVariableInput.d.ts +31 -0
  7. package/es/flow/components/field-value-variable/dateValue.d.ts +36 -0
  8. package/es/flow/components/field-value-variable/index.d.ts +11 -0
  9. package/es/flow/internal/registerDeviceTypeContext.d.ts +10 -0
  10. package/es/flow/models/actions/UpdateRecordActionUtils.d.ts +8 -2
  11. package/es/flow/models/base/ActionModelCore.d.ts +4 -2
  12. package/es/flow/models/base/BlockGridModel.d.ts +3 -0
  13. package/es/flow/models/blocks/form/FormBlockModel.d.ts +6 -0
  14. package/es/flow/models/blocks/form/FormGridModel.d.ts +6 -0
  15. package/es/flow/models/blocks/form/value-runtime/rules.d.ts +1 -0
  16. package/es/flow/models/blocks/table/JSColumnModel.d.ts +2 -0
  17. package/es/flow/models/blocks/table/TableColumnModel.d.ts +1 -0
  18. package/es/flow/models/fields/DateTimeFieldModel/dateLimit.d.ts +15 -7
  19. package/es/index.d.ts +1 -0
  20. package/es/index.mjs +105 -111
  21. package/es/ui-operation/index.d.ts +15 -0
  22. package/es/ui-operation/ui-operation-codec.d.ts +10 -0
  23. package/lib/index.js +108 -114
  24. package/lib/locale/languageCodes.js +1 -0
  25. package/package.json +8 -7
  26. package/src/__tests__/nocobase-buildin-plugin-auth.test.tsx +7 -2
  27. package/src/components/form/ScanInput/CodeScanner.tsx +61 -34
  28. package/src/components/form/ScanInput/__tests__/CodeScanner.test.tsx +101 -0
  29. package/src/components/form/ScanInput/__tests__/useCodeScanner.test.tsx +114 -18
  30. package/src/components/form/ScanInput/useCodeScanner.ts +166 -23
  31. package/src/flow/__tests__/FlowRoute.test.tsx +126 -8
  32. package/src/flow/__tests__/PluginFlowEngine.test.ts +58 -0
  33. package/src/flow/actions/__tests__/actionLinkageRules.forkProps.test.ts +314 -0
  34. package/src/flow/actions/__tests__/dataScopeFormValueClear.test.ts +102 -0
  35. package/src/flow/actions/__tests__/linkageRules.actionStates.test.ts +33 -0
  36. package/src/flow/actions/linkageRules.tsx +51 -13
  37. package/src/flow/common/Markdown/style.ts +4 -0
  38. package/src/flow/components/BlockItemCard.tsx +2 -2
  39. package/src/flow/components/FieldAssignValueInput.tsx +38 -621
  40. package/src/flow/components/FlowRoute.tsx +71 -29
  41. package/src/flow/components/field-value-variable/DateVariableEditor.tsx +181 -0
  42. package/src/flow/components/field-value-variable/FieldValueVariableInput.tsx +326 -0
  43. package/src/flow/components/field-value-variable/__tests__/FieldValueVariableInput.test.tsx +380 -0
  44. package/src/flow/components/field-value-variable/dateValue.ts +223 -0
  45. package/src/flow/components/field-value-variable/index.ts +12 -0
  46. package/src/flow/index.ts +2 -0
  47. package/src/flow/internal/registerDeviceTypeContext.ts +39 -0
  48. package/src/flow/models/actions/UpdateRecordActionModel.tsx +18 -1
  49. package/src/flow/models/actions/UpdateRecordActionUtils.ts +18 -6
  50. package/src/flow/models/actions/__tests__/UpdateRecordActionModel.test.ts +76 -4
  51. package/src/flow/models/base/ActionModelCore.tsx +11 -2
  52. package/src/flow/models/base/BlockGridModel.tsx +26 -0
  53. package/src/flow/models/base/CollectionBlockModel.tsx +15 -2
  54. package/src/flow/models/base/PageModel/PageTabModel.tsx +67 -17
  55. package/src/flow/models/base/PageModel/__tests__/PageTabModel.test.ts +509 -12
  56. package/src/flow/models/base/__tests__/ActionModelCore.render.test.tsx +49 -0
  57. package/src/flow/models/base/__tests__/BlockGridModel.selectSceneActivation.test.ts +124 -0
  58. package/src/flow/models/base/__tests__/CollectionBlockModel.addAppends.test.ts +41 -0
  59. package/src/flow/models/base/__tests__/CollectionBlockModel.initialBeforeRenderRefresh.test.ts +60 -0
  60. package/src/flow/models/blocks/assign-form/AssignFormItemModel.tsx +28 -53
  61. package/src/flow/models/blocks/filter-form/FilterFormBlockModel.tsx +27 -3
  62. package/src/flow/models/blocks/filter-form/__tests__/defaultValues.wiring.test.ts +32 -4
  63. package/src/flow/models/blocks/form/FormBlockModel.tsx +4 -0
  64. package/src/flow/models/blocks/form/FormGridModel.tsx +4 -0
  65. package/src/flow/models/blocks/form/__tests__/FormBlockModel.test.tsx +8 -2
  66. package/src/flow/models/blocks/form/value-runtime/__tests__/runtime.test.ts +435 -0
  67. package/src/flow/models/blocks/form/value-runtime/rules.ts +67 -14
  68. package/src/flow/models/blocks/form/value-runtime/runtime.ts +43 -19
  69. package/src/flow/models/blocks/js-block/JSBlock.tsx +1 -1
  70. package/src/flow/models/blocks/table/JSColumnModel.tsx +10 -1
  71. package/src/flow/models/blocks/table/TableBlockModel.tsx +3 -1
  72. package/src/flow/models/blocks/table/TableColumnModel.tsx +30 -1
  73. package/src/flow/models/blocks/table/__tests__/JSColumnModel.test.tsx +52 -5
  74. package/src/flow/models/blocks/table/__tests__/TableBlockModel.quickEditRefresh.test.ts +12 -0
  75. package/src/flow/models/blocks/table/__tests__/TableColumnModel.test.tsx +33 -0
  76. package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/PopupSubTableFieldModel.tsx +8 -0
  77. package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/__tests__/popupContext.test.ts +120 -0
  78. package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/actions/PopupSubTableEditActionModel.tsx +10 -2
  79. package/src/flow/models/fields/AssociationFieldModel/__tests__/RecordPickerFieldModel.itemContext.test.ts +46 -0
  80. package/src/flow/models/fields/DateTimeFieldModel/__tests__/DateTimeNoTzFieldModel.dateLimit.test.tsx +149 -0
  81. package/src/flow/models/fields/DateTimeFieldModel/dateLimit.ts +149 -113
  82. package/src/flow/models/fields/JsonFieldModel.tsx +31 -8
  83. package/src/flow/models/fields/__tests__/JsonFieldModel.test.ts +82 -0
  84. package/src/flow/models/fields/mobile-components/MobileLazySelect.tsx +1 -0
  85. package/src/flow/models/fields/mobile-components/__tests__/MobileSelect.test.tsx +20 -2
  86. package/src/flow/utils/dataScopeFormValueClear.ts +4 -3
  87. package/src/index.ts +1 -0
  88. package/src/layout-manager/LayoutContentRoute.tsx +2 -1
  89. package/src/layout-manager/LayoutRoute.tsx +2 -1
  90. package/src/layout-manager/__tests__/LayoutRoute.test.tsx +87 -1
  91. package/src/locale/languageCodes.ts +1 -0
  92. package/src/ui-operation/index.ts +33 -0
  93. package/src/ui-operation/ui-operation-codec.ts +54 -0
@@ -86,6 +86,10 @@ export class FormValueRuntime {
86
86
 
87
87
  this.ruleEngine = new RuleEngine({
88
88
  getBlockModelUid: () => String(this.model?.uid),
89
+ getAssignRulesModelUid: () => {
90
+ const grid = this.model?.subModels?.grid;
91
+ return !Array.isArray(grid) && grid?.uid ? String(grid.uid) : undefined;
92
+ },
89
93
  getActionName: () => this.model?.getAclActionName?.() ?? this.model?.context?.actionName,
90
94
  getBlockContext: () => this.model?.context,
91
95
  getEngine: () => this.model?.context?.engine,
@@ -190,13 +194,32 @@ export class FormValueRuntime {
190
194
  return values;
191
195
  }
192
196
 
193
- private toMirrorSnapshot(value: any) {
194
- const raw = isObservable(value) ? toJS(value) : value;
195
- return _.cloneDeepWith(raw, (item) => {
196
- if (!item || typeof item !== 'object') return undefined;
197
- if (Array.isArray(item) || _.isPlainObject(item)) return undefined;
198
- return item;
199
- });
197
+ private toMirrorSnapshot<T>(value: T): T {
198
+ const cloneValue = (input: unknown): unknown =>
199
+ _.cloneDeepWith(input, (item: unknown) => {
200
+ if (isObservable(item)) {
201
+ const plainItem = toJS(item);
202
+ if (plainItem !== item) return cloneValue(plainItem);
203
+ }
204
+
205
+ // Tracking form-value array proxies hide `constructor`, while Lodash's array
206
+ // clone relies on it being callable.
207
+ if (Array.isArray(item) && typeof item.constructor !== 'function') {
208
+ const plainArray = new Array(item.length);
209
+ for (let index = 0; index < item.length; index++) {
210
+ if (Object.hasOwn(item, index)) {
211
+ plainArray[index] = cloneValue(item[index]);
212
+ }
213
+ }
214
+ return plainArray;
215
+ }
216
+
217
+ if (!item || typeof item !== 'object') return undefined;
218
+ if (Array.isArray(item) || _.isPlainObject(item)) return undefined;
219
+ return item;
220
+ });
221
+
222
+ return cloneValue(value) as T;
200
223
  }
201
224
 
202
225
  canApplyDefaultValuePatch(namePath: NamePath, resolved: any) {
@@ -940,11 +963,12 @@ export class FormValueRuntime {
940
963
  const changedPaths: NamePath[] = [];
941
964
 
942
965
  if (!Array.isArray(patch)) {
943
- const patchEntries = Object.entries(patch || {}).filter(([pathKey, rawValue]) => {
944
- if (shouldSkipByLinkageScope(pathKey)) return false;
945
- const value = isObservable(rawValue) ? toJS(rawValue) : rawValue;
946
- return !_.isEqual(this.getFormValueAtPath([pathKey]), value);
947
- });
966
+ const patchEntries = Object.entries(patch || {})
967
+ .map(([pathKey, rawValue]) => [pathKey, this.toMirrorSnapshot(rawValue)] as const)
968
+ .filter(([pathKey, value]) => {
969
+ if (shouldSkipByLinkageScope(pathKey)) return false;
970
+ return !_.isEqual(this.getFormValueAtPath([pathKey]), value);
971
+ });
948
972
  const patchToApply = Object.fromEntries(patchEntries);
949
973
  const patchKeys = patchEntries.map(([pathKey]) => pathKey);
950
974
  if (!patchKeys.length) {
@@ -955,8 +979,7 @@ export class FormValueRuntime {
955
979
  }
956
980
  this.suppressFormCallbackDepth++;
957
981
  try {
958
- for (const [pathKey, rawValue] of patchEntries) {
959
- const value = isObservable(rawValue) ? toJS(rawValue) : rawValue;
982
+ for (const [pathKey, value] of patchEntries) {
960
983
  if (typeof form.setFieldValue === 'function') {
961
984
  form.setFieldValue(pathKey, value);
962
985
  } else if (typeof (form as any).setFields === 'function') {
@@ -1017,7 +1040,7 @@ export class FormValueRuntime {
1017
1040
  const namePath = this.resolveNamePath(callerCtx, item.path);
1018
1041
  const pathKey = namePathToPathKey(namePath);
1019
1042
  const rawValue = item.value;
1020
- const value = isObservable(rawValue) ? toJS(rawValue) : rawValue;
1043
+ const value = this.toMirrorSnapshot(rawValue);
1021
1044
 
1022
1045
  if (shouldSkipByLinkageScope(pathKey)) continue;
1023
1046
 
@@ -1156,17 +1179,18 @@ export class FormValueRuntime {
1156
1179
  if (!form) return;
1157
1180
  if (source === 'override' && this.findUserEditedHit(pathKey)) return;
1158
1181
 
1182
+ const value = this.toMirrorSnapshot(nextValue);
1159
1183
  const prevValue = _.get(this.valuesMirror, namePath);
1160
- if (_.isEqual(prevValue, nextValue)) return;
1184
+ if (_.isEqual(prevValue, value)) return;
1161
1185
 
1162
1186
  this.writeSeq += 1;
1163
1187
  const writeSeq = this.writeSeq;
1164
1188
 
1165
1189
  this.suppressFormCallbackDepth++;
1166
1190
  try {
1167
- form.setFieldValue?.(namePath, nextValue);
1168
- _.set(this.valuesMirror, namePath, this.toMirrorSnapshot(nextValue));
1169
- this.syncMountedFieldModelValue(namePath, nextValue);
1191
+ form.setFieldValue?.(namePath, value);
1192
+ _.set(this.valuesMirror, namePath, this.toMirrorSnapshot(value));
1193
+ this.syncMountedFieldModelValue(namePath, value);
1170
1194
  this.bumpChangeTick();
1171
1195
  } finally {
1172
1196
  this.suppressFormCallbackDepth--;
@@ -235,7 +235,7 @@ export class JSBlockModel extends BlockModel {
235
235
  const cardProps = {
236
236
  ...rest,
237
237
  height,
238
- style,
238
+ ...(style === undefined ? {} : { style }),
239
239
  ...(beforeContent === undefined ? {} : { beforeContent }),
240
240
  ...(afterContent === undefined ? {} : { afterContent }),
241
241
  };
@@ -9,13 +9,14 @@
9
9
 
10
10
  import { LockOutlined, QuestionCircleOutlined } from '@ant-design/icons';
11
11
  import { css } from '@emotion/css';
12
- import type { PropertyMetaFactory } from '@nocobase/flow-engine';
12
+ import type { FlowModelOptions, PropertyMetaFactory } from '@nocobase/flow-engine';
13
13
  import {
14
14
  Droppable,
15
15
  tExpr,
16
16
  FlowsFloatContextMenu,
17
17
  DragHandler,
18
18
  MemoFlowModelRenderer,
19
+ createCurrentRecordMetaFactory,
19
20
  createRecordMetaFactory,
20
21
  createRecordResolveOnServerWithLocal,
21
22
  ElementProxy,
@@ -51,6 +52,14 @@ function getRecordRenderSignature(record: any) {
51
52
  export class JSColumnModel extends TableCustomColumnModel {
52
53
  // Stable per‑instance render component to avoid remounts across re-renders
53
54
  private _RenderComponent?: React.ComponentType;
55
+
56
+ onInit(options: FlowModelOptions): void {
57
+ super.onInit(options);
58
+ this.context.defineProperty('record', {
59
+ meta: createCurrentRecordMetaFactory(this.context, () => this.context.collection),
60
+ });
61
+ }
62
+
54
63
  renderHiddenInConfig() {
55
64
  return (
56
65
  <Tooltip
@@ -244,7 +244,9 @@ export class TableBlockModel extends CollectionBlockModel<TableBlockModelStructu
244
244
  }
245
245
 
246
246
  createResource(ctx, params) {
247
- return this.context.createResource(MultiRecordResource);
247
+ const resource = this.context.createResource(MultiRecordResource);
248
+ resource.addRequestHeader('X-With-ACL-Meta', 'true');
249
+ return resource;
248
250
  }
249
251
 
250
252
  /**
@@ -194,6 +194,35 @@ export class TableColumnModel extends DisplayItemModel {
194
194
  .filter(Boolean);
195
195
  }
196
196
 
197
+ canQuickEdit(record: Record<string, unknown>): boolean {
198
+ if (!this.props.editable) {
199
+ return false;
200
+ }
201
+ if (this.context.skipAclCheck) {
202
+ return true;
203
+ }
204
+
205
+ const blockModel = this.context.blockModel;
206
+ const collection = blockModel?.collection;
207
+ const resource = blockModel?.resource;
208
+ const collectionField = this.collectionField;
209
+ if (!collection || !resource || !collectionField) {
210
+ return false;
211
+ }
212
+
213
+ const filterByTk = collection.getFilterByTK(record);
214
+ const recordPkValue = typeof filterByTk === 'string' || typeof filterByTk === 'number' ? filterByTk : undefined;
215
+
216
+ return this.context.acl.can({
217
+ dataSourceKey: collection.dataSourceKey,
218
+ resourceName: resource.getResourceName() || collection.name,
219
+ actionName: 'update',
220
+ recordPkValue,
221
+ allowedActions: resource.getMeta('allowedActions'),
222
+ fields: [collectionField.name],
223
+ });
224
+ }
225
+
197
226
  getColumnProps(): TableColumnProps & { sortField?: string } {
198
227
  if (!this.props.width) {
199
228
  return;
@@ -250,7 +279,7 @@ export class TableColumnModel extends DisplayItemModel {
250
279
  record,
251
280
  recordIndex: record?.__index || recordIndex,
252
281
  width: this.props.width - 16,
253
- editable: this.props.editable,
282
+ editable: this.canQuickEdit(record),
254
283
  dataIndex: this.props.dataIndex,
255
284
  title: this.props.title,
256
285
  overflowMode: this.props.overflowMode,
@@ -11,17 +11,62 @@ import { FlowEngine } from '@nocobase/flow-engine';
11
11
  import { describe, expect, it, vi } from 'vitest';
12
12
  import { JSColumnModel } from '../JSColumnModel';
13
13
 
14
- describe('JSColumnModel row refresh', () => {
14
+ describe('JSColumnModel', () => {
15
+ it('declares current record metadata before rendering any row', async () => {
16
+ const engine = new FlowEngine();
17
+ engine.registerModels({ JSColumnModel });
18
+ const model = engine.createModel<JSColumnModel>({
19
+ use: 'JSColumnModel',
20
+ uid: 'js-column-record-meta',
21
+ props: {
22
+ width: 200,
23
+ title: 'JS column',
24
+ },
25
+ });
26
+
27
+ engine.context.dataSourceManager.getDataSource('main').addCollection({
28
+ name: 'users',
29
+ filterTargetKey: 'id',
30
+ fields: [
31
+ { name: 'id', type: 'integer', interface: 'number' },
32
+ { name: 'age', type: 'integer', interface: 'integer' },
33
+ ],
34
+ });
35
+
36
+ const collection = engine.context.dataSourceManager.getCollection('main', 'users');
37
+ model.context.defineProperty('collection', {
38
+ value: collection,
39
+ });
40
+
41
+ const recordNode = model.context.getPropertyMetaTree().find((node) => node.name === 'record');
42
+ const recordFields =
43
+ typeof recordNode?.children === 'function' ? await recordNode.children() : recordNode?.children || [];
44
+
45
+ expect(model.context.record).toBeUndefined();
46
+ expect(recordNode).toMatchObject({
47
+ name: 'record',
48
+ title: 'Current record',
49
+ paths: ['record'],
50
+ });
51
+ expect(recordFields).toEqual(
52
+ expect.arrayContaining([
53
+ expect.objectContaining({ name: 'id', paths: ['record', 'id'] }),
54
+ expect.objectContaining({ name: 'age', paths: ['record', 'age'] }),
55
+ ]),
56
+ );
57
+ });
58
+
15
59
  it('changes renderer key and invalidates beforeRender cache when row content changes', () => {
16
60
  const engine = new FlowEngine();
17
- const model = new JSColumnModel({
61
+ engine.registerModels({ JSColumnModel });
62
+ const model = engine.createModel<JSColumnModel>({
63
+ use: 'JSColumnModel',
18
64
  uid: 'js-column-row-refresh',
19
- flowEngine: engine,
20
65
  props: {
21
66
  width: 200,
22
67
  title: 'JS column',
23
68
  },
24
- } as any);
69
+ });
25
70
 
26
71
  engine.context.dataSourceManager.getDataSource('main').addCollection({
27
72
  name: 'users',
@@ -39,12 +84,14 @@ describe('JSColumnModel row refresh', () => {
39
84
  });
40
85
 
41
86
  const column = model.getColumnProps();
42
- const first = column.render(null, { id: 1, age: 3, workyears: 39.2 }, 0) as any;
87
+ const firstRecord = { id: 1, age: 3, workyears: 39.2 };
88
+ const first = column.render(null, firstRecord, 0) as any;
43
89
  const fork = model.getFork('1') as any;
44
90
  const invalidateFlowCache = vi.fn();
45
91
  fork.invalidateFlowCache = invalidateFlowCache;
46
92
  const second = column.render(null, { id: 1, age: 37, workyears: 39.2 }, 0) as any;
47
93
 
94
+ expect(fork.context.record).toEqual({ id: 1, age: 37, workyears: 39.2 });
48
95
  expect(first.key).not.toBe(second.key);
49
96
  expect(invalidateFlowCache).toHaveBeenCalledWith('beforeRender');
50
97
  });
@@ -9,8 +9,20 @@
9
9
 
10
10
  import { FlowEngine, MultiRecordResource } from '@nocobase/flow-engine';
11
11
  import { describe, expect, it } from 'vitest';
12
+ import { TableBlockModel } from '../TableBlockModel';
12
13
 
13
14
  describe('TableBlockModel quick edit refresh', () => {
15
+ it('requests record-level ACL metadata for table data', () => {
16
+ const engine = new FlowEngine();
17
+ const model = {
18
+ context: engine.context,
19
+ };
20
+
21
+ const resource = TableBlockModel.prototype.createResource.call(model, {}, {});
22
+
23
+ expect(resource.getRequestOptions().headers['X-With-ACL-Meta']).toBe('true');
24
+ });
25
+
14
26
  it('updates table data through a new array reference after quick editing a flat row', () => {
15
27
  const engine = new FlowEngine();
16
28
  const resource = engine.context.createResource(MultiRecordResource);
@@ -66,6 +66,39 @@ describe('TableColumnModel sorter settings', () => {
66
66
  expect(setProps).toHaveBeenCalledWith('editable', false);
67
67
  });
68
68
 
69
+ it('checks record update scope and field permission before enabling quick edit', () => {
70
+ const can = vi.fn(() => false);
71
+ const model = {
72
+ props: { editable: true },
73
+ collectionField: { name: 'title' },
74
+ context: {
75
+ skipAclCheck: false,
76
+ acl: { can },
77
+ blockModel: {
78
+ collection: {
79
+ dataSourceKey: 'main',
80
+ name: 'posts',
81
+ getFilterByTK: () => 2,
82
+ },
83
+ resource: {
84
+ getResourceName: () => 'posts',
85
+ getMeta: () => ({ update: [1] }),
86
+ },
87
+ },
88
+ },
89
+ };
90
+
91
+ expect(TableColumnModel.prototype.canQuickEdit.call(model, { id: 2 })).toBe(false);
92
+ expect(can).toHaveBeenCalledWith({
93
+ dataSourceKey: 'main',
94
+ resourceName: 'posts',
95
+ actionName: 'update',
96
+ recordPkValue: 2,
97
+ allowedActions: { update: [1] },
98
+ fields: ['title'],
99
+ });
100
+ });
101
+
69
102
  it('hides sortable setting for association fields', async () => {
70
103
  const engine = new FlowEngine();
71
104
  const model = new TableColumnModel({ uid: 'table-column-association-sorter', flowEngine: engine } as any);
@@ -28,6 +28,7 @@ import { observer } from '@formily/reactive-react';
28
28
  import React, { useEffect, useMemo, useState, useRef } from 'react';
29
29
  import { useTranslation } from 'react-i18next';
30
30
  import { buildRecordPickerPopupContextInputArgs, RecordPickerContent } from '../RecordPickerFieldModel';
31
+ import { buildOpenerUids } from '../recordSelectShared';
31
32
  import { AssociationFieldModel } from '../AssociationFieldModel';
32
33
  import { adjustColumnOrder } from '../../../blocks/table/utils';
33
34
  import { isSubTableColumnFieldComponentContext } from '../SubTableFieldModel/SubTableColumnModel';
@@ -653,6 +654,11 @@ PopupSubTableFieldModel.registerFlow({
653
654
  const parentItemOptions = ctx?.getPropertyOptions?.('item');
654
655
  const itemIndex = Array.isArray(ctx.model?.props?.value) ? ctx.model.props.value.length : 0;
655
656
  const itemLength = itemIndex + 1;
657
+ const associationName = ctx.collectionField?.resourceName;
658
+ const sourceId = parentItem?.value
659
+ ? ctx.collectionField?.collection?.getFilterByTK?.(parentItem.value)
660
+ : undefined;
661
+ const openerUids = buildOpenerUids(ctx, ctx.inputArgs);
656
662
  ctx.viewer.open({
657
663
  type: openMode,
658
664
  width: sizeToWidthMap[openMode][size],
@@ -663,12 +669,14 @@ PopupSubTableFieldModel.registerFlow({
663
669
  scene: 'subForm',
664
670
  dataSourceKey: ctx.collection.dataSourceKey,
665
671
  collectionName: ctx.collectionField?.target,
672
+ ...(associationName && sourceId != null ? { associationName, sourceId } : {}),
666
673
  collectionField: ctx.collectionField,
667
674
  parentItem,
668
675
  parentItemMeta: parentItemOptions?.meta,
669
676
  parentItemResolver: parentItemOptions?.resolveOnServer,
670
677
  itemIndex,
671
678
  itemLength,
679
+ openerUids,
672
680
  },
673
681
  content: () => <EditFormContent model={ctx.model} scene="create" />,
674
682
  styles: {
@@ -0,0 +1,120 @@
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 { PopupSubTableFieldModel } from '../PopupSubTableFieldModel';
12
+ import { PopupSubTableEditActionModel } from '../actions/PopupSubTableEditActionModel';
13
+
14
+ function getOpenViewHandler(modelClass: typeof PopupSubTableFieldModel | typeof PopupSubTableEditActionModel) {
15
+ const flow = modelClass.globalFlowRegistry.getFlow('popupSettings');
16
+ const step = flow?.getStep('openView');
17
+ const handler = step?.serialize().handler;
18
+
19
+ if (!handler) {
20
+ throw new Error('popupSettings.openView handler is not registered');
21
+ }
22
+
23
+ return handler;
24
+ }
25
+
26
+ function createContext(record?: Record<string, unknown>, parentRecord: Record<string, unknown> = { id: 1 }) {
27
+ const open = vi.fn();
28
+ const sourceCollection = {
29
+ getFilterByTK: vi.fn((sourceRecord: Record<string, unknown>) => sourceRecord.id),
30
+ };
31
+ const model = {
32
+ uid: 'popup-subtable-uid',
33
+ props: {
34
+ value: record ? [record] : [],
35
+ },
36
+ context: {
37
+ inputArgs: {},
38
+ },
39
+ flowEngine: {
40
+ context: {
41
+ themeToken: {
42
+ colorBgLayout: '#fff',
43
+ },
44
+ },
45
+ },
46
+ };
47
+
48
+ return {
49
+ open,
50
+ context: {
51
+ inputArgs: {},
52
+ item: {
53
+ value: parentRecord,
54
+ },
55
+ view: {
56
+ inputArgs: {
57
+ viewUid: 'parent-popup-uid',
58
+ openerUids: ['root-page-uid'],
59
+ },
60
+ },
61
+ viewer: { open },
62
+ layoutContentElement: {},
63
+ model,
64
+ associationModel: model,
65
+ collection: {
66
+ dataSourceKey: 'main',
67
+ filterTargetKey: 'id',
68
+ },
69
+ collectionField: {
70
+ target: 'roles',
71
+ resourceName: 'users.roles',
72
+ collection: sourceCollection,
73
+ },
74
+ record,
75
+ getFormValues: () => ({ id: 1 }),
76
+ getPropertyOptions: () => undefined,
77
+ },
78
+ };
79
+ }
80
+
81
+ describe('PopupSubTable popup context', () => {
82
+ it('passes popup and parent record context to the add-new popup', () => {
83
+ const { context, open } = createContext();
84
+ const handler = getOpenViewHandler(PopupSubTableFieldModel);
85
+
86
+ handler(context, { mode: 'drawer', size: 'medium' });
87
+
88
+ expect(open).toHaveBeenCalledOnce();
89
+ expect(open.mock.calls[0][0].inputArgs).toMatchObject({
90
+ openerUids: ['root-page-uid', 'parent-popup-uid'],
91
+ associationName: 'users.roles',
92
+ sourceId: 1,
93
+ });
94
+ });
95
+
96
+ it('passes popup and parent record context to the edit popup', () => {
97
+ const { context, open } = createContext({ id: 2, name: 'Member' });
98
+ const handler = getOpenViewHandler(PopupSubTableEditActionModel);
99
+
100
+ handler(context, { mode: 'dialog', size: 'medium' });
101
+
102
+ expect(open).toHaveBeenCalledOnce();
103
+ expect(open.mock.calls[0][0].inputArgs).toMatchObject({
104
+ openerUids: ['root-page-uid', 'parent-popup-uid'],
105
+ associationName: 'users.roles',
106
+ sourceId: 1,
107
+ });
108
+ });
109
+
110
+ it('does not expose a parent record reference before the parent record is persisted', () => {
111
+ const { context, open } = createContext(undefined, { nickname: 'Draft user' });
112
+ const handler = getOpenViewHandler(PopupSubTableFieldModel);
113
+
114
+ handler(context, { mode: 'drawer', size: 'medium' });
115
+
116
+ expect(open).toHaveBeenCalledOnce();
117
+ expect(open.mock.calls[0][0].inputArgs).not.toHaveProperty('associationName');
118
+ expect(open.mock.calls[0][0].inputArgs).not.toHaveProperty('sourceId');
119
+ });
120
+ });
@@ -17,6 +17,7 @@ import { capitalize } from 'lodash';
17
17
  import '../../../../base/ActionModel';
18
18
  import { ActionModel, ActionWithoutPermission } from '../../../../base/ActionModelCore';
19
19
  import { SkeletonFallback } from '../../../../../components/SkeletonFallback';
20
+ import { buildOpenerUids } from '../../recordSelectShared';
20
21
  import { bindPopupSubTableBeforeClose } from './popupSubTableBeforeClose';
21
22
 
22
23
  function FieldWithoutPermissionPlaceholder({ targetModel, children }) {
@@ -29,7 +30,7 @@ function FieldWithoutPermissionPlaceholder({ targetModel, children }) {
29
30
  const dataSourcePrefix = `${t(dataSource.displayName || dataSource.key)} > `;
30
31
  const collectionPrefix = collection ? `${t(collection.title) || collection.name || collection.tableName} > ` : '';
31
32
  return `${dataSourcePrefix}${collectionPrefix}${name}`;
32
- }, []);
33
+ }, [collection, dataSource.displayName, dataSource.key, name, t]);
33
34
  const { actionName } = fieldModel.forbidden || {};
34
35
  const messageValue = useMemo(() => {
35
36
  return t(
@@ -39,7 +40,7 @@ function FieldWithoutPermissionPlaceholder({ targetModel, children }) {
39
40
  actionName: t(capitalize(actionName)),
40
41
  },
41
42
  ).replaceAll('&gt;', '>');
42
- }, [nameValue, t]);
43
+ }, [actionName, nameValue, t]);
43
44
  return <Tooltip title={messageValue}>{children}</Tooltip>;
44
45
  }
45
46
 
@@ -347,6 +348,11 @@ PopupSubTableEditActionModel.registerFlow({
347
348
  return undefined;
348
349
  }
349
350
  })();
351
+ const associationName = ctx.collectionField?.resourceName;
352
+ const sourceId = parentItem?.value
353
+ ? ctx.collectionField?.collection?.getFilterByTK?.(parentItem.value)
354
+ : undefined;
355
+ const openerUids = buildOpenerUids(ctx, ctx.inputArgs);
350
356
  ctx.viewer.open({
351
357
  type: openMode,
352
358
  width: sizeToWidthMap[openMode][size],
@@ -357,6 +363,7 @@ PopupSubTableEditActionModel.registerFlow({
357
363
  scene: 'subForm',
358
364
  dataSourceKey: ctx.collection.dataSourceKey,
359
365
  collectionName: ctx.collectionField?.target,
366
+ ...(associationName && sourceId != null ? { associationName, sourceId } : {}),
360
367
  collectionField: ctx.collectionField,
361
368
  record: ctx.record,
362
369
  parentItem,
@@ -364,6 +371,7 @@ PopupSubTableEditActionModel.registerFlow({
364
371
  parentItemResolver: parentItemOptions?.resolveOnServer,
365
372
  itemIndex,
366
373
  itemLength,
374
+ openerUids,
367
375
  },
368
376
  content: () => <EditFormContent model={ctx.model} />,
369
377
  styles: {
@@ -243,6 +243,52 @@ describe('RecordPickerFieldModel item context', () => {
243
243
  expect(associationOpts.serverOnlyWhenContextParams).toBe(true);
244
244
  });
245
245
 
246
+ it('builds whole-association descriptors for current and recursive parent item values', async () => {
247
+ const people = {
248
+ name: 'people',
249
+ dataSourceKey: 'main',
250
+ filterTargetKey: 'id',
251
+ getFields: () => [],
252
+ getField: () => undefined,
253
+ } as any;
254
+ const owner = {
255
+ name: 'owner',
256
+ target: 'people',
257
+ targetCollection: people,
258
+ isAssociationField: () => true,
259
+ } as any;
260
+ const collection = {
261
+ name: 'tasks',
262
+ dataSourceKey: 'main',
263
+ getFields: () => [owner],
264
+ getField: (name: string) => (name === 'owner' ? owner : undefined),
265
+ } as any;
266
+ const options = createAssociationItemChainContextPropertyOptions({
267
+ t: (value) => value,
268
+ title: 'Current item',
269
+ collectionAccessor: () => collection,
270
+ propertiesAccessor: (ctx) => ctx.item.value,
271
+ resolverPropertiesAccessor: () => ({ owner: { id: 7 } }),
272
+ parentCollectionAccessor: () => collection,
273
+ parentAccessors: {
274
+ parentPropertiesAccessor: () => ({ owner: { id: 8 } }),
275
+ parentItemMetaAccessor: () => undefined,
276
+ parentItemResolverAccessor: () => undefined,
277
+ },
278
+ });
279
+ const meta = await options.meta();
280
+ const params = await meta.buildVariablesParams({
281
+ item: { value: { owner: { id: 7 } }, parentItem: { value: { owner: { id: 8 } } } },
282
+ });
283
+
284
+ expect(params).toEqual({
285
+ parentItem: { value: { owner: { collection: 'people', dataSourceKey: 'main', filterByTk: 8 } } },
286
+ value: { owner: { collection: 'people', dataSourceKey: 'main', filterByTk: 7 } },
287
+ });
288
+ expect(options.resolveOnServer('value.owner.name')).toBe(true);
289
+ expect(options.resolveOnServer('parentItem.value.owner.name')).toBe(true);
290
+ });
291
+
246
292
  it('disables current item attributes for select-record popup item context', async () => {
247
293
  const parentCtx = new FlowContext();
248
294
  const departmentsCollection = {