-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AAE-27343 Listen to form rules changes from reactive widgets #10392
Changes from all commits
85c7049
1270085
fbdec84
96ff193
7935d17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,6 +20,7 @@ import { | |||||||||||||||||||||||||||||||||
Component, | ||||||||||||||||||||||||||||||||||
ComponentFactory, | ||||||||||||||||||||||||||||||||||
ComponentRef, | ||||||||||||||||||||||||||||||||||
DestroyRef, | ||||||||||||||||||||||||||||||||||
inject, | ||||||||||||||||||||||||||||||||||
Input, | ||||||||||||||||||||||||||||||||||
NgModule, | ||||||||||||||||||||||||||||||||||
|
@@ -33,6 +34,8 @@ import { FormRenderingService } from '../../services/form-rendering.service'; | |||||||||||||||||||||||||||||||||
import { WidgetVisibilityService } from '../../services/widget-visibility.service'; | ||||||||||||||||||||||||||||||||||
import { FormFieldModel } from '../widgets/core/form-field.model'; | ||||||||||||||||||||||||||||||||||
import { FieldStylePipe } from '../../pipes/field-style.pipe'; | ||||||||||||||||||||||||||||||||||
import { FormFieldTypes } from '../widgets/core/form-field-types'; | ||||||||||||||||||||||||||||||||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
declare const adf: any; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
@@ -62,6 +65,7 @@ export class FormFieldComponent implements OnInit, OnDestroy { | |||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private readonly formRenderingService = inject(FormRenderingService); | ||||||||||||||||||||||||||||||||||
private readonly visibilityService = inject(WidgetVisibilityService); | ||||||||||||||||||||||||||||||||||
private readonly destroyRef = inject(DestroyRef); | ||||||||||||||||||||||||||||||||||
private readonly compiler = inject(Compiler); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
ngOnInit() { | ||||||||||||||||||||||||||||||||||
|
@@ -88,14 +92,29 @@ export class FormFieldComponent implements OnInit, OnDestroy { | |||||||||||||||||||||||||||||||||
instance.fieldChanged.subscribe((field) => { | ||||||||||||||||||||||||||||||||||
if (field && this.field.form) { | ||||||||||||||||||||||||||||||||||
this.visibilityService.refreshVisibility(field.form); | ||||||||||||||||||||||||||||||||||
field.form.onFormFieldChanged(field); | ||||||||||||||||||||||||||||||||||
this.triggerFormFieldChanged(field); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if (FormFieldTypes.isReactiveType(instance?.field?.type)) { | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was expecting this to be called when something has changed into the form field so inside the fieldChanged stream There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm why? we subscribe to the formRulesEvent when we create this component instance, why do we need to put it under another subscription (nested)? it should listen for these events for the whole lifecycle of the instance There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to subscribe to the form rules here, you just need to put your part of the code (the if) into the fieldChanged subscription, otherwise other event relying on triggering a change on the form field will fail to update the validation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we do not subscribe for formRules changes, we wont be able to update the state of particular instance until the instance.fieldChanged will be triggered (so for example after blur, or input something event).
Suggested change
(i tested it and it doesnt work) |
||||||||||||||||||||||||||||||||||
this.updateReactiveFormControlOnFormRulesEvent(instance); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private updateReactiveFormControlOnFormRulesEvent(instance: any): void { | ||||||||||||||||||||||||||||||||||
instance?.formService.formRulesEvent.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => { | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reducing the check to only the form rules might miss some other parts where we update the form and the field changed event is triggered There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on each onFieldChanged call, inside the base widget.component.ts , and also on the all events, we update the value of formRulesEvent subject so it will be always triggered for all the events (and its needed for all events, because in modeling, we can set rules for all available event types, like blur, click etc). i think it should be enough, but maybe im not aware of some corner case |
||||||||||||||||||||||||||||||||||
instance?.updateReactiveFormControl(); | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add an interface to
and implement it in reactive widgets
So Also we can add some comment to this interface, explaining what it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, will add it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you add an interface (or an abstract class like WidgetComponent) please double check with a custom form widget if it's all good |
||||||||||||||||||||||||||||||||||
this.triggerFormFieldChanged(instance.field); | ||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private triggerFormFieldChanged(field: FormFieldModel): void { | ||||||||||||||||||||||||||||||||||
field.form.onFormFieldChanged(field); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
ngOnDestroy() { | ||||||||||||||||||||||||||||||||||
if (this.componentRef) { | ||||||||||||||||||||||||||||||||||
this.componentRef.destroy(); | ||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -57,15 +57,14 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit { | |||||
maxDate: Date; | ||||||
datetimeInputControl: FormControl<Date> = new FormControl<Date>(null); | ||||||
|
||||||
|
||||||
public readonly formService = inject(FormService); | ||||||
|
||||||
private readonly destroyRef = inject(DestroyRef); | ||||||
private readonly dateAdapter = inject(DateAdapter); | ||||||
private readonly dateTimeAdapter = inject(DatetimeAdapter); | ||||||
|
||||||
ngOnInit(): void { | ||||||
this.patchFormControl(); | ||||||
this.setFormControlValue(); | ||||||
this.updateFormControlState(); | ||||||
this.initDateAdapter(); | ||||||
this.initDateRange(); | ||||||
this.subscribeToDateChanges(); | ||||||
|
@@ -77,12 +76,20 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit { | |||||
this.onFieldChanged(this.field); | ||||||
} | ||||||
|
||||||
private patchFormControl(): void { | ||||||
updateReactiveFormControl(): void { | ||||||
this.updateFormControlState(); | ||||||
this.validateField(); | ||||||
} | ||||||
|
||||||
private setFormControlValue(): void { | ||||||
this.datetimeInputControl.setValue(this.field.value, { emitEvent: false }); | ||||||
} | ||||||
|
||||||
private updateFormControlState(): void { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think state in this case better describe what this method does, because it updates also readonly/disabled state (required is also state of the field) |
||||||
this.datetimeInputControl.setValidators(this.isRequired() ? [Validators.required] : []); | ||||||
if (this.field?.readOnly || this.readOnly) { | ||||||
this.datetimeInputControl.disable({ emitEvent: false }); | ||||||
} | ||||||
this.field?.readOnly || this.readOnly | ||||||
? this.datetimeInputControl.disable({ emitEvent: false }) | ||||||
: this.datetimeInputControl.enable({ emitEvent: false }); | ||||||
|
||||||
this.datetimeInputControl.updateValueAndValidity({ emitEvent: false }); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import { WidgetComponent } from '../widget.component'; | |
import { ErrorMessageModel } from '../core/error-message.model'; | ||
import { parseISO } from 'date-fns'; | ||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
import { ReactiveFormWidget } from '../reactive-widget.interface'; | ||
|
||
@Component({ | ||
selector: 'date-widget', | ||
|
@@ -55,7 +56,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | |
imports: [MatFormFieldModule, TranslateModule, MatInputModule, MatDatepickerModule, ReactiveFormsModule, ErrorWidgetComponent, NgIf], | ||
encapsulation: ViewEncapsulation.None | ||
}) | ||
export class DateWidgetComponent extends WidgetComponent implements OnInit { | ||
export class DateWidgetComponent extends WidgetComponent implements OnInit, ReactiveFormWidget { | ||
minDate: Date; | ||
maxDate: Date; | ||
startAt: Date; | ||
|
@@ -68,7 +69,8 @@ export class DateWidgetComponent extends WidgetComponent implements OnInit { | |
private readonly destroyRef = inject(DestroyRef); | ||
|
||
ngOnInit(): void { | ||
this.patchFormControl(); | ||
this.setFormControlValue(); | ||
this.updateFormControlState(); | ||
this.initDateAdapter(); | ||
this.initDateRange(); | ||
this.initStartAt(); | ||
|
@@ -80,12 +82,21 @@ export class DateWidgetComponent extends WidgetComponent implements OnInit { | |
this.validateField(); | ||
this.onFieldChanged(this.field); | ||
} | ||
private patchFormControl(): void { | ||
|
||
updateReactiveFormControl(): void { | ||
this.updateFormControlState(); | ||
this.validateField(); | ||
} | ||
|
||
private setFormControlValue(): void { | ||
this.dateInputControl.setValue(this.field.value, { emitEvent: false }); | ||
} | ||
|
||
private updateFormControlState(): void { | ||
this.dateInputControl.setValidators(this.isRequired() ? [Validators.required] : []); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use custom validators in date widget? 🤔 or only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as default option we use only required from default validators. for other cases, like min/max constraints, we handle it in the handleErrors method, and its needed to be compatible with existing form structure + other non-reactive form widgets. |
||
if (this.field?.readOnly || this.readOnly) { | ||
this.dateInputControl.disable({ emitEvent: false }); | ||
} | ||
this.field?.readOnly || this.readOnly | ||
? this.dateInputControl.disable({ emitEvent: false }) | ||
: this.dateInputControl.enable({ emitEvent: false }); | ||
|
||
this.dateInputControl.updateValueAndValidity({ emitEvent: false }); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { FormService } from '../../services/form.service'; | ||
|
||
export interface ReactiveFormWidget { | ||
updateReactiveFormControl(): void; | ||
formService: FormService; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth to have a method just for a single line of code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method now is used in two places, so thats why i move it to separate method, but i can agree that maybe its not worth