Skip to content

Commit

Permalink
feat(Form): created form disable directive
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Sep 12, 2020
1 parent 8e73a47 commit 2918118
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ export class FooComponent {

Additional form components, directives, validators etc.

### Form Disabled

Shorthand to disable/enable all controls in a form.

Usage:
```html
<!-- basic usage -->
<form [disabled]="foo">
<input ngModel name="username" />
<input ngModel name="email" />
</form>

<!-- shorthand for disabled: true -->
<form disabled>
<input ngModel name="username" />
<input ngModel name="email" />
</form>

<!-- shorthand for disabled: false -->
<form disabled="false">
<input ngModel name="username" />
<input ngModel name="email" />
</form>

```

### Image Input

Makes a form component from an img element
Expand Down
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 projects/ng-utils/src/form/directives/form-disabled.directive.ts
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]();
}
});
}
}
1 change: 1 addition & 0 deletions projects/ng-utils/src/form/directives/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './form-disabled.directive';
9 changes: 9 additions & 0 deletions projects/ng-utils/src/form/form.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ImageInputModule } from './image-input';
import { FormDisabledDirective } from './directives';

const MODULES = [
CommonModule,
FormsModule,
ImageInputModule,
];

const DIRECTIVES = [
FormDisabledDirective
];

@NgModule({
imports: [
...MODULES,
],
declarations: [
...DIRECTIVES,
],
exports: [
...MODULES,
...DIRECTIVES
]
})
export class FormModule { }
3 changes: 3 additions & 0 deletions projects/ng-utils/src/form/index.ts
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';
2 changes: 1 addition & 1 deletion projects/ng-utils/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

export * from './advanced-route/index';
export * from './form/image-input/index';
export * from './form/index';
export * from './resize/index';
export * from './substitute/index';
export * from './overlay/index';
Expand Down

0 comments on commit 2918118

Please sign in to comment.