From 6e3d29ced51cd98b022cc10600b13c4b468c5ca7 Mon Sep 17 00:00:00 2001 From: kurilova Date: Tue, 30 Dec 2025 14:16:21 +0000 Subject: [PATCH 1/2] Deselect options when None is selected --- .../dynamic-form/dynamic-form.component.ts | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts b/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts index b0abc5eb3..778685bae 100644 --- a/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts +++ b/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts @@ -17,7 +17,7 @@ import { Component, HostListener, inject, - Input, + Input, OnDestroy, OnInit, Renderer2, viewChildren, @@ -53,6 +53,10 @@ import { MatCheckboxModule } from '@angular/material/checkbox'; import { TextFieldModule } from '@angular/cdk/text-field'; import { ProfileValidators } from '../../pages/risk-assessment/profile-form/profile.validators'; import { DomSanitizer } from '@angular/platform-browser'; +import { Subject } from 'rxjs/internal/Subject'; +import { takeUntil } from 'rxjs/internal/operators/takeUntil'; +import { startWith } from 'rxjs/internal/operators/startWith'; +import { pairwise } from 'rxjs/internal/operators/pairwise'; @Component({ selector: 'app-dynamic-form', @@ -79,13 +83,14 @@ import { DomSanitizer } from '@angular/platform-browser'; styleUrl: './dynamic-form.component.scss', encapsulation: ViewEncapsulation.None, }) -export class DynamicFormComponent implements OnInit { +export class DynamicFormComponent implements OnInit, OnDestroy { readonly formFields = viewChildren(MatFormField); private fb = inject(FormBuilder); private profileValidators = inject(ProfileValidators); private domSanitizer = inject(DomSanitizer); private renderer = inject(Renderer2); + private destroy$: Subject = new Subject(); public readonly FormControlType = FormControlType; @@ -124,13 +129,20 @@ export class DynamicFormComponent implements OnInit { this.createProfileForm(this.format); } + ngOnDestroy() { + this.destroy$.next(true); + this.destroy$.unsubscribe(); + } + createProfileForm(questions: QuestionFormat[]) { questions.forEach((question, index) => { if (question.type === FormControlType.SELECT_MULTIPLE) { - this.formGroup.addControl( - index.toString(), - this.getMultiSelectGroup(question) - ); + const multiSelect = this.getMultiSelectGroup(question); + this.formGroup.addControl(index.toString(), multiSelect); + const noneKey = this.getNoneOptionIndex(question.options); + if (noneKey) { + this.applyNoneLogic(multiSelect, noneKey.toString()); + } } else { const validators = this.getValidators( question.type, @@ -210,4 +222,39 @@ export class DynamicFormComponent implements OnInit { } }); } + + private applyNoneLogic(control: AbstractControl, noneKey: string) { + control.valueChanges + .pipe(takeUntil(this.destroy$), startWith(control.value), pairwise()) + .subscribe(([prev, curr]) => { + const changedKey = Object.keys(curr).find( + key => curr[key] === true && prev[key] === false + ); + + if (!changedKey) { + return; + } + + if (changedKey == noneKey) { + const newValue = { ...curr }; + + Object.keys(newValue).forEach(key => { + if (key !== noneKey) { + newValue[key] = false; + } + }); + + control.setValue(newValue, { emitEvent: false }); + } else if (curr[noneKey] === true) { + control.patchValue({ [noneKey]: false }, { emitEvent: false }); + } + }); + } + + private getNoneOptionIndex(options: OptionType[] | undefined): number | null { + if (!options) return null; + return options.findIndex(option => + this.getOptionValue(option)?.toLowerCase().includes('none') + ); + } } From 2b3a8cf10a31b1c14c14b995a5bfe4d33cc637a3 Mon Sep 17 00:00:00 2001 From: kurilova Date: Tue, 30 Dec 2025 14:17:32 +0000 Subject: [PATCH 2/2] Lint fix --- .../src/app/components/dynamic-form/dynamic-form.component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts b/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts index 778685bae..ce1297f4c 100644 --- a/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts +++ b/modules/ui/src/app/components/dynamic-form/dynamic-form.component.ts @@ -17,7 +17,8 @@ import { Component, HostListener, inject, - Input, OnDestroy, + Input, + OnDestroy, OnInit, Renderer2, viewChildren,