fix: properly handle values in unions (#2826)

This commit is contained in:
Alex Inkin
2025-02-09 04:36:32 +04:00
committed by GitHub
parent b2b98643d8
commit 95cad7bdd9
2 changed files with 12 additions and 18 deletions

View File

@@ -30,6 +30,7 @@ export class FormUnionComponent implements OnChanges {
private readonly form = inject(FormGroupName)
private readonly formService = inject(FormService)
private readonly values: Record<string, any> = {}
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,

View File

@@ -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)