blob: 912c276279256b055db02be56e99462585a5472c [file]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import {Input, OnDestroy, Output} from "@angular/core";
import {FormGroup} from "@angular/forms";
import {defer, merge, of, Subject} from "rxjs";
import {map} from "rxjs/operators";
export abstract class AbstractReactiveFormComponent<T extends object> implements OnDestroy {
@Input()
public abstract appFormGroup: FormGroup;
@Output()
public appValueChange = defer(() => this.appFormGroup.valueChanges).pipe(
map(() => this.getValue())
);
protected value$ = defer(() => merge(of(this.getValue()), this.appValueChange));
protected destroy$ = new Subject();
public ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
@Input("appValue")
public patchValue(value: Partial<T>) {
this.appFormGroup.patchValue(value);
}
public getValue(): T {
return this.appFormGroup.value;
}
}