Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): support "required" indicator for custom form-controls #2068

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
FormGroupDirective,
NgControl,
NgForm,
Validators,
} from "@angular/forms";
import { MatFormFieldControl } from "@angular/material/form-field";
import {
Expand All @@ -28,7 +29,16 @@ export abstract class CustomFormControlDirective<T>
// eslint-disable-next-line @angular-eslint/no-input-rename
@Input("aria-describedby") userAriaDescribedBy: string;
@Input() placeholder: string;
@Input() required = false;

@Input()
get required() {
return this._required;
}
set required(req: boolean) {
this._required = coerceBooleanProperty(req);
this.stateChanges.next();
}
private _required = false;

abstract inputElement: { _elementRef: ElementRef<HTMLElement> };
stateChanges = new Subject<void>();
Expand Down Expand Up @@ -139,5 +149,15 @@ export abstract class CustomFormControlDirective<T>
this.errorState = newState;
this.stateChanges.next();
}

if (control) {
if (control.hasValidator(Validators.required)) {
this.required = true;
this.stateChanges.next();
} else if (this.required) {
this.required = false;
this.stateChanges.next();
}
sleidig marked this conversation as resolved.
Show resolved Hide resolved
}
}
}