Skip to content
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

fix value update in readonly form field wrapper #215

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import {
EventEmitter,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
SimpleChanges,
ViewChild,
} from '@angular/core';
import { ControlContainer, FormGroupDirective } from '@angular/forms';
import { ReadOnlyFormFieldComponent } from '../readonly-form-field/readonly-form-field.component';
import { NgIf } from '@angular/common';
import { ObserversModule } from '@angular/cdk/observers';
import {ControlContainer, FormGroupDirective} from '@angular/forms';
import {ReadOnlyFormFieldComponent} from '../readonly-form-field/readonly-form-field.component';
import {NgIf} from '@angular/common';
import {ObserversModule} from '@angular/cdk/observers';
import {Subscription} from 'rxjs';

/**
* Wraps a mat-form-field to replace it by a readOnly representation if necessary
Expand All @@ -30,7 +32,7 @@ import { ObserversModule } from '@angular/cdk/observers';
standalone: true,
imports: [NgIf, ReadOnlyFormFieldComponent, ObserversModule],
})
export class ReadOnlyFormFieldWrapperComponent implements OnInit, AfterViewInit, OnChanges, AfterViewChecked {
export class ReadOnlyFormFieldWrapperComponent implements OnInit, AfterViewInit, OnChanges, AfterViewChecked, OnDestroy {
@ViewChild('contentWrapper', { static: false })
originalContent: ElementRef;
@ViewChild('readOnlyContentWrapper', { static: false })
Expand Down Expand Up @@ -95,6 +97,7 @@ export class ReadOnlyFormFieldWrapperComponent implements OnInit, AfterViewInit,
* Automatically taken from the contained <mat-label>
*/
label: string;
private subscriptions: Subscription[] = [];

constructor(
private changeDetector: ChangeDetectorRef,
Expand All @@ -112,6 +115,10 @@ export class ReadOnlyFormFieldWrapperComponent implements OnInit, AfterViewInit,

ngAfterViewChecked(): void {}

ngOnDestroy() {
this.subscriptions.forEach((subscription) => subscription.unsubscribe());
}

ngOnChanges(_: SimpleChanges): void {
this.doRendering();
}
Expand Down Expand Up @@ -174,6 +181,11 @@ export class ReadOnlyFormFieldWrapperComponent implements OnInit, AfterViewInit,
}
if (form && form.get(formControlName)) {
this.value = form.get(formControlName).getRawValue();
this.subscriptions.push(
form.get(formControlName).valueChanges.subscribe((changedValue) => {
this.value = changedValue;
}),
);
}
}

Expand Down
Loading