@nocobase/client-v2 3.0.0-alpha.7 → 3.0.0-alpha.8
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/flow/actions/linkageRules.d.ts +24 -0
- package/es/flow/components/FieldAssignValueInput.d.ts +4 -30
- package/es/flow/components/field-value-variable/DateVariableEditor.d.ts +24 -0
- package/es/flow/components/field-value-variable/FieldValueVariableInput.d.ts +31 -0
- package/es/flow/components/field-value-variable/dateValue.d.ts +36 -0
- package/es/flow/components/field-value-variable/index.d.ts +11 -0
- package/es/flow/models/blocks/form/FormBlockModel.d.ts +6 -0
- package/es/flow/models/blocks/form/FormGridModel.d.ts +6 -0
- package/es/flow/models/blocks/form/value-runtime/rules.d.ts +1 -0
- package/es/index.mjs +101 -110
- package/lib/index.js +93 -102
- package/package.json +7 -7
- package/src/__tests__/app.test.tsx +15 -6
- package/src/components/AppComponents.tsx +4 -4
- package/src/flow/actions/__tests__/linkageRules.actionStates.test.ts +33 -0
- package/src/flow/actions/linkageRules.tsx +25 -7
- package/src/flow/components/FieldAssignValueInput.tsx +38 -621
- package/src/flow/components/field-value-variable/DateVariableEditor.tsx +181 -0
- package/src/flow/components/field-value-variable/FieldValueVariableInput.tsx +326 -0
- package/src/flow/components/field-value-variable/__tests__/FieldValueVariableInput.test.tsx +380 -0
- package/src/flow/components/field-value-variable/dateValue.ts +223 -0
- package/src/flow/components/field-value-variable/index.ts +12 -0
- package/src/flow/models/blocks/assign-form/AssignFormItemModel.tsx +28 -53
- package/src/flow/models/blocks/form/FormBlockModel.tsx +4 -0
- package/src/flow/models/blocks/form/FormGridModel.tsx +4 -0
- package/src/flow/models/blocks/form/value-runtime/__tests__/runtime.test.ts +435 -0
- package/src/flow/models/blocks/form/value-runtime/rules.ts +67 -14
- package/src/flow/models/blocks/form/value-runtime/runtime.ts +43 -19
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/PopupSubTableFieldModel.tsx +8 -0
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/__tests__/popupContext.test.ts +120 -0
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/actions/PopupSubTableEditActionModel.tsx +10 -2
- package/src/flow/models/fields/mobile-components/MobileLazySelect.tsx +1 -0
- package/src/flow/models/fields/mobile-components/__tests__/MobileSelect.test.tsx +20 -2
|
@@ -8,7 +8,13 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { isObservable, reaction, toJS } from '@formily/reactive';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
FlowContext,
|
|
13
|
+
FlowModel,
|
|
14
|
+
isRunJSValue,
|
|
15
|
+
normalizeRunJSValue,
|
|
16
|
+
type ResolveJsonTemplateOptions,
|
|
17
|
+
} from '@nocobase/flow-engine';
|
|
12
18
|
import { getValuesByPath } from '@nocobase/shared';
|
|
13
19
|
import _ from 'lodash';
|
|
14
20
|
import { dayjs } from '@nocobase/utils/client';
|
|
@@ -49,6 +55,7 @@ type RuntimeRule = {
|
|
|
49
55
|
getValue: () => any;
|
|
50
56
|
getCondition?: () => any;
|
|
51
57
|
getContext: () => any;
|
|
58
|
+
getContractModelUid?: () => string | undefined;
|
|
52
59
|
};
|
|
53
60
|
|
|
54
61
|
type ObservableBinding = {
|
|
@@ -83,8 +90,18 @@ type ToManyAggregateSourceInfo = {
|
|
|
83
90
|
lastWrite?: FormValueWriteMeta;
|
|
84
91
|
};
|
|
85
92
|
|
|
93
|
+
type RuntimeItemChain = {
|
|
94
|
+
index?: number;
|
|
95
|
+
length?: number;
|
|
96
|
+
__is_new__?: boolean;
|
|
97
|
+
__is_stored__?: boolean;
|
|
98
|
+
value: unknown;
|
|
99
|
+
parentItem?: RuntimeItemChain;
|
|
100
|
+
};
|
|
101
|
+
|
|
86
102
|
export type RuleEngineOptions = {
|
|
87
103
|
getBlockModelUid: () => string;
|
|
104
|
+
getAssignRulesModelUid: () => string | undefined;
|
|
88
105
|
getActionName: () => string | undefined;
|
|
89
106
|
getBlockContext: () => any;
|
|
90
107
|
getEngine: () => any;
|
|
@@ -208,6 +225,7 @@ export class RuleEngine {
|
|
|
208
225
|
getValue: () => template?.value,
|
|
209
226
|
getCondition: () => template?.condition,
|
|
210
227
|
getContext: () => this.options.getBlockContext(),
|
|
228
|
+
getContractModelUid: this.options.getAssignRulesModelUid,
|
|
211
229
|
};
|
|
212
230
|
|
|
213
231
|
this.rules.set(id, { rule, state: { deps: new Set(), depDisposers: [], runSeq: 0, scheduledAtWriteSeq: 0 } });
|
|
@@ -545,6 +563,7 @@ export class RuleEngine {
|
|
|
545
563
|
getValue: () => template?.value,
|
|
546
564
|
getCondition: () => template?.condition,
|
|
547
565
|
getContext: () => this.options.getBlockContext(),
|
|
566
|
+
getContractModelUid: this.options.getAssignRulesModelUid,
|
|
548
567
|
};
|
|
549
568
|
|
|
550
569
|
this.rules.set(id, { rule, state: { deps: new Set(), depDisposers: [], runSeq: 0, scheduledAtWriteSeq: 0 } });
|
|
@@ -913,6 +932,7 @@ export class RuleEngine {
|
|
|
913
932
|
getValue: () => template?.value,
|
|
914
933
|
getCondition: () => template?.condition,
|
|
915
934
|
getContext: () => model?.context,
|
|
935
|
+
getContractModelUid: this.options.getAssignRulesModelUid,
|
|
916
936
|
};
|
|
917
937
|
|
|
918
938
|
this.rules.set(id, { rule, state: { deps: new Set(), depDisposers: [], runSeq: 0, scheduledAtWriteSeq: 0 } });
|
|
@@ -1662,7 +1682,7 @@ export class RuleEngine {
|
|
|
1662
1682
|
}
|
|
1663
1683
|
}
|
|
1664
1684
|
|
|
1665
|
-
const evalCtx = this.createRuleEvaluationContext(baseCtx, collector, targetNamePath);
|
|
1685
|
+
const evalCtx = this.createRuleEvaluationContext(baseCtx, collector, targetNamePath, rule.getContractModelUid?.());
|
|
1666
1686
|
return { collector, evalCtx, rawValue, isRunJS };
|
|
1667
1687
|
}
|
|
1668
1688
|
|
|
@@ -2001,7 +2021,12 @@ export class RuleEngine {
|
|
|
2001
2021
|
return canOverwrite;
|
|
2002
2022
|
}
|
|
2003
2023
|
|
|
2004
|
-
private createRuleEvaluationContext(
|
|
2024
|
+
private createRuleEvaluationContext(
|
|
2025
|
+
baseCtx: any,
|
|
2026
|
+
collector: DepCollector,
|
|
2027
|
+
targetNamePath: NamePath | null,
|
|
2028
|
+
contractModelUid?: string,
|
|
2029
|
+
) {
|
|
2005
2030
|
const trackingFormValues = this.options.createTrackingFormValues(collector);
|
|
2006
2031
|
const ctx: any = new FlowContext();
|
|
2007
2032
|
try {
|
|
@@ -2021,12 +2046,19 @@ export class RuleEngine {
|
|
|
2021
2046
|
}
|
|
2022
2047
|
|
|
2023
2048
|
const delegatedResolveJsonTemplate =
|
|
2024
|
-
typeof ctx.resolveJsonTemplate === 'function'
|
|
2049
|
+
typeof ctx.resolveJsonTemplate === 'function'
|
|
2050
|
+
? (ctx.resolveJsonTemplate.bind(ctx) as (
|
|
2051
|
+
template: unknown,
|
|
2052
|
+
options?: ResolveJsonTemplateOptions,
|
|
2053
|
+
) => Promise<unknown>)
|
|
2054
|
+
: undefined;
|
|
2025
2055
|
ctx.defineMethod('resolveJsonTemplate', async (template: unknown) => {
|
|
2026
2056
|
const tokenStore = this.createLocalTemplateTokenStore();
|
|
2027
2057
|
const localResolved = this.resolveLocalFormValuesTemplates(baseCtx, template, collector, tokenStore);
|
|
2028
2058
|
const nextTemplate = localResolved.matched ? localResolved.value : template;
|
|
2029
|
-
const resolved = delegatedResolveJsonTemplate
|
|
2059
|
+
const resolved = delegatedResolveJsonTemplate
|
|
2060
|
+
? await delegatedResolveJsonTemplate(nextTemplate, contractModelUid ? { contractModelUid } : undefined)
|
|
2061
|
+
: nextTemplate;
|
|
2030
2062
|
return this.restoreLocalTemplateTokens(resolved, tokenStore);
|
|
2031
2063
|
});
|
|
2032
2064
|
|
|
@@ -2038,7 +2070,7 @@ export class RuleEngine {
|
|
|
2038
2070
|
// - parentItem:上级项(同结构,可链式 parentItem.parentItem...)
|
|
2039
2071
|
// 计算顺序:
|
|
2040
2072
|
// 1) 优先按 targetNamePath 从 formValues 构建“关联链 item”
|
|
2041
|
-
// 2)
|
|
2073
|
+
// 2) 若无法构建(例如目标字段是顶层非关联字段),回退到上游显式注入的 baseCtx.item
|
|
2042
2074
|
// (如 PopupSubTable 新增弹窗传入的 parentItem 链)
|
|
2043
2075
|
let itemCached: any;
|
|
2044
2076
|
let itemCachedReady = false;
|
|
@@ -2209,9 +2241,23 @@ export class RuleEngine {
|
|
|
2209
2241
|
parentItem,
|
|
2210
2242
|
};
|
|
2211
2243
|
};
|
|
2212
|
-
const
|
|
2213
|
-
|
|
2214
|
-
|
|
2244
|
+
const blockCtx = this.options.getBlockContext();
|
|
2245
|
+
const formRootItem = (() => {
|
|
2246
|
+
try {
|
|
2247
|
+
const item = blockCtx?.item as RuntimeItemChain | undefined;
|
|
2248
|
+
if (!item || typeof item !== 'object') return undefined;
|
|
2249
|
+
return item.value === blockCtx?.formValues ? item : undefined;
|
|
2250
|
+
} catch {
|
|
2251
|
+
return undefined;
|
|
2252
|
+
}
|
|
2253
|
+
})();
|
|
2254
|
+
const defaultRoot = {
|
|
2255
|
+
...buildNode(trackingFormValues, formRootItem?.index, formRootItem?.length, formRootItem?.parentItem),
|
|
2256
|
+
__is_new__: formRootItem?.__is_new__ ?? trackingFormValues?.__is_new__,
|
|
2257
|
+
__is_stored__: formRootItem?.__is_stored__ ?? trackingFormValues?.__is_stored__,
|
|
2258
|
+
};
|
|
2259
|
+
// item 用于“关系字段目标或其子路径”场景;
|
|
2260
|
+
// 顶层非关联字段/非关联嵌套对象字段应使用 formValues。
|
|
2215
2261
|
if (!targetNamePath || !Array.isArray(targetNamePath) || !targetNamePath.length) return undefined;
|
|
2216
2262
|
if (!rootCollection?.getField) return undefined;
|
|
2217
2263
|
|
|
@@ -2219,7 +2265,9 @@ export class RuleEngine {
|
|
|
2219
2265
|
const prefix: NamePath = [];
|
|
2220
2266
|
let collection = rootCollection;
|
|
2221
2267
|
|
|
2222
|
-
|
|
2268
|
+
// The target itself can be an association. Keep that final association in the item chain so
|
|
2269
|
+
// ctx.item.parentItem continues to point at the containing row instead of skipping directly to the form root.
|
|
2270
|
+
for (let i = 0; i < targetNamePath.length; i++) {
|
|
2223
2271
|
const seg = targetNamePath[i];
|
|
2224
2272
|
if (typeof seg !== 'string') break;
|
|
2225
2273
|
|
|
@@ -2231,9 +2279,12 @@ export class RuleEngine {
|
|
|
2231
2279
|
|
|
2232
2280
|
if (toMany) {
|
|
2233
2281
|
const next = targetNamePath[i + 1];
|
|
2234
|
-
if (typeof next
|
|
2235
|
-
|
|
2236
|
-
|
|
2282
|
+
if (typeof next === 'number') {
|
|
2283
|
+
prefix.push(next);
|
|
2284
|
+
i += 1;
|
|
2285
|
+
} else if (i !== targetNamePath.length - 1) {
|
|
2286
|
+
break;
|
|
2287
|
+
}
|
|
2237
2288
|
}
|
|
2238
2289
|
|
|
2239
2290
|
const targetCollection = field?.targetCollection;
|
|
@@ -2248,9 +2299,11 @@ export class RuleEngine {
|
|
|
2248
2299
|
const assocEntry = assocEntries[idx];
|
|
2249
2300
|
const value = _.get(trackingFormValues, assocEntry.path);
|
|
2250
2301
|
const lastSeg = assocEntry.path[assocEntry.path.length - 1];
|
|
2251
|
-
const
|
|
2302
|
+
const isIndexedToManyItem = assocEntry.toMany && typeof lastSeg === 'number';
|
|
2303
|
+
const index = isIndexedToManyItem ? lastSeg : undefined;
|
|
2252
2304
|
const length = (() => {
|
|
2253
2305
|
if (!assocEntry.toMany) return undefined;
|
|
2306
|
+
if (!isIndexedToManyItem) return Array.isArray(value) ? value.length : undefined;
|
|
2254
2307
|
// assocEntry.path: [..., associationKey, rowIndex]
|
|
2255
2308
|
const listPath = assocEntry.path.slice(0, -1);
|
|
2256
2309
|
const list = _.get(trackingFormValues, listPath);
|
|
@@ -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:
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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 || {})
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
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,
|
|
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 =
|
|
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,
|
|
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,
|
|
1168
|
-
_.set(this.valuesMirror, namePath, this.toMirrorSnapshot(
|
|
1169
|
-
this.syncMountedFieldModelValue(namePath,
|
|
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--;
|
|
@@ -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('>', '>');
|
|
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: {
|
|
@@ -158,8 +158,15 @@ vi.mock('antd-mobile', () => {
|
|
|
158
158
|
mockState.popupProps = props;
|
|
159
159
|
return props.visible ? <div data-testid="popup">{props.children}</div> : null;
|
|
160
160
|
},
|
|
161
|
-
SearchBar: ({ value, onChange }: any) => (
|
|
162
|
-
<
|
|
161
|
+
SearchBar: ({ value, onChange, onCancel, cancelText, showCancelButton }: any) => (
|
|
162
|
+
<div>
|
|
163
|
+
<input data-testid="search" value={value ?? ''} onChange={(e) => onChange?.(e.target.value)} />
|
|
164
|
+
{showCancelButton && value ? (
|
|
165
|
+
<button type="button" onClick={onCancel}>
|
|
166
|
+
{cancelText ?? '取消'}
|
|
167
|
+
</button>
|
|
168
|
+
) : null}
|
|
169
|
+
</div>
|
|
163
170
|
),
|
|
164
171
|
CheckList: MockCheckList,
|
|
165
172
|
};
|
|
@@ -289,6 +296,17 @@ describe('MobileLazySelect', () => {
|
|
|
289
296
|
resetMockState();
|
|
290
297
|
});
|
|
291
298
|
|
|
299
|
+
it('renders a translated cancel action after searching', () => {
|
|
300
|
+
renderMobileLazySelect();
|
|
301
|
+
|
|
302
|
+
openLazyPopup();
|
|
303
|
+
act(() => {
|
|
304
|
+
fireEvent.change(screen.getByTestId('search'), { target: { value: '11' } });
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
|
|
308
|
+
});
|
|
309
|
+
|
|
292
310
|
it('keeps pending relation records selected until confirm', () => {
|
|
293
311
|
const { onChange, rerender } = renderMobileLazySelect();
|
|
294
312
|
|