Skip to content

Instantly share code, notes, and snippets.

@dougal83
Created July 4, 2024 20:58
Show Gist options
  • Save dougal83/0aa258c5b7dedca7575c4ad73784855b to your computer and use it in GitHub Desktop.
Save dougal83/0aa258c5b7dedca7575c4ad73784855b to your computer and use it in GitHub Desktop.
Add [disabled] directive to FormGroup for convenience. Intended for use with signals.
import { Directive, effect, input } from '@angular/core';
import { FormGroupDirective } from '@angular/forms';
/**
* Add [disabled] directive to FormGroup for convenience.
*
* @usageNotes
* Give convienient utility for disabling form submit with 'form.disabled'.
* <button type="submit" [disabled]="form.invalid || form.disabled">Submit</button>
*/
@Directive({
standalone: true,
// eslint-disable-next-line @angular-eslint/directive-selector
selector: 'form[disabled]',
})
export class FormDisabledDirective {
disabled = input<boolean>(false);
constructor(private host: FormGroupDirective) {
effect(() => {
if (this.disabled()) {
this.host.control.disable();
} else {
this.host.control.enable();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment