-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Form): created form disable directive
- Loading branch information
1 parent
8e73a47
commit 2918118
Showing
7 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
projects/ng-utils/src/form/directives/form-disabled.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { FormDisabledDirective } from './form-disabled.directive'; | ||
|
||
describe('FormDisabledDirective', () => { | ||
it('should create an instance', () => { | ||
const directive = new FormDisabledDirective(); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
projects/ng-utils/src/form/directives/form-disabled.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Directive, Host, Input } from '@angular/core'; | ||
import { NgForm } from '@angular/forms'; | ||
import { difference } from '@thalesrc/js-utils/legacy'; | ||
import { combineLatest, Observable } from 'rxjs'; | ||
import { distinctUntilChanged, map, takeUntil } from 'rxjs/operators'; | ||
import { InputStream } from '../../utils/input-stream'; | ||
import { Unsubscriber } from '../../utils/unsubscriber'; | ||
|
||
@Directive({ | ||
// tslint:disable-next-line:directive-selector | ||
selector: 'form:not([ngNoForm]):not([formGroup])[disabled], ng-form[disabled], [ng-form][disabled]', | ||
exportAs: 'thaDisabled' | ||
}) | ||
export class FormDisabledDirective extends Unsubscriber { | ||
// tslint:disable-next-line:no-input-rename | ||
@Input('disabled') | ||
@InputStream(false) | ||
private disabled$: Observable<boolean>; | ||
|
||
constructor( | ||
@Host() form: NgForm | ||
) { | ||
super(); | ||
|
||
const controls$ = form.valueChanges.pipe( | ||
map(Object.keys), | ||
distinctUntilChanged((oldKeys, newKeys) => !difference(oldKeys, newKeys).length), | ||
map(() => Object.values(form.controls)) | ||
); | ||
|
||
const disabled$ = this.disabled$.pipe( | ||
map((value: unknown) => value === '' | ||
? true | ||
: value === 'false' | ||
? false | ||
: !!value) | ||
); | ||
|
||
combineLatest([controls$, disabled$]).pipe(takeUntil(this.onDestroy$)).subscribe(([controls, readonly]) => { | ||
const method = readonly ? 'disable' : 'enable'; | ||
|
||
for (const control of controls) { | ||
control[method](); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './form-disabled.directive'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './form.module'; | ||
export * from './directives/index'; | ||
export * from './image-input/index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters