AAE-27343 Listen to form rules changes from reactive widgets (#10392)

* AAE-27343 Listen to form rules changes from reactive widgets

* remove leftover

* apply interface for reactive widgets and unit test

* update readonly control status

* [ci:force][link-adf:fix/AAE-27343-listen-to-form-rules-changes-in-reactive-widgets]
This commit is contained in:
Tomasz Gnyp
2024-11-29 10:27:24 +01:00
committed by GitHub
parent 7eb51ef58f
commit 23fe4d4486
9 changed files with 192 additions and 88 deletions

View File

@@ -19,12 +19,13 @@
import { Component, DestroyRef, inject, OnInit, ViewEncapsulation } from '@angular/core';
import {
ErrorMessageModel,
ErrorWidgetComponent,
FormFieldModel,
FormFieldOption,
FormService,
WidgetComponent
FormFieldOption,
WidgetComponent,
ErrorWidgetComponent,
ErrorMessageModel,
FormFieldModel,
ReactiveFormWidget
} from '@alfresco/adf-core';
import { ProcessDefinitionService } from '../../services/process-definition.service';
import { TaskFormService } from '../../services/task-form.service';
@@ -55,15 +56,14 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
},
encapsulation: ViewEncapsulation.None
})
export class DropdownWidgetComponent extends WidgetComponent implements OnInit {
public formsService = inject(FormService);
export class DropdownWidgetComponent extends WidgetComponent implements OnInit, ReactiveFormWidget {
public formService = inject(FormService);
public taskFormService = inject(TaskFormService);
public processDefinitionService = inject(ProcessDefinitionService);
private readonly destroyRef = inject(DestroyRef);
dropdownControl = new FormControl<FormFieldOption | string>(undefined);
private readonly destroyRef = inject(DestroyRef);
get isReadOnlyType(): boolean {
return this.field.type === 'readonly';
}
@@ -93,7 +93,15 @@ export class DropdownWidgetComponent extends WidgetComponent implements OnInit {
}
}
this.initFormControl();
this.setFormControlValue();
this.updateFormControlState();
this.subscribeToInputChanges();
this.handleErrors();
}
updateReactiveFormControl(): void {
this.updateFormControlState();
this.handleErrors();
}
getValuesByTaskId() {
@@ -124,15 +132,7 @@ export class DropdownWidgetComponent extends WidgetComponent implements OnInit {
return !!this.field?.form?.readOnly;
}
private initFormControl() {
if (this.field?.required) {
this.dropdownControl.addValidators([this.customRequiredValidator(this.field)]);
}
if (this.field?.readOnly || this.readOnly) {
this.dropdownControl.disable({ emitEvent: false });
}
private subscribeToInputChanges(): void {
this.dropdownControl.valueChanges
.pipe(
filter(() => !!this.field),
@@ -143,9 +143,19 @@ export class DropdownWidgetComponent extends WidgetComponent implements OnInit {
this.handleErrors();
this.onFieldChanged(this.field);
});
}
private setFormControlValue(): void {
this.dropdownControl.setValue(this.getOptionValue(this.field?.value), { emitEvent: false });
this.handleErrors();
}
private updateFormControlState(): void {
this.dropdownControl.setValidators(this.isRequired() ? [this.customRequiredValidator(this.field)] : []);
this.field?.readOnly || this.readOnly
? this.dropdownControl.disable({ emitEvent: false })
: this.dropdownControl.enable({ emitEvent: false });
this.dropdownControl.updateValueAndValidity({ emitEvent: false });
}
private handleErrors() {
@@ -155,11 +165,13 @@ export class DropdownWidgetComponent extends WidgetComponent implements OnInit {
if (this.dropdownControl.valid) {
this.field.validationSummary = new ErrorMessageModel('');
this.field.markAsValid();
return;
}
if (this.dropdownControl.invalid && this.dropdownControl.errors.required) {
this.field.validationSummary = new ErrorMessageModel({ message: 'FORM.FIELD.REQUIRED' });
this.field.markAsInvalid();
}
}