Skip to content

Commit 19fb3eb

Browse files
authored
fix: inherited values also work from initial values of a new entity (#2549)
fixes #2431
1 parent a77bd02 commit 19fb3eb

File tree

5 files changed

+184
-100
lines changed

5 files changed

+184
-100
lines changed

src/app/core/common-components/entity-form/entity-form.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ export type EntityFormGroup<T extends Entity> = TypedFormGroup<Partial<T>>;
3333
export interface EntityForm<T extends Entity> {
3434
formGroup: EntityFormGroup<T>;
3535
entity: T;
36+
37+
/**
38+
* map of field ids to the default value configuration for that field
39+
*/
3640
defaultValueConfigs: Map<string, DefaultValueConfig>;
41+
42+
/**
43+
* map of field ids to the current value to be inherited from the referenced parent entities' field
44+
*/
3745
inheritedParentValues: Map<string, any>;
46+
3847
watcher: Map<string, Subscription>;
3948
}
4049

src/app/core/config/config-fix.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ export const defaultJsonConfig = {
397397
"language"
398398
],
399399
"filters": [
400-
{ "id": "privateSchool" },
401-
{ "id": "parentSchool" }
400+
{ "id": "privateSchool" }
402401
]
403402
}
404403
},
@@ -416,7 +415,6 @@ export const defaultJsonConfig = {
416415
"config": {
417416
"fieldGroups": [
418417
{ "fields": ["name", "privateSchool", "parentSchool"] },
419-
{ fields: [ "testSchools", "bool", "string", "enum", "refSingle", "refMulti" ]},
420418
{ "fields": ["address", "phone"] },
421419
{ "fields": ["language", "timing"] },
422420
{ "fields": ["remarks"] }
@@ -1071,21 +1069,11 @@ export const defaultJsonConfig = {
10711069
},
10721070
"privateSchool": {
10731071
"dataType": "boolean",
1074-
"label": $localize`:Label for if a school is a private school:Private School`,
1075-
"defaultValue": {
1076-
"mode": "inherited",
1077-
"localAttribute": "parentSchool",
1078-
"field": "privateSchool"
1079-
}
1072+
"label": $localize`:Label for if a school is a private school:Private School`
10801073
},
10811074
"language": {
10821075
"dataType": "string",
1083-
"label": $localize`:Label for the language of a school:Language`,
1084-
"defaultValue": {
1085-
"mode": "inherited",
1086-
"localAttribute": "parentSchool",
1087-
"field": "language"
1088-
}
1076+
"label": $localize`:Label for the language of a school:Language`
10891077
},
10901078
"address": {
10911079
"dataType": "location",
@@ -1102,12 +1090,6 @@ export const defaultJsonConfig = {
11021090
"remarks": {
11031091
"dataType": "string",
11041092
"label": $localize`:Label for the remarks for a school:Remarks`
1105-
},
1106-
"parentSchool": {
1107-
"dataType": "entity",
1108-
"additional": "School",
1109-
"label": $localize`:Label for school attribute:Branch of`,
1110-
"description": $localize`:Description for school attribute:Select the "parent school" here to build a hierarchy of a school with multiple branch institutions.`
11111093
}
11121094
},
11131095
},

src/app/core/default-values/default-value-strategy.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export abstract class DefaultValueStrategy {
2828
async initEntityForm<T extends Entity>(form: EntityForm<T>): Promise<void> {}
2929
}
3030

31+
/**
32+
* Get the default value configs filtered for the given mode.
33+
* @param defaultValueConfigs
34+
* @param mode
35+
*/
3136
export function getConfigsByMode(
3237
defaultValueConfigs: Map<string, DefaultValueConfig>,
3338
mode: ("inherited" | "static" | "dynamic")[],

src/app/core/default-values/inherited-value.service.spec.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe("InheritedValueService", () => {
6868
);
6969

7070
// then
71-
expect(targetFormControl.value).toBe(null);
71+
expect(targetFormControl.value).toBe(undefined);
7272
expect(
7373
form.watcher.has("sourceFormControlValueChanges_field1"),
7474
).toBeFalse();
@@ -314,7 +314,7 @@ describe("InheritedValueService", () => {
314314
// when/then
315315
form.formGroup.get("reference-1").setValue("non-existing-entity-id");
316316
tick(); // fetching reference is always async
317-
expect(form.formGroup.get("field").value).toBe(null);
317+
expect(form.formGroup.get("field").value).toBe(undefined);
318318
}));
319319

320320
it("should do nothing, if formGroup is disabled", fakeAsync(() => {
@@ -343,4 +343,31 @@ describe("InheritedValueService", () => {
343343
tick(); // fetching reference is always async
344344
expect(form.formGroup.get("field").value).toBe(null);
345345
}));
346+
347+
it("should set value on FormControl, if source is not in formGroup but set on entity", fakeAsync(() => {
348+
// given
349+
let form = getDefaultInheritedForm({
350+
field: {
351+
isArray: true,
352+
defaultValue: {
353+
mode: "inherited",
354+
field: "foo",
355+
localAttribute: "reference-1",
356+
},
357+
},
358+
});
359+
form.formGroup.removeControl("reference-1");
360+
361+
let entity0 = new Entity();
362+
entity0["foo"] = ["bar"];
363+
mockEntityMapperService.load.and.returnValue(Promise.resolve(entity0));
364+
form.entity["reference-1"] = entity0.getId();
365+
366+
// when
367+
defaultValueService.handleEntityForm(form, form.entity);
368+
tick();
369+
370+
// then
371+
expect(form.formGroup.get("field").value).toEqual(["bar"]);
372+
}));
346373
});

0 commit comments

Comments
 (0)