diff --git a/README.md b/README.md index d992215a..b48de7b3 100644 --- a/README.md +++ b/README.md @@ -163,9 +163,15 @@ Every time the form changes, that component will `emit` a value from the `dataOu From the parent component you can do like the following: ```html - + ``` +_Note the presence of disabled, this is an optional input provided by both `NgxRootFormComponent` and `NgxAutomaticRootFormComponent` that let you disable (or enable when true) the whole form._ + Differences between: - `NgxRootFormComponent`: Will never emit the form value automatically when it changes, to emit the value you'll have to call the method `manualSave` when needed diff --git a/projects/ngx-sub-form/src/lib/ngx-root-form.component.spec.ts b/projects/ngx-sub-form/src/lib/ngx-root-form.component.spec.ts index ba97a974..0694384a 100644 --- a/projects/ngx-sub-form/src/lib/ngx-root-form.component.spec.ts +++ b/projects/ngx-sub-form/src/lib/ngx-root-form.component.spec.ts @@ -30,10 +30,16 @@ const getNewValues = (): Required => ({ @Component({ template: ` - + `, }) class TestWrapperComponent { + public disabled: boolean | null | undefined = false; + public vehicle$: BehaviorSubject> = new BehaviorSubject(getDefaultValues()); public vehicleUpdated(vehicle: Required): void {} @@ -54,7 +60,7 @@ class RootFormComponent extends NgxRootFormComponent { @DataInput() // tslint:disable-next-line:no-input-rename @Input('vehicle') - public dataInput: Required | null = null; + public dataInput: Required | null | undefined = null; // tslint:disable-next-line:no-output-rename @Output('vehicleUpdated') @@ -115,4 +121,28 @@ describe(`NgxRootFormComponent`, () => { expect(vehicleUpdatedSpy).toHaveBeenCalledWith(getNewValues()); }); + + it(`should be able to disable/enable the form via the "disabled" input`, () => { + expect(componentForm.formGroup.enabled).toBe(true); + + component.disabled = true; + componentFixture.detectChanges(); + expect(componentForm.formGroup.disabled).toBe(true); + + component.disabled = null; + componentFixture.detectChanges(); + expect(componentForm.formGroup.enabled).toBe(true); + + component.disabled = undefined; + componentFixture.detectChanges(); + expect(componentForm.formGroup.enabled).toBe(true); + + component.disabled = false; + componentFixture.detectChanges(); + expect(componentForm.formGroup.enabled).toBe(true); + + component.disabled = true; + componentFixture.detectChanges(); + expect(componentForm.formGroup.disabled).toBe(true); + }); }); diff --git a/projects/ngx-sub-form/src/lib/ngx-root-form.component.ts b/projects/ngx-sub-form/src/lib/ngx-root-form.component.ts index 76d6b59b..0db17773 100644 --- a/projects/ngx-sub-form/src/lib/ngx-root-form.component.ts +++ b/projects/ngx-sub-form/src/lib/ngx-root-form.component.ts @@ -1,4 +1,4 @@ -import { EventEmitter, OnInit } from '@angular/core'; +import { EventEmitter, OnInit, Input } from '@angular/core'; import isEqual from 'lodash-es/isEqual'; import { BehaviorSubject, Subject } from 'rxjs'; import { filter, tap } from 'rxjs/operators'; @@ -24,6 +24,11 @@ export abstract class NgxRootFormComponent = new Subject(); + @Input() + public set disabled(shouldDisable: boolean | undefined) { + this.setDisabledState(shouldDisable); + } + protected emitInitialValueOnInit = false; protected emitNullOnDestroy = false; diff --git a/projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts b/projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts index 941d4a2c..6fca6600 100644 --- a/projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts +++ b/projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts @@ -300,7 +300,7 @@ export abstract class NgxSubFormComponentReadonly - diff --git a/src/app/main/listing/listing-form/listing-form.component.scss b/src/app/main/listing/listing-form/listing-form.component.scss index ace4f012..1841500f 100644 --- a/src/app/main/listing/listing-form/listing-form.component.scss +++ b/src/app/main/listing/listing-form/listing-form.component.scss @@ -30,10 +30,6 @@ mat-form-field { width: 100%; } -.readonly { - padding: 15px 0; -} - .invalid-form { padding: 15px 0; } diff --git a/src/app/main/listing/listing-form/listing-form.component.ts b/src/app/main/listing/listing-form/listing-form.component.ts index 01c2d202..6d1357d7 100644 --- a/src/app/main/listing/listing-form/listing-form.component.ts +++ b/src/app/main/listing/listing-form/listing-form.component.ts @@ -1,4 +1,4 @@ -import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; +import { Component, EventEmitter, Input, Output } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; import { Controls, @@ -36,8 +36,7 @@ interface OneListingForm { styleUrls: ['./listing-form.component.scss'], }) // export class ListingFormComponent extends NgxAutomaticRootFormComponent -export class ListingFormComponent extends NgxRootFormComponent - implements OnInit, OnDestroy { +export class ListingFormComponent extends NgxRootFormComponent { @DataInput() // tslint:disable-next-line:no-input-rename @Input('listing') @@ -49,9 +48,6 @@ export class ListingFormComponent extends NgxRootFormComponent) => Observable { // return NGX_SUB_FORM_HANDLE_VALUE_CHANGES_RATE_STRATEGIES.debounce(500); // } @@ -68,23 +64,6 @@ export class ListingFormComponent extends NgxRootFormComponent { - if (readonly) { - this.formGroup.disable(); - } else { - this.formGroup.enable(); - } - }), - takeUntilDestroyed(this), - ) - .subscribe(); - } - protected transformFromFormGroup(formValue: OneListingForm): OneListing | null { const { vehicleProduct, droidProduct, listingType, ...commonValues } = formValue; diff --git a/src/app/main/listing/listing.component.html b/src/app/main/listing/listing.component.html index 6150f33f..9bad2354 100644 --- a/src/app/main/listing/listing.component.html +++ b/src/app/main/listing/listing.component.html @@ -1 +1,7 @@ - +Readonly + + diff --git a/src/app/main/listing/listing.component.scss b/src/app/main/listing/listing.component.scss index e69de29b..81fb7772 100644 --- a/src/app/main/listing/listing.component.scss +++ b/src/app/main/listing/listing.component.scss @@ -0,0 +1,3 @@ +.readonly { + padding: 15px 0; +} diff --git a/src/app/main/listing/listing.component.ts b/src/app/main/listing/listing.component.ts index 5434b62e..6a782e32 100644 --- a/src/app/main/listing/listing.component.ts +++ b/src/app/main/listing/listing.component.ts @@ -1,5 +1,6 @@ import { Component } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; +import { FormControl } from '@angular/forms'; import { NullableObject } from 'ngx-sub-form'; import { Observable, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; @@ -13,6 +14,8 @@ import { UuidService } from '../../services/uuid.service'; styleUrls: ['./listing.component.scss'], }) export class ListingComponent { + public readonlyFormControl: FormControl = new FormControl(false); + constructor( private route: ActivatedRoute, private listingService: ListingService,