mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 10:21:52 +00:00
fix: properly handle values in unions (#2826)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user