@nocobase/client-v2 2.1.36 → 2.1.38
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 +2 -23
- package/es/flow/components/field-value-variable/DateVariableEditor.d.ts +23 -0
- package/es/flow/components/field-value-variable/FieldValueVariableInput.d.ts +28 -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 +65 -74
- package/lib/index.js +92 -101
- package/package.json +7 -7
- 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 +34 -571
- package/src/flow/components/field-value-variable/DateVariableEditor.tsx +162 -0
- package/src/flow/components/field-value-variable/FieldValueVariableInput.tsx +306 -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/__tests__/FormBlockModel.test.tsx +8 -2
- package/src/flow/models/blocks/form/value-runtime/__tests__/runtime.test.ts +435 -0
- package/src/flow/models/blocks/form/value-runtime/rules.ts +68 -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/AssociationFieldModel/__tests__/RecordPickerFieldModel.itemContext.test.ts +46 -0
- package/src/flow/models/fields/mobile-components/MobileLazySelect.tsx +1 -0
- package/src/flow/models/fields/mobile-components/__tests__/MobileSelect.test.tsx +20 -2
|
@@ -10,23 +10,20 @@
|
|
|
10
10
|
import React from 'react';
|
|
11
11
|
import { Input } from 'antd';
|
|
12
12
|
import { define, observable } from '@formily/reactive';
|
|
13
|
-
import {
|
|
14
|
-
FlowModelRenderer,
|
|
15
|
-
FormItem,
|
|
16
|
-
VariableInput,
|
|
17
|
-
tExpr,
|
|
18
|
-
isVariableExpression,
|
|
19
|
-
parseValueToPath,
|
|
20
|
-
isRunJSValue,
|
|
21
|
-
EditableItemModel,
|
|
22
|
-
jioToJoiSchema,
|
|
23
|
-
} from '@nocobase/flow-engine';
|
|
13
|
+
import { FlowModelRenderer, FormItem, tExpr, EditableItemModel, jioToJoiSchema } from '@nocobase/flow-engine';
|
|
24
14
|
// 无需类型导入(避免未使用的类型)
|
|
25
15
|
import { FormItemModel } from '../form/FormItemModel';
|
|
26
16
|
import { EditFormModel } from '../form/EditFormModel';
|
|
27
17
|
import { customAlphabet as Alphabet } from 'nanoid';
|
|
28
18
|
import { ensureOptionsFromUiSchemaEnumIfAbsent } from '../../../internal/utils/enumOptionsUtils';
|
|
29
19
|
import { RunJSValueEditor } from '../../../components/RunJSValueEditor';
|
|
20
|
+
import {
|
|
21
|
+
DEFAULT_DATE_VARIABLE_COMPONENT_PROPS,
|
|
22
|
+
FieldValueVariableInput,
|
|
23
|
+
getFieldInterface,
|
|
24
|
+
isDateLikeField,
|
|
25
|
+
resolveDateVariableComponentProps,
|
|
26
|
+
} from '../../../components/field-value-variable';
|
|
30
27
|
|
|
31
28
|
type AssignFormTempOriginField = {
|
|
32
29
|
uid?: string;
|
|
@@ -285,7 +282,9 @@ export class AssignFormItemModel extends FormItemModel {
|
|
|
285
282
|
) : null;
|
|
286
283
|
};
|
|
287
284
|
|
|
288
|
-
const NullComponent: React.FC = () =>
|
|
285
|
+
const NullComponent: React.FC = () => (
|
|
286
|
+
<Input placeholder={`<${this.context.t?.('Null') ?? 'Null'}>`} readOnly style={{ width: '100%' }} />
|
|
287
|
+
);
|
|
289
288
|
|
|
290
289
|
const RunJSComponent: React.FC<any> = (inputProps: any) => {
|
|
291
290
|
return (
|
|
@@ -298,46 +297,18 @@ export class AssignFormItemModel extends FormItemModel {
|
|
|
298
297
|
);
|
|
299
298
|
};
|
|
300
299
|
|
|
301
|
-
const
|
|
302
|
-
renderInputComponent: (meta: any) => {
|
|
303
|
-
const firstPath = meta?.paths?.[0];
|
|
304
|
-
if (firstPath === 'constant') return ConstantValueEditor as any;
|
|
305
|
-
if (firstPath === 'null') return NullComponent as any;
|
|
306
|
-
if (firstPath === 'runjs') return RunJSComponent as any;
|
|
307
|
-
return undefined;
|
|
308
|
-
},
|
|
309
|
-
resolveValueFromPath: (item: any) => {
|
|
310
|
-
const firstPath = item?.paths?.[0];
|
|
311
|
-
if (firstPath === 'constant') return '';
|
|
312
|
-
if (firstPath === 'null') return null;
|
|
313
|
-
if (firstPath === 'runjs') return { code: '', version: 'v2' };
|
|
314
|
-
return undefined;
|
|
315
|
-
},
|
|
316
|
-
resolvePathFromValue: (currentValue: any) => {
|
|
317
|
-
if (currentValue === null) return ['null'];
|
|
318
|
-
if (isRunJSValue(currentValue)) return ['runjs'];
|
|
319
|
-
return isVariableExpression(currentValue) ? parseValueToPath(currentValue) : ['constant'];
|
|
320
|
-
},
|
|
321
|
-
} as any;
|
|
322
|
-
|
|
323
|
-
// 合并变量树:在最前面追加“常量/空值”两个选项
|
|
324
|
-
const mergedMetaTree = async () => {
|
|
300
|
+
const baseMetaTree = async () => {
|
|
325
301
|
const getTree = (this.context as any)?.getPropertyMetaTree;
|
|
326
|
-
|
|
327
|
-
return [
|
|
328
|
-
{
|
|
329
|
-
title: tExpr('Constant'),
|
|
330
|
-
name: 'constant',
|
|
331
|
-
type: 'string',
|
|
332
|
-
paths: ['constant'],
|
|
333
|
-
render: ConstantValueEditor,
|
|
334
|
-
},
|
|
335
|
-
{ title: tExpr('Null'), name: 'null', type: 'object', paths: ['null'], render: NullComponent },
|
|
336
|
-
{ title: tExpr('RunJS'), name: 'runjs', type: 'object', paths: ['runjs'], render: RunJSComponent },
|
|
337
|
-
...base,
|
|
338
|
-
];
|
|
302
|
+
return typeof getTree === 'function' ? await getTree() : [];
|
|
339
303
|
};
|
|
340
304
|
|
|
305
|
+
const targetCollectionField = collection?.getField?.(this.fieldPath);
|
|
306
|
+
const targetFieldInterface = getFieldInterface(targetCollectionField);
|
|
307
|
+
const dateLike = isDateLikeField(targetCollectionField, this.fieldPath?.split('.').slice(-1)[0]);
|
|
308
|
+
const dateComponentProps = dateLike
|
|
309
|
+
? resolveDateVariableComponentProps(targetCollectionField, targetFieldInterface)
|
|
310
|
+
: DEFAULT_DATE_VARIABLE_COMPONENT_PROPS;
|
|
311
|
+
|
|
341
312
|
// 计算 label:优先使用配置中的 label,其次集合字段标题,最后回退字段路径
|
|
342
313
|
let labelText = this.props?.label ?? this.fieldPath ?? '';
|
|
343
314
|
const cf = collection?.getField?.(this.fieldPath);
|
|
@@ -357,14 +328,18 @@ export class AssignFormItemModel extends FormItemModel {
|
|
|
357
328
|
const formValue = formBindingProps?.__assign_value__;
|
|
358
329
|
const mergedValue = typeof formValue === 'undefined' ? this.assignValue : formValue;
|
|
359
330
|
return (
|
|
360
|
-
<
|
|
331
|
+
<FieldValueVariableInput
|
|
361
332
|
value={mergedValue}
|
|
362
|
-
onChange={(v
|
|
333
|
+
onChange={(v) => {
|
|
363
334
|
this.assignValue = v;
|
|
364
335
|
formBindingProps?.__assign_trigger__?.(v);
|
|
365
336
|
}}
|
|
366
|
-
|
|
367
|
-
|
|
337
|
+
baseMetaTree={baseMetaTree}
|
|
338
|
+
constantComponent={ConstantValueEditor}
|
|
339
|
+
nullComponent={NullComponent}
|
|
340
|
+
runJSComponent={RunJSComponent}
|
|
341
|
+
isDateLikeField={dateLike}
|
|
342
|
+
dateComponentProps={dateComponentProps}
|
|
368
343
|
clearValue={''}
|
|
369
344
|
/>
|
|
370
345
|
);
|
|
@@ -77,6 +77,10 @@ export class FormBlockModel<
|
|
|
77
77
|
> extends CollectionBlockModel<T> {
|
|
78
78
|
formValueRuntime?: FormValueRuntime;
|
|
79
79
|
|
|
80
|
+
serialize() {
|
|
81
|
+
return { ...super.serialize(), variableContractType: { type: 'form', use: this.use } };
|
|
82
|
+
}
|
|
83
|
+
|
|
80
84
|
private userModifiedTopLevelFields = new Set<string>();
|
|
81
85
|
|
|
82
86
|
get form() {
|
|
@@ -21,6 +21,10 @@ export type DefaultFormGridStructure = {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
export class FormGridModel<T extends DefaultFormGridStructure = DefaultFormGridStructure> extends GridModel<T> {
|
|
24
|
+
serialize() {
|
|
25
|
+
return { ...super.serialize(), variableContractType: { type: 'formGrid', use: this.use } };
|
|
26
|
+
}
|
|
27
|
+
|
|
24
28
|
itemFallback = (<Skeleton.Input block size="small" style={{ marginBottom: '0.5rem' }} />);
|
|
25
29
|
itemSettingsMenuLevel = 2;
|
|
26
30
|
itemFlowSettings = {
|
|
@@ -465,11 +465,17 @@ describe('FormBlockModel (form/formValues injection & server resolve anchors)',
|
|
|
465
465
|
|
|
466
466
|
it('builds non-empty contextParams for ctx.formValues.* deep association path', async () => {
|
|
467
467
|
const model = await setupFormModel();
|
|
468
|
+
const sessionPayload = Buffer.from(JSON.stringify({ userId: 1, signInTime: 'form-record-slots' })).toString(
|
|
469
|
+
'base64url',
|
|
470
|
+
);
|
|
468
471
|
// 注入 api mock 到引擎上下文,拦截 variables:resolve 的请求
|
|
469
472
|
const api = {
|
|
473
|
+
auth: { token: `test.${sessionPayload}.sig` },
|
|
470
474
|
request: vi.fn(async (config: any) => {
|
|
471
|
-
const
|
|
472
|
-
const batch =
|
|
475
|
+
const requestValues = config?.data?.values || {};
|
|
476
|
+
const batch = requestValues.batch || [];
|
|
477
|
+
expect(batch[0]?.rd).toEqual(expect.any(String));
|
|
478
|
+
expect(batch[0]?.template).toEqual({ who: '{{ ctx.formValues.assignees.org.name }}' });
|
|
473
479
|
const cp = batch[0]?.contextParams || {};
|
|
474
480
|
const keys = Object.keys(cp).sort();
|
|
475
481
|
// 聚合为单键,不再使用索引键
|
|
@@ -445,6 +445,43 @@ describe('FormValueRuntime (default rules)', () => {
|
|
|
445
445
|
});
|
|
446
446
|
|
|
447
447
|
describe('FormValueRuntime (form assign rules)', () => {
|
|
448
|
+
it('uses the form grid as the variable contract owner', async () => {
|
|
449
|
+
const engineEmitter = new EventEmitter();
|
|
450
|
+
const formStub = createFormStub({ b: 'licensed' });
|
|
451
|
+
const onResolveJsonTemplate = vi.fn();
|
|
452
|
+
const blockModel: any = {
|
|
453
|
+
uid: 'form-contract-owner',
|
|
454
|
+
subModels: { grid: { uid: 'form-contract-owner-grid' } },
|
|
455
|
+
flowEngine: { emitter: engineEmitter },
|
|
456
|
+
emitter: new EventEmitter(),
|
|
457
|
+
dispatchEvent: vi.fn(),
|
|
458
|
+
getAclActionName: () => 'create',
|
|
459
|
+
};
|
|
460
|
+
const runtime = new FormValueRuntime({ model: blockModel, getForm: () => formStub as any });
|
|
461
|
+
const blockCtx = createFieldContext(runtime);
|
|
462
|
+
const resolveJsonTemplate = blockCtx.resolveJsonTemplate.bind(blockCtx);
|
|
463
|
+
blockCtx.defineMethod('resolveJsonTemplate', async (template: unknown, options?: unknown) => {
|
|
464
|
+
onResolveJsonTemplate(options);
|
|
465
|
+
return resolveJsonTemplate(template, options);
|
|
466
|
+
});
|
|
467
|
+
blockModel.context = blockCtx;
|
|
468
|
+
runtime.mount({ sync: true });
|
|
469
|
+
|
|
470
|
+
runtime.syncAssignRules([
|
|
471
|
+
{
|
|
472
|
+
key: 'license-version',
|
|
473
|
+
enable: true,
|
|
474
|
+
targetPath: 'a',
|
|
475
|
+
mode: 'assign',
|
|
476
|
+
condition: { logic: '$and', items: [] },
|
|
477
|
+
value: '__B__',
|
|
478
|
+
},
|
|
479
|
+
]);
|
|
480
|
+
|
|
481
|
+
await waitFor(() => expect(formStub.getFieldValue(['a'])).toBe('licensed'));
|
|
482
|
+
expect(onResolveJsonTemplate).toHaveBeenCalledWith({ contractModelUid: 'form-contract-owner-grid' });
|
|
483
|
+
});
|
|
484
|
+
|
|
448
485
|
it('migrates block-level rule to field instance on mount and restores on unmount', async () => {
|
|
449
486
|
const engineEmitter = new EventEmitter();
|
|
450
487
|
const blockEmitter = new EventEmitter();
|
|
@@ -3999,6 +4036,404 @@ describe('FormValueRuntime (form assign rules)', () => {
|
|
|
3999
4036
|
expect(formStub.getFieldValue(['users', 0, 'user', 'id'])).toBe(1);
|
|
4000
4037
|
});
|
|
4001
4038
|
|
|
4039
|
+
it('assigns a to-one association from a sibling association in the same to-many row', async () => {
|
|
4040
|
+
const engineEmitter = new EventEmitter();
|
|
4041
|
+
const blockEmitter = new EventEmitter();
|
|
4042
|
+
const secondary = { id: 2, name: 'Secondary' };
|
|
4043
|
+
const formStub = createFormStub({ users: [{ user: null, secondaryUser: null }] });
|
|
4044
|
+
|
|
4045
|
+
const blockModel: any = {
|
|
4046
|
+
uid: 'form-assign-assoc-to-many-sibling',
|
|
4047
|
+
flowEngine: { emitter: engineEmitter },
|
|
4048
|
+
emitter: blockEmitter,
|
|
4049
|
+
dispatchEvent: vi.fn(),
|
|
4050
|
+
getAclActionName: () => 'create',
|
|
4051
|
+
};
|
|
4052
|
+
|
|
4053
|
+
const runtime = new FormValueRuntime({ model: blockModel, getForm: () => formStub as any });
|
|
4054
|
+
runtime.mount({ sync: true });
|
|
4055
|
+
|
|
4056
|
+
const blockCtx = createFieldContext(runtime);
|
|
4057
|
+
const userCollection: any = { getField: () => null };
|
|
4058
|
+
const userField: any = { isAssociationField: () => true, type: 'belongsTo', targetCollection: userCollection };
|
|
4059
|
+
const usersItemCollection: any = {
|
|
4060
|
+
getField: (name: string) => (name === 'user' || name === 'secondaryUser' ? userField : null),
|
|
4061
|
+
};
|
|
4062
|
+
const usersField: any = { isAssociationField: () => true, type: 'hasMany', targetCollection: usersItemCollection };
|
|
4063
|
+
const collection: any = { getField: (name: string) => (name === 'users' ? usersField : null) };
|
|
4064
|
+
blockCtx.defineProperty('collection', { value: collection });
|
|
4065
|
+
|
|
4066
|
+
const rowModel: any = {
|
|
4067
|
+
uid: 'users.user:users:0',
|
|
4068
|
+
isFork: true,
|
|
4069
|
+
forkId: 'users:0',
|
|
4070
|
+
subModels: { field: { context: { collectionField: userField } } },
|
|
4071
|
+
getStepParams(flowKey: string, stepKey: string) {
|
|
4072
|
+
if (flowKey === 'fieldSettings' && stepKey === 'init') return { fieldPath: 'users.user' };
|
|
4073
|
+
return undefined;
|
|
4074
|
+
},
|
|
4075
|
+
};
|
|
4076
|
+
const rowCtx = createFieldContext(runtime);
|
|
4077
|
+
rowCtx.defineProperty('blockModel', { value: blockModel });
|
|
4078
|
+
rowCtx.defineProperty('collection', { value: collection });
|
|
4079
|
+
rowCtx.defineProperty('fieldIndex', { value: ['users:0'] });
|
|
4080
|
+
rowCtx.defineProperty('model', { value: rowModel });
|
|
4081
|
+
rowModel.context = rowCtx;
|
|
4082
|
+
|
|
4083
|
+
blockCtx.defineProperty('engine', {
|
|
4084
|
+
value: { forEachModel: (callback: (model: unknown) => void) => callback(rowModel) },
|
|
4085
|
+
});
|
|
4086
|
+
blockModel.context = blockCtx;
|
|
4087
|
+
|
|
4088
|
+
runtime.syncAssignRules([
|
|
4089
|
+
{
|
|
4090
|
+
key: 'r1',
|
|
4091
|
+
enable: true,
|
|
4092
|
+
targetPath: 'users.user',
|
|
4093
|
+
mode: 'assign',
|
|
4094
|
+
condition: { logic: '$and', items: [] },
|
|
4095
|
+
value: '{{ ctx.item.parentItem.value.secondaryUser }}',
|
|
4096
|
+
},
|
|
4097
|
+
]);
|
|
4098
|
+
|
|
4099
|
+
await runtime.setFormValues(blockCtx, [{ path: ['users', 0, 'secondaryUser'], value: secondary }], {
|
|
4100
|
+
source: 'user',
|
|
4101
|
+
});
|
|
4102
|
+
await waitFor(() => expect(formStub.getFieldValue(['users', 0, 'user'])).toEqual(secondary));
|
|
4103
|
+
|
|
4104
|
+
await runtime.setFormValues(blockCtx, [{ path: ['users', 0, 'user'], value: { id: 3, name: 'Manual' } }], {
|
|
4105
|
+
source: 'user',
|
|
4106
|
+
});
|
|
4107
|
+
await waitFor(() => expect(formStub.getFieldValue(['users', 0, 'user'])).toEqual(secondary));
|
|
4108
|
+
|
|
4109
|
+
const nextSecondary = { id: 4, name: 'Next secondary' };
|
|
4110
|
+
await runtime.setFormValues(blockCtx, [{ path: ['users', 0, 'secondaryUser'], value: nextSecondary }], {
|
|
4111
|
+
source: 'user',
|
|
4112
|
+
});
|
|
4113
|
+
await waitFor(() => expect(formStub.getFieldValue(['users', 0, 'user'])).toEqual(nextSecondary));
|
|
4114
|
+
});
|
|
4115
|
+
|
|
4116
|
+
it('materializes assigned proxies so later to-many rows continue updating', async () => {
|
|
4117
|
+
const engineEmitter = new EventEmitter();
|
|
4118
|
+
const blockEmitter = new EventEmitter();
|
|
4119
|
+
const initialSecondary = { id: 2, name: 'Initial secondary', roleCodes: ['admin'] };
|
|
4120
|
+
const nextSecondary = { id: 4, name: 'Next secondary', roleCodes: ['editor'] };
|
|
4121
|
+
type UserRow = {
|
|
4122
|
+
user: unknown;
|
|
4123
|
+
secondaryUser: typeof initialSecondary | null;
|
|
4124
|
+
};
|
|
4125
|
+
const store: { users: UserRow[] } = {
|
|
4126
|
+
users: [{ user: null, secondaryUser: initialSecondary }],
|
|
4127
|
+
};
|
|
4128
|
+
type FormNamePath = Parameters<FormInstance['getFieldValue']>[0];
|
|
4129
|
+
const formStub = {
|
|
4130
|
+
getFieldValue: (namePath: FormNamePath) => lodashGet(store, namePath),
|
|
4131
|
+
setFieldValue: (namePath: FormNamePath, value: unknown) => lodashSet(store, namePath, value),
|
|
4132
|
+
// Match Ant Form: the outer store is plain, but nested values may still be reactive proxies.
|
|
4133
|
+
getFieldsValue: () => store,
|
|
4134
|
+
setFieldsValue: (patch: Partial<typeof store>) => lodashMerge(store, patch),
|
|
4135
|
+
};
|
|
4136
|
+
|
|
4137
|
+
const blockModel = {
|
|
4138
|
+
uid: 'form-assign-assoc-to-many-later-row-reactive-proxy',
|
|
4139
|
+
flowEngine: { emitter: engineEmitter },
|
|
4140
|
+
emitter: blockEmitter,
|
|
4141
|
+
dispatchEvent: vi.fn(),
|
|
4142
|
+
getAclActionName: () => 'create',
|
|
4143
|
+
context: undefined as unknown,
|
|
4144
|
+
};
|
|
4145
|
+
|
|
4146
|
+
const runtime = new FormValueRuntime({
|
|
4147
|
+
model: blockModel as unknown as ConstructorParameters<typeof FormValueRuntime>[0]['model'],
|
|
4148
|
+
getForm: () => formStub as unknown as FormInstance,
|
|
4149
|
+
});
|
|
4150
|
+
runtime.mount({ sync: true });
|
|
4151
|
+
|
|
4152
|
+
const blockCtx = createFieldContext(runtime);
|
|
4153
|
+
const userCollection = { getField: () => null };
|
|
4154
|
+
const userField = { isAssociationField: () => true, type: 'belongsTo', targetCollection: userCollection };
|
|
4155
|
+
const usersItemCollection = {
|
|
4156
|
+
getField: (name: string) => (name === 'user' || name === 'secondaryUser' ? userField : null),
|
|
4157
|
+
};
|
|
4158
|
+
const usersField = { isAssociationField: () => true, type: 'hasMany', targetCollection: usersItemCollection };
|
|
4159
|
+
const collection = { getField: (name: string) => (name === 'users' ? usersField : null) };
|
|
4160
|
+
blockCtx.defineProperty('collection', { value: collection });
|
|
4161
|
+
|
|
4162
|
+
const createRowModel = (index: number) => {
|
|
4163
|
+
const forkId = `users:${index}`;
|
|
4164
|
+
const rowCtx = createFieldContext(runtime);
|
|
4165
|
+
const rowModel = {
|
|
4166
|
+
uid: `users.user:${forkId}`,
|
|
4167
|
+
isFork: true,
|
|
4168
|
+
forkId,
|
|
4169
|
+
subModels: { field: { context: { collectionField: userField } } },
|
|
4170
|
+
getStepParams(flowKey: string, stepKey: string) {
|
|
4171
|
+
if (flowKey === 'fieldSettings' && stepKey === 'init') return { fieldPath: 'users.user' };
|
|
4172
|
+
return undefined;
|
|
4173
|
+
},
|
|
4174
|
+
context: rowCtx,
|
|
4175
|
+
};
|
|
4176
|
+
rowCtx.defineProperty('blockModel', { value: blockModel });
|
|
4177
|
+
rowCtx.defineProperty('collection', { value: collection });
|
|
4178
|
+
rowCtx.defineProperty('fieldIndex', { value: [forkId] });
|
|
4179
|
+
rowCtx.defineProperty('model', { value: rowModel });
|
|
4180
|
+
return rowModel;
|
|
4181
|
+
};
|
|
4182
|
+
const rowModels = [createRowModel(0)];
|
|
4183
|
+
|
|
4184
|
+
blockCtx.defineProperty('engine', {
|
|
4185
|
+
value: { forEachModel: (callback: (model: unknown) => void) => rowModels.forEach(callback) },
|
|
4186
|
+
});
|
|
4187
|
+
blockModel.context = blockCtx;
|
|
4188
|
+
|
|
4189
|
+
runtime.syncAssignRules([
|
|
4190
|
+
{
|
|
4191
|
+
key: 'user-from-row-sibling',
|
|
4192
|
+
enable: true,
|
|
4193
|
+
targetPath: 'users.user',
|
|
4194
|
+
mode: 'assign',
|
|
4195
|
+
condition: { logic: '$and', items: [] },
|
|
4196
|
+
value: '{{ ctx.item.parentItem.value.secondaryUser }}',
|
|
4197
|
+
},
|
|
4198
|
+
]);
|
|
4199
|
+
|
|
4200
|
+
await waitFor(() => expect(formStub.getFieldValue(['users', 0, 'user'])).toEqual(initialSecondary));
|
|
4201
|
+
const assignedUser = formStub.getFieldValue(['users', 0, 'user']);
|
|
4202
|
+
const assignedRoleCodes = formStub.getFieldValue(['users', 0, 'user', 'roleCodes']);
|
|
4203
|
+
expect(assignedUser.constructor).toBe(Object);
|
|
4204
|
+
expect(Array.isArray(assignedRoleCodes)).toBe(true);
|
|
4205
|
+
expect(assignedRoleCodes.constructor).toBe(Array);
|
|
4206
|
+
|
|
4207
|
+
const secondRow: UserRow = { user: null, secondaryUser: null };
|
|
4208
|
+
store.users.push(secondRow);
|
|
4209
|
+
const addedRows: Array<UserRow | undefined> = new Array(2);
|
|
4210
|
+
addedRows[1] = secondRow;
|
|
4211
|
+
expect(() => runtime.handleFormValuesChange({ users: addedRows }, store)).not.toThrow();
|
|
4212
|
+
|
|
4213
|
+
const row1 = createRowModel(1);
|
|
4214
|
+
rowModels.push(row1);
|
|
4215
|
+
engineEmitter.emit('model:mounted', { model: row1 });
|
|
4216
|
+
|
|
4217
|
+
secondRow.secondaryUser = nextSecondary;
|
|
4218
|
+
const changedRows: Array<Partial<UserRow> | undefined> = new Array(2);
|
|
4219
|
+
changedRows[1] = { secondaryUser: nextSecondary };
|
|
4220
|
+
expect(0 in changedRows).toBe(false);
|
|
4221
|
+
|
|
4222
|
+
expect(() => runtime.handleFormValuesChange({ users: changedRows }, store)).not.toThrow();
|
|
4223
|
+
await waitFor(() => expect(formStub.getFieldValue(['users', 1, 'user'])).toEqual(nextSecondary));
|
|
4224
|
+
expect(formStub.getFieldValue(['users', 0, 'user'])).toEqual(initialSecondary);
|
|
4225
|
+
});
|
|
4226
|
+
|
|
4227
|
+
it('assigns a to-many association from a sibling association in the same to-many row', async () => {
|
|
4228
|
+
const engineEmitter = new EventEmitter();
|
|
4229
|
+
const blockEmitter = new EventEmitter();
|
|
4230
|
+
const initialRoles = [{ id: 2, name: 'Initial role' }];
|
|
4231
|
+
const formStub = createFormStub({ users: [{ roles: [], secondaryRoles: [] }] });
|
|
4232
|
+
|
|
4233
|
+
const blockModel: any = {
|
|
4234
|
+
uid: 'form-assign-to-many-assoc-from-to-many-row-sibling',
|
|
4235
|
+
flowEngine: { emitter: engineEmitter },
|
|
4236
|
+
emitter: blockEmitter,
|
|
4237
|
+
dispatchEvent: vi.fn(),
|
|
4238
|
+
getAclActionName: () => 'create',
|
|
4239
|
+
};
|
|
4240
|
+
|
|
4241
|
+
const runtime = new FormValueRuntime({ model: blockModel, getForm: () => formStub as any });
|
|
4242
|
+
runtime.mount({ sync: true });
|
|
4243
|
+
|
|
4244
|
+
const blockCtx = createFieldContext(runtime);
|
|
4245
|
+
const roleCollection: any = { filterTargetKey: 'id', getField: () => null };
|
|
4246
|
+
const rolesField: any = {
|
|
4247
|
+
isAssociationField: () => true,
|
|
4248
|
+
type: 'belongsToMany',
|
|
4249
|
+
targetCollection: roleCollection,
|
|
4250
|
+
};
|
|
4251
|
+
const usersItemCollection: any = {
|
|
4252
|
+
getField: (name: string) => (name === 'roles' || name === 'secondaryRoles' ? rolesField : null),
|
|
4253
|
+
};
|
|
4254
|
+
const usersField: any = { isAssociationField: () => true, type: 'hasMany', targetCollection: usersItemCollection };
|
|
4255
|
+
const collection: any = { getField: (name: string) => (name === 'users' ? usersField : null) };
|
|
4256
|
+
blockCtx.defineProperty('collection', { value: collection });
|
|
4257
|
+
|
|
4258
|
+
const rowModel: any = {
|
|
4259
|
+
uid: 'users.roles:users:0',
|
|
4260
|
+
isFork: true,
|
|
4261
|
+
forkId: 'users:0',
|
|
4262
|
+
subModels: { field: { context: { collectionField: rolesField } } },
|
|
4263
|
+
getStepParams(flowKey: string, stepKey: string) {
|
|
4264
|
+
if (flowKey === 'fieldSettings' && stepKey === 'init') return { fieldPath: 'users.roles' };
|
|
4265
|
+
return undefined;
|
|
4266
|
+
},
|
|
4267
|
+
};
|
|
4268
|
+
const rowCtx = createFieldContext(runtime);
|
|
4269
|
+
rowCtx.defineProperty('blockModel', { value: blockModel });
|
|
4270
|
+
rowCtx.defineProperty('collection', { value: collection });
|
|
4271
|
+
rowCtx.defineProperty('fieldIndex', { value: ['users:0'] });
|
|
4272
|
+
rowCtx.defineProperty('model', { value: rowModel });
|
|
4273
|
+
rowModel.context = rowCtx;
|
|
4274
|
+
|
|
4275
|
+
blockCtx.defineProperty('engine', {
|
|
4276
|
+
value: { forEachModel: (callback: (model: unknown) => void) => callback(rowModel) },
|
|
4277
|
+
});
|
|
4278
|
+
blockModel.context = blockCtx;
|
|
4279
|
+
|
|
4280
|
+
runtime.syncAssignRules([
|
|
4281
|
+
{
|
|
4282
|
+
key: 'roles-from-row-sibling',
|
|
4283
|
+
enable: true,
|
|
4284
|
+
targetPath: 'users.roles',
|
|
4285
|
+
mode: 'assign',
|
|
4286
|
+
condition: { logic: '$and', items: [] },
|
|
4287
|
+
value: '{{ ctx.item.parentItem.value.secondaryRoles }}',
|
|
4288
|
+
},
|
|
4289
|
+
]);
|
|
4290
|
+
|
|
4291
|
+
await runtime.setFormValues(blockCtx, [{ path: ['users', 0, 'secondaryRoles'], value: initialRoles }], {
|
|
4292
|
+
source: 'user',
|
|
4293
|
+
});
|
|
4294
|
+
await waitFor(() => expect(formStub.getFieldValue(['users', 0, 'roles'])).toEqual(initialRoles));
|
|
4295
|
+
|
|
4296
|
+
const nextRoles = [{ id: 3, name: 'Next role' }];
|
|
4297
|
+
await runtime.setFormValues(blockCtx, [{ path: ['users', 0, 'secondaryRoles'], value: nextRoles }], {
|
|
4298
|
+
source: 'user',
|
|
4299
|
+
});
|
|
4300
|
+
await waitFor(() => expect(formStub.getFieldValue(['users', 0, 'roles'])).toEqual(nextRoles));
|
|
4301
|
+
});
|
|
4302
|
+
|
|
4303
|
+
it('preserves the PopupSubTable outer item chain for a top-level to-one association target', async () => {
|
|
4304
|
+
const engineEmitter = new EventEmitter();
|
|
4305
|
+
const blockEmitter = new EventEmitter();
|
|
4306
|
+
const initialAssignee = { id: 2, name: 'Initial assignee' };
|
|
4307
|
+
const nextAssignee = { id: 3, name: 'Next assignee' };
|
|
4308
|
+
const outerValue = observable({ defaultAssignee: initialAssignee });
|
|
4309
|
+
const formStub = createFormStub({ assignee: null });
|
|
4310
|
+
|
|
4311
|
+
const blockModel: any = {
|
|
4312
|
+
uid: 'popup-sub-table-form-assign-parent-item',
|
|
4313
|
+
flowEngine: { emitter: engineEmitter },
|
|
4314
|
+
emitter: blockEmitter,
|
|
4315
|
+
dispatchEvent: vi.fn(),
|
|
4316
|
+
getAclActionName: () => 'create',
|
|
4317
|
+
};
|
|
4318
|
+
|
|
4319
|
+
const runtime = new FormValueRuntime({ model: blockModel, getForm: () => formStub as any });
|
|
4320
|
+
runtime.mount({ sync: true });
|
|
4321
|
+
|
|
4322
|
+
const blockCtx = createFieldContext(runtime);
|
|
4323
|
+
const userCollection: any = { filterTargetKey: 'id', getField: () => null };
|
|
4324
|
+
const assigneeField: any = {
|
|
4325
|
+
isAssociationField: () => true,
|
|
4326
|
+
type: 'belongsTo',
|
|
4327
|
+
targetCollection: userCollection,
|
|
4328
|
+
};
|
|
4329
|
+
const collection: any = { getField: (name: string) => (name === 'assignee' ? assigneeField : null) };
|
|
4330
|
+
const outerItem = { index: 0, length: 1, value: outerValue };
|
|
4331
|
+
blockCtx.defineProperty('collection', { value: collection });
|
|
4332
|
+
blockCtx.defineProperty('item', {
|
|
4333
|
+
get: () => ({
|
|
4334
|
+
index: 1,
|
|
4335
|
+
length: 3,
|
|
4336
|
+
__is_new__: true,
|
|
4337
|
+
__is_stored__: false,
|
|
4338
|
+
value: blockCtx.formValues,
|
|
4339
|
+
parentItem: outerItem,
|
|
4340
|
+
}),
|
|
4341
|
+
cache: false,
|
|
4342
|
+
});
|
|
4343
|
+
blockModel.context = blockCtx;
|
|
4344
|
+
|
|
4345
|
+
runtime.syncAssignRules([
|
|
4346
|
+
{
|
|
4347
|
+
key: 'assignee-from-popup-parent',
|
|
4348
|
+
enable: true,
|
|
4349
|
+
targetPath: 'assignee',
|
|
4350
|
+
mode: 'assign',
|
|
4351
|
+
condition: {
|
|
4352
|
+
logic: '$and',
|
|
4353
|
+
items: [
|
|
4354
|
+
{ path: '{{ ctx.item.parentItem.index }}', operator: '$eq', value: 1 },
|
|
4355
|
+
{ path: '{{ ctx.item.parentItem.length }}', operator: '$eq', value: 3 },
|
|
4356
|
+
{ path: '{{ ctx.item.parentItem.__is_new__ }}', operator: '$eq', value: true },
|
|
4357
|
+
{ path: '{{ ctx.item.parentItem.__is_stored__ }}', operator: '$eq', value: false },
|
|
4358
|
+
],
|
|
4359
|
+
},
|
|
4360
|
+
value: '{{ ctx.item.parentItem.parentItem.value.defaultAssignee }}',
|
|
4361
|
+
},
|
|
4362
|
+
]);
|
|
4363
|
+
|
|
4364
|
+
await waitFor(() => expect(formStub.getFieldValue(['assignee'])).toEqual(initialAssignee));
|
|
4365
|
+
|
|
4366
|
+
outerValue.defaultAssignee = nextAssignee;
|
|
4367
|
+
await waitFor(() => expect(formStub.getFieldValue(['assignee'])).toEqual(nextAssignee));
|
|
4368
|
+
});
|
|
4369
|
+
|
|
4370
|
+
it('preserves the PopupSubTable outer item chain for a top-level to-many association target', async () => {
|
|
4371
|
+
const engineEmitter = new EventEmitter();
|
|
4372
|
+
const blockEmitter = new EventEmitter();
|
|
4373
|
+
const initialReviewers = [{ id: 2, name: 'Initial reviewer' }];
|
|
4374
|
+
const nextReviewers = [{ id: 3, name: 'Next reviewer' }];
|
|
4375
|
+
const outerValue = observable({ defaultReviewers: initialReviewers });
|
|
4376
|
+
const formStub = createFormStub({ reviewers: [] });
|
|
4377
|
+
|
|
4378
|
+
const blockModel: any = {
|
|
4379
|
+
uid: 'popup-sub-table-form-assign-to-many-parent-item',
|
|
4380
|
+
flowEngine: { emitter: engineEmitter },
|
|
4381
|
+
emitter: blockEmitter,
|
|
4382
|
+
dispatchEvent: vi.fn(),
|
|
4383
|
+
getAclActionName: () => 'create',
|
|
4384
|
+
};
|
|
4385
|
+
|
|
4386
|
+
const runtime = new FormValueRuntime({ model: blockModel, getForm: () => formStub as any });
|
|
4387
|
+
runtime.mount({ sync: true });
|
|
4388
|
+
|
|
4389
|
+
const blockCtx = createFieldContext(runtime);
|
|
4390
|
+
const userCollection: any = { filterTargetKey: 'id', getField: () => null };
|
|
4391
|
+
const reviewersField: any = {
|
|
4392
|
+
isAssociationField: () => true,
|
|
4393
|
+
type: 'belongsToMany',
|
|
4394
|
+
targetCollection: userCollection,
|
|
4395
|
+
};
|
|
4396
|
+
const collection: any = { getField: (name: string) => (name === 'reviewers' ? reviewersField : null) };
|
|
4397
|
+
const outerItem = { index: 0, length: 1, value: outerValue };
|
|
4398
|
+
blockCtx.defineProperty('collection', { value: collection });
|
|
4399
|
+
blockCtx.defineProperty('item', {
|
|
4400
|
+
get: () => ({
|
|
4401
|
+
index: 1,
|
|
4402
|
+
length: 3,
|
|
4403
|
+
__is_new__: true,
|
|
4404
|
+
__is_stored__: false,
|
|
4405
|
+
value: blockCtx.formValues,
|
|
4406
|
+
parentItem: outerItem,
|
|
4407
|
+
}),
|
|
4408
|
+
cache: false,
|
|
4409
|
+
});
|
|
4410
|
+
blockModel.context = blockCtx;
|
|
4411
|
+
|
|
4412
|
+
runtime.syncAssignRules([
|
|
4413
|
+
{
|
|
4414
|
+
key: 'reviewers-from-popup-parent',
|
|
4415
|
+
enable: true,
|
|
4416
|
+
targetPath: 'reviewers',
|
|
4417
|
+
mode: 'assign',
|
|
4418
|
+
condition: {
|
|
4419
|
+
logic: '$and',
|
|
4420
|
+
items: [
|
|
4421
|
+
{ path: '{{ ctx.item.parentItem.index }}', operator: '$eq', value: 1 },
|
|
4422
|
+
{ path: '{{ ctx.item.parentItem.length }}', operator: '$eq', value: 3 },
|
|
4423
|
+
{ path: '{{ ctx.item.parentItem.__is_new__ }}', operator: '$eq', value: true },
|
|
4424
|
+
{ path: '{{ ctx.item.parentItem.__is_stored__ }}', operator: '$eq', value: false },
|
|
4425
|
+
],
|
|
4426
|
+
},
|
|
4427
|
+
value: '{{ ctx.item.parentItem.parentItem.value.defaultReviewers }}',
|
|
4428
|
+
},
|
|
4429
|
+
]);
|
|
4430
|
+
|
|
4431
|
+
await waitFor(() => expect(formStub.getFieldValue(['reviewers'])).toEqual(initialReviewers));
|
|
4432
|
+
|
|
4433
|
+
outerValue.defaultReviewers = nextReviewers;
|
|
4434
|
+
await waitFor(() => expect(formStub.getFieldValue(['reviewers'])).toEqual(nextReviewers));
|
|
4435
|
+
});
|
|
4436
|
+
|
|
4002
4437
|
it('does not write to to-many nested path without row index', async () => {
|
|
4003
4438
|
const engineEmitter = new EventEmitter();
|
|
4004
4439
|
const blockEmitter = new EventEmitter();
|