The task I am trying to solve:
Create re-usable input component wrappers to save time when writing template for forms.
Example of what I mean:
Instead of having to write the following template:
<form [formGroup]="myForm">
<mat-form-field>
<input matInput placeholder="Email" [formControl]="email" required>
<mat-error *ngIf="email.invalid">{{getErrorMessage()}}</mat-error>
</mat-form-field>
<form>
I would like to write:
<form [formGroup]="myForm">
<my-input-component [form]="myForm" [myFormControl]="email" [myFormControlName]="'email'" [label]="'Email'"></my-input-component>
</form>
Where my-input-component looks like:
<mat-form-field [formGroup]="form">
<input
matInput
type="text"
[attr.inputmode]="inputMode"
[placeholder]="label"
[formControlName]="myFormControlName"
/>
<mat-error class="errors" *ngIf="myFormControl.invalid">
<div>{{ getError() }}</div>
</mat-error>
</mat-form-field>
This is working as is but I don't know if this is a good approach to pass the FormGroup and FormControls around as bindings.
After searching online I have continuously come across NG_CONTROL_VALUE_ACCESSOR and was a bit confused if that could or should be used in my scenario.
I don't intend for these components to be "custom" in the sense of using a slider as a form control or something of that nature, but rather just wanted "wrappers" to save some time.
Any suggestions or advice on the topic would be greatly appreciated!
getErrorMessage()is a contract not explicitly defined or enforced as far as I can see; that's problematic IMHO. Considering there's a finite set of input types you'll have to deal with, have you considered making anEmailInputComponentfor example that can be used in an obvious way by anyone needing an email input type?getErrorMessage(), my thought was that this function inside would cover any validation type that would be applied (would have a default case for one-offs). Also, my intention is to have a few of these wrappers, one for each common type e.g. text input, number input, select dropdown, etc.