@nocobase/client-v2 2.1.38 → 2.1.40

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 (20) hide show
  1. package/es/flow/models/blocks/filter-form/FilterFormGridModel.d.ts +1 -0
  2. package/es/flow/models/blocks/filter-form/FilterFormSubmitActionModel.d.ts +3 -1
  3. package/es/flow/models/blocks/form/FormBlockModel.d.ts +1 -0
  4. package/es/index.mjs +60 -60
  5. package/lib/index.js +92 -92
  6. package/package.json +7 -7
  7. package/src/flow/actions/__tests__/linkageRules.subFormSetFieldProps.test.ts +72 -0
  8. package/src/flow/actions/linkageRules.tsx +8 -3
  9. package/src/flow/models/blocks/filter-form/FilterFormGridModel.tsx +36 -5
  10. package/src/flow/models/blocks/filter-form/FilterFormItemModel.tsx +129 -14
  11. package/src/flow/models/blocks/filter-form/FilterFormSubmitActionModel.tsx +57 -4
  12. package/src/flow/models/blocks/filter-form/__tests__/FilterFormGridModel.onModelCreated.test.ts +174 -1
  13. package/src/flow/models/blocks/filter-form/__tests__/FilterFormItemModel.defineChildren.test.ts +359 -1
  14. package/src/flow/models/blocks/filter-form/__tests__/FilterFormItemModel.getFilterValue.test.ts +72 -0
  15. package/src/flow/models/blocks/filter-form/__tests__/FilterFormSubmitActionModel.lifecycle.test.ts +90 -0
  16. package/src/flow/models/blocks/form/FormBlockModel.tsx +43 -0
  17. package/src/flow/models/blocks/form/__tests__/runJsFormSubmit.test.ts +131 -0
  18. package/src/flow/models/fields/AssociationFieldModel/CascadeSelectFieldModel.tsx +33 -1
  19. package/src/flow/models/fields/mobile-components/MobileLazySelect.tsx +5 -0
  20. package/src/flow/models/fields/mobile-components/__tests__/MobileSelect.test.tsx +16 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/client-v2",
3
- "version": "2.1.38",
3
+ "version": "2.1.40",
4
4
  "license": "Apache-2.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.mjs",
@@ -27,11 +27,11 @@
27
27
  "@formily/antd-v5": "1.2.3",
28
28
  "@formily/react": "^2.2.27",
29
29
  "@formily/shared": "^2.2.27",
30
- "@nocobase/evaluators": "2.1.38",
31
- "@nocobase/flow-engine": "2.1.38",
32
- "@nocobase/sdk": "2.1.38",
33
- "@nocobase/shared": "2.1.38",
34
- "@nocobase/utils": "2.1.38",
30
+ "@nocobase/evaluators": "2.1.40",
31
+ "@nocobase/flow-engine": "2.1.40",
32
+ "@nocobase/sdk": "2.1.40",
33
+ "@nocobase/shared": "2.1.40",
34
+ "@nocobase/utils": "2.1.40",
35
35
  "ahooks": "^3.7.2",
36
36
  "antd": "5.24.2",
37
37
  "antd-style": "3.7.1",
@@ -48,5 +48,5 @@
48
48
  "react-i18next": "^11.15.1",
49
49
  "react-router-dom": "^6.30.1"
50
50
  },
51
- "gitHead": "cf73a1ede7f46029428d3ed143af2e5dc4f035e6"
51
+ "gitHead": "4099ac7037f46789c6eff98be54f20ae9fdde0e2"
52
52
  }
@@ -18,6 +18,7 @@ import {
18
18
  subFormFieldLinkageRules,
19
19
  subFormLinkageSetFieldProps,
20
20
  } from '../linkageRules';
21
+ import { CreateFormModel, EditFormModel, FormGridModel, FormItemModel, JSItemModel } from '../../models';
21
22
 
22
23
  const createSubFormFieldModel = ({
23
24
  uid,
@@ -1056,6 +1057,77 @@ describe('linkageSetFieldProps action', () => {
1056
1057
  expect(await screen.findByText('Published')).toBeTruthy();
1057
1058
  });
1058
1059
 
1060
+ it.each([
1061
+ ['create form', CreateFormModel, linkageSetFieldProps, false],
1062
+ ['edit form', EditFormModel, linkageSetFieldProps, false],
1063
+ ['to-one subform', FlowModel, subFormLinkageSetFieldProps, false],
1064
+ ['to-many subform row', FlowModel, subFormLinkageSetFieldProps, true],
1065
+ ])('should exclude JS items from %s field state options', async (_, ModelClass, action, useGridFork) => {
1066
+ const engine = new FlowEngine();
1067
+ engine.registerModels({
1068
+ CreateFormModel,
1069
+ EditFormModel,
1070
+ FormGridModel,
1071
+ FormItemModel,
1072
+ JSItemModel,
1073
+ });
1074
+ const model = new ModelClass({
1075
+ flowEngine: engine,
1076
+ uid: 'form-model',
1077
+ use: ModelClass.name,
1078
+ }) as FlowModel;
1079
+ const grid = engine.createModel<FormGridModel>({
1080
+ use: 'FormGridModel',
1081
+ uid: 'form-grid',
1082
+ });
1083
+ const fieldModel = engine.createModel<FormItemModel>({
1084
+ use: 'FormItemModel',
1085
+ uid: 'name-field',
1086
+ props: { label: 'Name' },
1087
+ subModels: {
1088
+ field: {
1089
+ use: 'FlowModel',
1090
+ uid: 'name-field-component',
1091
+ },
1092
+ },
1093
+ });
1094
+ const jsItemModel = engine.createModel<JSItemModel>({
1095
+ use: 'JSItemModel',
1096
+ uid: 'js-item',
1097
+ props: { label: 'JS item' },
1098
+ });
1099
+ Object.assign(model.subModels, {
1100
+ grid: useGridFork
1101
+ ? grid.createFork({}, 'row-0')
1102
+ : Object.assign(grid, {
1103
+ subModels: {
1104
+ ...grid.subModels,
1105
+ items: [fieldModel, jsItemModel],
1106
+ },
1107
+ }),
1108
+ });
1109
+ Object.assign(model.subModels.grid.subModels, {
1110
+ items: [fieldModel, jsItemModel],
1111
+ });
1112
+ const ctx = model.context;
1113
+ const Comp: any = action.uiSchema.value['x-component'];
1114
+ const view = render(
1115
+ React.createElement(
1116
+ FlowSettingsContextProvider,
1117
+ { value: ctx },
1118
+ React.createElement(Comp, {
1119
+ value: { fields: [] },
1120
+ onChange: () => {},
1121
+ }),
1122
+ ),
1123
+ );
1124
+
1125
+ fireEvent.mouseDown(view.container.querySelector('.ant-select-selector') as Element);
1126
+
1127
+ expect(await screen.findByText('Name')).toBeTruthy();
1128
+ expect(screen.queryByText('JS item')).toBeNull();
1129
+ });
1130
+
1059
1131
  it('should only show options state for supported single field selection', () => {
1060
1132
  const supportedField: any = {
1061
1133
  uid: 'status-field',
@@ -124,11 +124,16 @@ const isActionFlowModel = (model: any): boolean => {
124
124
  return false;
125
125
  };
126
126
 
127
+ const isFormFieldModel = (model: unknown): model is FlowModel => {
128
+ if (!model || typeof model !== 'object') return false;
129
+ return !!(model as FlowModel).subModels?.field;
130
+ };
131
+
127
132
  // 获取表单中所有字段的 model 实例的通用函数
128
133
  const getFormFields = (ctx: any) => {
129
134
  try {
130
135
  const fieldModels = ctx.model?.subModels?.grid?.subModels?.items || [];
131
- return fieldModels.map((model: any) => ({
136
+ return fieldModels.filter(isFormFieldModel).map((model) => ({
132
137
  label: model.props.label || model.props.name,
133
138
  value: model.uid,
134
139
  model,
@@ -350,8 +355,8 @@ const FieldStateEditor = ({
350
355
  const getFormFieldsByForkModel = (ctx: any) => {
351
356
  try {
352
357
  const fieldModels = ctx.model?.subModels?.grid?.subModels?.items || [];
353
- return fieldModels.map((model: any) => {
354
- const forkModel = Array.from(model.forks)[0] as any;
358
+ return fieldModels.filter(isFormFieldModel).map((model) => {
359
+ const forkModel = Array.from(model.forks)[0] as FlowModel | undefined;
355
360
 
356
361
  if (forkModel) {
357
362
  return {
@@ -59,11 +59,42 @@ export class FilterFormGridModel extends GridModel {
59
59
 
60
60
  readonly loading = observable.ref(false);
61
61
 
62
- private getAssociationFilterTargetKey(field: any) {
63
- const filterTargetKey = field?.targetCollection?.filterTargetKey;
62
+ private isRecordSelectFilterField(subModel: FilterFormItemModel) {
63
+ const fieldModel = subModel.subModels?.field;
64
+ if (!fieldModel) {
65
+ return true;
66
+ }
67
+
68
+ const RecordSelectModel = this.flowEngine.getModelClass('FilterFormRecordSelectFieldModel');
69
+ if (!RecordSelectModel) {
70
+ return fieldModel.constructor?.name === 'FilterFormRecordSelectFieldModel';
71
+ }
72
+
73
+ return fieldModel instanceof RecordSelectModel;
74
+ }
75
+
76
+ private getAssociationFilterTargetKey(field: any, subModel: FilterFormItemModel) {
77
+ const normalizeKey = (key: any) => {
78
+ if (Array.isArray(key)) {
79
+ return key[0];
80
+ }
81
+ return key;
82
+ };
83
+
84
+ const fieldModelValueKey = normalizeKey(subModel.subModels?.field?.props?.fieldNames?.value);
85
+ if (fieldModelValueKey) {
86
+ return fieldModelValueKey;
87
+ }
88
+
89
+ const filterTargetKey = normalizeKey(field?.targetCollection?.filterTargetKey);
90
+ const fieldTargetKey = normalizeKey(field?.targetKey);
91
+
92
+ if (filterTargetKey && filterTargetKey !== 'id') {
93
+ return filterTargetKey;
94
+ }
64
95
 
65
- if (Array.isArray(filterTargetKey)) {
66
- return filterTargetKey[0] || 'id';
96
+ if (!this.isRecordSelectFilterField(subModel) && fieldTargetKey) {
97
+ return fieldTargetKey;
67
98
  }
68
99
 
69
100
  return filterTargetKey || 'id';
@@ -370,7 +401,7 @@ export class FilterFormGridModel extends GridModel {
370
401
  if (field?.target) {
371
402
  return {
372
403
  targetId: model.uid,
373
- filterPaths: [`${fieldPath}.${this.getAssociationFilterTargetKey(field)}`],
404
+ filterPaths: [`${fieldPath}.${this.getAssociationFilterTargetKey(field, subModel)}`],
374
405
  };
375
406
  }
376
407
  }
@@ -31,6 +31,12 @@ import {
31
31
  import { FilterFormFieldModel } from './fields';
32
32
  import { normalizeFilterValueByOperator } from './valueNormalization';
33
33
 
34
+ type CollectionWithManager = Collection & {
35
+ collectionManager?: {
36
+ getCollection?: (name: string) => Collection | undefined;
37
+ };
38
+ };
39
+
34
40
  const getAssociationTargetCollection = (field: any, collection?: Collection, model?: CollectionBlockModel) => {
35
41
  if (field?.targetCollection) {
36
42
  return field.targetCollection;
@@ -42,6 +48,10 @@ const getAssociationTargetCollection = (field: any, collection?: Collection, mod
42
48
  if (collection?.dataSource?.getCollection) {
43
49
  return collection.dataSource.getCollection(targetName);
44
50
  }
51
+ const collectionManager = (collection as CollectionWithManager | undefined)?.collectionManager;
52
+ if (collectionManager?.getCollection) {
53
+ return collectionManager.getCollection(targetName);
54
+ }
45
55
  const dataSourceKey = collection?.dataSourceKey;
46
56
  if (dataSourceKey && model?.context?.dataSourceManager?.getCollection) {
47
57
  return model.context.dataSourceManager.getCollection(dataSourceKey, targetName);
@@ -168,6 +178,74 @@ const buildVirtualFilterCollectionField = (ctx: FlowModelContext, filterField: a
168
178
  return result;
169
179
  };
170
180
 
181
+ const getFilterableChildren = (field: any) => {
182
+ const children = field?.filterable?.children;
183
+ return Array.isArray(children) ? children : [];
184
+ };
185
+
186
+ const hasAssociationChildren = (field: any, collection?: Collection, model?: CollectionBlockModel) => {
187
+ return (
188
+ isAssociationCollectionField(field) &&
189
+ (getFilterableChildren(field).length > 0 || !!getAssociationTargetCollection(field, collection, model))
190
+ );
191
+ };
192
+
193
+ const inferFilterableChildInterface = (child: any) => {
194
+ if (child?.interface) {
195
+ return child.interface;
196
+ }
197
+
198
+ const schema = child?.schema || child?.uiSchema;
199
+ const component = schema?.['x-component'];
200
+ if (component === 'Select') {
201
+ return 'select';
202
+ }
203
+ if (component === 'Checkbox.Group') {
204
+ return 'checkboxGroup';
205
+ }
206
+ if (component === 'Checkbox') {
207
+ return 'checkbox';
208
+ }
209
+
210
+ const type = child?.type || schema?.type;
211
+ if (type === 'number' || type === 'integer') {
212
+ return 'number';
213
+ }
214
+ return 'input';
215
+ };
216
+
217
+ const translateFilterableChildTitle = (title: unknown, t: (value: string) => string) => {
218
+ return typeof title === 'string' ? t(title) : title;
219
+ };
220
+
221
+ const buildFilterableChildField = (child: any, t: (value: string) => string) => {
222
+ const name = child?.name || child?.value;
223
+ if (!name) {
224
+ return;
225
+ }
226
+
227
+ const schema = child?.schema || child?.uiSchema || {};
228
+ const rawTitle = child?.title || child?.label || schema?.title || name;
229
+ const title = translateFilterableChildTitle(rawTitle, t);
230
+ const filterable = child?.filterable || (child?.operators ? { operators: child.operators } : undefined);
231
+
232
+ return {
233
+ ...child,
234
+ name,
235
+ title,
236
+ type: child?.type || schema?.type || 'string',
237
+ interface: inferFilterableChildInterface(child),
238
+ uiSchema: {
239
+ ...schema,
240
+ title: translateFilterableChildTitle(schema?.title || title, t),
241
+ },
242
+ filterable,
243
+ __filterFormVirtual: true,
244
+ isAssociationField: () => false,
245
+ getComponentProps: () => schema?.['x-component-props'] || {},
246
+ };
247
+ };
248
+
171
249
  const buildFilterFormFieldItem = ({
172
250
  model,
173
251
  collection,
@@ -187,11 +265,7 @@ const buildFilterFormFieldItem = ({
187
265
  if (!binding) {
188
266
  return;
189
267
  }
190
- const isAssociation = isAssociationCollectionField(field);
191
- const fieldModel =
192
- isAssociation && ctxWithFlags.engine?.getModelClass?.('FilterFormRecordSelectFieldModel')
193
- ? 'FilterFormRecordSelectFieldModel'
194
- : binding.modelName;
268
+ const fieldModel = binding.modelName;
195
269
  const label = field.title || field.name;
196
270
  const displayLabel = labelPrefix ? `${labelPrefix} / ${label}` : label;
197
271
  return {
@@ -211,7 +285,10 @@ const buildFilterFormFieldItem = ({
211
285
  },
212
286
  filterFormItemSettings: {
213
287
  init: {
214
- filterField: _.pick(field, ['name', 'title', 'interface', 'type']),
288
+ filterField: {
289
+ ..._.pick(field, ['name', 'title', 'interface', 'type', 'filterable', 'uiSchema', '__filterFormVirtual']),
290
+ title: displayLabel,
291
+ },
215
292
  defaultTargetUid: model.uid,
216
293
  },
217
294
  },
@@ -245,7 +322,8 @@ const buildAssociationFieldMenuItem = ({
245
322
  depth: number;
246
323
  }) => {
247
324
  const targetCollection = getAssociationTargetCollection(field, collection, model);
248
- if (!targetCollection) {
325
+ const filterableChildren = getFilterableChildren(field);
326
+ if (!targetCollection && !filterableChildren.length) {
249
327
  return;
250
328
  }
251
329
  if (depth > MAX_ASSOCIATION_DEPTH) {
@@ -262,10 +340,35 @@ const buildAssociationFieldMenuItem = ({
262
340
  const targetFields = getTargetFilterableFields(field, collection, model);
263
341
  const fieldItems: any[] = [];
264
342
  const associationItems: any[] = [];
343
+ const addedFieldPaths = new Set<string>();
344
+
345
+ filterableChildren.forEach((child: any) => {
346
+ const virtualChildField = buildFilterableChildField(child, t);
347
+ if (!virtualChildField) {
348
+ return;
349
+ }
350
+
351
+ const childFieldPath = `${fieldPath}.${virtualChildField.name}`;
352
+ const childItem = buildFilterFormFieldItem({
353
+ model,
354
+ collection,
355
+ ctxWithFlags,
356
+ field: virtualChildField,
357
+ fieldPath: childFieldPath,
358
+ labelPrefix: label,
359
+ });
360
+ if (childItem) {
361
+ addedFieldPaths.add(childFieldPath);
362
+ fieldItems.push(childItem);
363
+ }
364
+ });
265
365
 
266
366
  targetFields.forEach((targetField: any) => {
267
367
  const targetFieldPath = `${fieldPath}.${targetField.name}`;
268
- if (targetField?.targetCollection) {
368
+ if (addedFieldPaths.has(targetFieldPath)) {
369
+ return;
370
+ }
371
+ if (hasAssociationChildren(targetField, collection, model)) {
269
372
  const associationItem = buildAssociationFieldMenuItem({
270
373
  model,
271
374
  collection,
@@ -358,7 +461,7 @@ const getModelFieldGroups = async (model: CollectionBlockModel) => {
358
461
  baseItems.push(baseItem);
359
462
  }
360
463
 
361
- if (field?.targetCollection) {
464
+ if (hasAssociationChildren(field, collection, model)) {
362
465
  const associationItem = buildAssociationFieldMenuItem({
363
466
  model,
364
467
  collection,
@@ -489,6 +592,14 @@ export class FilterFormItemModel extends FilterableItemModel<{
489
592
  title: initFilterField?.title || initFilterField?.name,
490
593
  },
491
594
  });
595
+ if (initFilterField?.__filterFormVirtual) {
596
+ const virtualField = buildVirtualFilterCollectionField(this.context, initFilterField);
597
+ if (virtualField) {
598
+ this.context.defineProperty('collectionField', {
599
+ value: virtualField,
600
+ });
601
+ }
602
+ }
492
603
  }
493
604
  }
494
605
 
@@ -692,7 +803,8 @@ FilterFormItemModel.registerFlow({
692
803
  // @ts-ignore
693
804
  (fieldSettingsInitParams?.dataSourceKey && fieldSettingsInitParams?.collectionName)
694
805
  );
695
- if (!hasCollectionContext && !collectionField && normalizedFilterField) {
806
+ const shouldUseVirtualFilterField = !!normalizedFilterField?.__filterFormVirtual;
807
+ if ((shouldUseVirtualFilterField || (!hasCollectionContext && !collectionField)) && normalizedFilterField) {
696
808
  const virtualField = buildVirtualFilterCollectionField(ctx, normalizedFilterField);
697
809
  if (virtualField) {
698
810
  ctx.model.context.defineProperty('collectionField', {
@@ -787,10 +899,13 @@ FilterFormItemModel.registerFlow({
787
899
  type: 'select',
788
900
  key: 'use',
789
901
  props: {
790
- options: classes.map((model) => ({
791
- label: t(model.modelName),
792
- value: model.modelName,
793
- })),
902
+ options: classes.map((model) => {
903
+ const ModelClass = ctx.engine.getModelClass(model.modelName);
904
+ return {
905
+ label: t(ModelClass?.meta?.label || model.modelName),
906
+ value: model.modelName,
907
+ };
908
+ }),
794
909
  },
795
910
  };
796
911
  },
@@ -8,23 +8,76 @@
8
8
  */
9
9
 
10
10
  import type { ButtonProps } from 'antd/es/button';
11
+ import { reaction } from '@formily/reactive';
11
12
  import { tExpr } from '@nocobase/flow-engine';
12
13
  import { FilterFormActionModel } from './FilterFormActionModel';
13
14
 
15
+ type FilterFormBlockState = {
16
+ autoTriggerFilter: boolean;
17
+ };
18
+
19
+ const mountedSubmitActions = new WeakSet<object>();
20
+ const visibleSubmitActionsByBlock = new WeakMap<FilterFormBlockState, Set<object>>();
21
+ const hiddenReactionDisposers = new WeakMap<object, () => void>();
22
+
23
+ const syncAutoTriggerFilter = (blockModel: FilterFormBlockState, lifecycleAction: object & { hidden?: boolean }) => {
24
+ let visibleSubmitActions = visibleSubmitActionsByBlock.get(blockModel);
25
+
26
+ if (mountedSubmitActions.has(lifecycleAction) && !lifecycleAction.hidden) {
27
+ if (!visibleSubmitActions) {
28
+ visibleSubmitActions = new Set<object>();
29
+ visibleSubmitActionsByBlock.set(blockModel, visibleSubmitActions);
30
+ }
31
+ visibleSubmitActions.add(lifecycleAction);
32
+ } else {
33
+ visibleSubmitActions?.delete(lifecycleAction);
34
+ }
35
+
36
+ blockModel.autoTriggerFilter = Boolean(!visibleSubmitActions?.size);
37
+ if (!visibleSubmitActions?.size) {
38
+ visibleSubmitActionsByBlock.delete(blockModel);
39
+ }
40
+ };
41
+
14
42
  export class FilterFormSubmitActionModel extends FilterFormActionModel {
15
43
  defaultProps: ButtonProps = {
16
44
  title: tExpr('Filter'),
17
45
  type: 'primary',
18
46
  };
19
47
 
20
- onInit(options): void {
21
- super.onInit(options);
22
- this.context.blockModel.autoTriggerFilter = false;
48
+ private getLifecycleAction(): object & { hidden?: boolean } {
49
+ return this.context.model || this;
50
+ }
51
+
52
+ private syncAutoTriggerFilter(): void {
53
+ const blockModel = this.context.blockModel as FilterFormBlockState;
54
+ const lifecycleAction = this.getLifecycleAction();
55
+ syncAutoTriggerFilter(blockModel, lifecycleAction);
56
+ }
57
+
58
+ onMount(): void {
59
+ super.onMount();
60
+ const blockModel = this.context.blockModel as FilterFormBlockState;
61
+ const lifecycleAction = this.getLifecycleAction();
62
+ mountedSubmitActions.add(lifecycleAction);
63
+ hiddenReactionDisposers.get(lifecycleAction)?.();
64
+ hiddenReactionDisposers.set(
65
+ lifecycleAction,
66
+ reaction(
67
+ () => lifecycleAction.hidden,
68
+ () => syncAutoTriggerFilter(blockModel, lifecycleAction),
69
+ ),
70
+ );
71
+ this.syncAutoTriggerFilter();
23
72
  }
24
73
 
25
74
  onUnmount(): void {
26
75
  super.onUnmount();
27
- this.context.blockModel.autoTriggerFilter = true;
76
+ const lifecycleAction = this.getLifecycleAction();
77
+ hiddenReactionDisposers.get(lifecycleAction)?.();
78
+ hiddenReactionDisposers.delete(lifecycleAction);
79
+ mountedSubmitActions.delete(lifecycleAction);
80
+ this.syncAutoTriggerFilter();
28
81
  }
29
82
  }
30
83