There is no built-in functionality for propagating touched
status to inner FormControl of custom control.
Your simple option would be checking status in ngDoCheck
and once custom control becomes touched update status for inner FormControl
:
ngDoCheck() {
if (this.formControl.touched) {
return;
}
if (this.controlDir.control.touched) {
this.formControl.markAsTouched();
}
}
Forked Stackblitz
Personally, I don't like such kind of implementations with ControlValueAccessor
.
I would rather use the same FormControl. This can be done by adding viewProviders
with ControlValueAccessor
provider to your custom control:
custom-control.component.ts
@Component({
selector: 'my-custom-control',
template: `
<mat-form-field id="userType">
<mat-label>My Custom Component</mat-label>
<mat-select [formControlName]="controlName" (blur)="onTouched()">
<mat-option *ngFor="let current of userTypes" [value]="current.id">{{current.name}}</mat-option>
</mat-select>
</mat-form-field>
`,
viewProviders: [{
provide: ControlContainer,
useFactory: (container: ControlContainer) => container,
deps: [[new SkipSelf(), ControlContainer]],
}]
})
export class MyCustomControl {
@Input() controlName: string;
userTypes: LookupModel[] = [
new LookupModel(1, 'first'),
new LookupModel(2, 'second')
];
}
parent html
<form [formGroup]="form">
<my-custom-control controlName="userTypeCustomControl"></my-custom-control>
Stackblitz Example