diff --git a/web/projects/ui/src/app/routes/portal/components/form/form-union/form-union.component.ts b/web/projects/ui/src/app/routes/portal/components/form/form-union/form-union.component.ts index e426259a6..e8602eeb0 100644 --- a/web/projects/ui/src/app/routes/portal/components/form/form-union/form-union.component.ts +++ b/web/projects/ui/src/app/routes/portal/components/form/form-union/form-union.component.ts @@ -30,6 +30,7 @@ export class FormUnionComponent implements OnChanges { private readonly form = inject(FormGroupName) private readonly formService = inject(FormService) + private readonly values: Record = {} get union(): string { return this.form.value.selection @@ -37,10 +38,13 @@ export class FormUnionComponent implements OnChanges { @tuiPure onUnion(union: string) { + this.values[this.union] = this.form.control.controls['value'].value this.form.control.setControl( 'value', this.formService.getFormGroup( union ? this.spec.variants[union].spec : {}, + [], + this.values[union], ), { emitEvent: false, diff --git a/web/projects/ui/src/app/services/form.service.ts b/web/projects/ui/src/app/services/form.service.ts index 80721380b..453accb8e 100644 --- a/web/projects/ui/src/app/services/form.service.ts +++ b/web/projects/ui/src/app/services/form.service.ts @@ -37,18 +37,14 @@ export class FormService { } } - getUnionObject( - spec: IST.ValueSpecUnion, - selected: string | null, - ): UntypedFormGroup { - const group = this.getFormGroup({ - selection: this.getUnionSelectSpec(spec, selected), - }) + getUnionObject(spec: IST.ValueSpecUnion, value: any): UntypedFormGroup { + const valid = spec.variants[value?.selection] + const selected = valid ? value?.selection : spec.default + const selection = this.getUnionSelectSpec(spec, selected) + const group = this.getFormGroup({ selection }) + const control = selected ? spec.variants[selected].spec : {} - group.setControl( - 'value', - this.getFormGroup(selected ? spec.variants[selected].spec : {}), - ) + group.setControl('value', this.getFormGroup(control, [], value?.value)) return group } @@ -134,13 +130,7 @@ export class FormService { fileValidators(spec), ) case 'union': - const currentSelection = currentValue?.selection - const isValid = !!spec.variants[currentSelection] - - return this.getUnionObject( - spec, - isValid ? currentSelection : spec.default, - ) + return this.getUnionObject(spec, currentValue) case 'toggle': value = currentValue === undefined ? spec.default : currentValue return this.formBuilder.control(value)