Created
July 4, 2024 20:58
-
-
Save dougal83/0aa258c5b7dedca7575c4ad73784855b to your computer and use it in GitHub Desktop.
Add [disabled] directive to FormGroup for convenience. Intended for use with signals.
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
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