diff --git a/frontend/src/app/features/content/shared/forms/array-item.component.html b/frontend/src/app/features/content/shared/forms/array-item.component.html index 5b37e9629..e5154cf48 100644 --- a/frontend/src/app/features/content/shared/forms/array-item.component.html +++ b/frontend/src/app/features/content/shared/forms/array-item.component.html @@ -43,7 +43,7 @@
-
+
-
+
{ expect(array.get(1)!.get('field1')!.hidden).toBeFalsy(); }); + it('should add component with default values', () => { + const componentId = MathHelper.guid(); + const component = createSchema({ + id: 1, + fields: [ + createField({ + id: 11, + properties: createProperties('String', { + defaultValue: 'Initial', + }), + partitioning: 'invariant', + }), + createField({ + id: 12, + properties: createProperties('Number', { + defaultValue: 12, + }), + partitioning: 'invariant', + }), + ], + }); + + const contentForm = createForm([ + createField({ + id: 4, + properties: createProperties('Component'), + partitioning: 'invariant', + }), + ], [], { + [componentId]: component, + }); + + contentForm.load({}); + + // Should be undefined by default. + expect(contentForm.value).toEqual({ + field4: { + iv: undefined, + }, + }); + + (contentForm.get('field4')?.get('iv') as ComponentForm).selectSchema(componentId); + + // Should add field from component. + expect(contentForm.value).toEqual({ + field4: { + iv: { + schemaId: componentId, + field11: 'Initial', + field12: 12, + }, + }, + }); + }); + it('should replace component with new fields', () => { const component1Id = MathHelper.guid(); const component1 = createSchema({ diff --git a/frontend/src/app/shared/state/contents.forms.ts b/frontend/src/app/shared/state/contents.forms.ts index 7ed264a17..8ca02e03f 100644 --- a/frontend/src/app/shared/state/contents.forms.ts +++ b/frontend/src/app/shared/state/contents.forms.ts @@ -326,7 +326,7 @@ export class FieldArrayForm extends AbstractContentForm) { + public set internalItems(value: ReadonlyArray) { this.item$.next(value); } @@ -367,7 +367,7 @@ export class FieldArrayForm extends AbstractContentForm i !== index); + this.model.internalItems = this.model.items.filter((_, i) => i !== index); } public clearControls() { - this.model.items = []; + this.model.internalItems = []; } private createItem() { @@ -439,19 +439,19 @@ export type FieldItemForm = ComponentForm | FieldValueForm | FieldArrayForm; type FieldMap = { [name: string]: FieldItemForm }; export class ObjectFormBase extends AbstractContentForm { - private readonly fieldSections$ = new BehaviorSubject>>([]); + private readonly sections$ = new BehaviorSubject>>([]); private readonly fields$ = new BehaviorSubject({}); - public get fieldSectionsChanges(): Observable>> { - return this.fieldSections$; + public get sectionsChanges(): Observable>> { + return this.sections$; } - public get fieldSections() { - return this.fieldSections$.value; + public get sections() { + return this.sections$.value; } - public set fieldSections(value: ReadonlyArray>) { - this.fieldSections$.next(value); + public set internalFieldSections(value: ReadonlyArray>) { + this.sections$.next(value); } public get fieldsChanges(): Observable { @@ -462,7 +462,7 @@ export class ObjectFormBase extends Abstract return this.fields$.value; } - public set fields(value: FieldMap) { + public set internalFieldByName(value: FieldMap) { this.fields$.next(value); } @@ -483,7 +483,7 @@ export class ObjectFormBase extends Abstract field.updateState(context, this.form.value, state); } - for (const section of this.fieldSections) { + for (const section of this.sections) { section.updateHidden(); } } @@ -529,8 +529,8 @@ abstract class ObjectTemplate impleme } } - protected setControlsCore(schema: ReadonlyArray, value: any, model: T, form: FormGroup) { - const fieldMap: FieldMap = {}; + protected setControlsCore(schema: ReadonlyArray, _: any, model: T, form: FormGroup) { + const fieldByName: FieldMap = {}; const fieldSections: FieldSection[] = []; for (const { separator, fields } of groupFields(schema)) { @@ -549,14 +549,14 @@ abstract class ObjectTemplate impleme forms.push(childForm); - fieldMap[field.name] = childForm; + fieldByName[field.name] = childForm; } fieldSections.push(new FieldSection(separator, forms)); } - model.fields = fieldMap; - model.fieldSections = fieldSections; + model.internalFieldByName = fieldByName; + model.internalFieldSections = fieldSections; } protected clearControlsCore(model: T) { @@ -564,8 +564,8 @@ abstract class ObjectTemplate impleme model.form.removeControl(name); } - model.fields = {}; - model.fieldSections = []; + model.internalFieldByName = {}; + model.internalFieldSections = []; } } @@ -595,7 +595,7 @@ export class ComponentForm extends ObjectFormBase { return this.schema$.value; } - public set schema(value: SchemaDto | undefined) { + public set internalSchema(value: SchemaDto | undefined) { this.schema$.next(value); } @@ -610,11 +610,10 @@ export class ComponentForm extends ObjectFormBase { partition); this.form.reset(undefined); - this.form.build(); } public selectSchema(schemaId: string) { - this.form.reset({ schemaId }); + this.form.patchValue({ schemaId }); } } @@ -626,13 +625,13 @@ class ComponentTemplate extends ObjectTemplate { protected setControlsCore(schema: ReadonlyArray, value: any, model: ComponentForm, form: FormGroup) { form.setControl('schemaId', new FormControl()); - this.model.schema = model.globals.schemas[value?.schemaId]; + this.model.internalSchema = model.globals.schemas[value?.schemaId]; super.setControlsCore(schema, value, model, form); } protected clearControlsCore(model: ComponentForm) { - this.model.schema = undefined; + this.model.internalSchema = undefined; super.clearControlsCore(model); }