Reputation: 6857
I'm looking at this question, trying to figure out how to extend FormControlDirective: Attempting to extend FormControlDirective to implement my own FormControl directive results in faulty binding.
There is an answer, but I'm not sure what is meant by:
The
formControl
\formControlName
selectors appear in one more place - the value accessor. In order your directive to work you should implement all default value accessors for thehybridFormControl
directive ( following the pattern for the built-in directives).
Here is my code:
export const formControlBinding: any = {
provide: NgControl,
useExisting: forwardRef(() => ControlDirective)
};
@Directive({
selector: '[appControl]',
providers: [formControlBinding],
exportAs: 'ngForm'
})
export class ControlDirective extends FormControlDirective implements OnInit {
constructor(
@Optional() @Self() @Inject(NG_VALIDATORS) validators: Array<Validator|ValidatorFn>,
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: Array<AsyncValidator|AsyncValidatorFn>,
@Optional() @Self() @Inject(NG_VALUE_ACCESSOR) valueAccessors: ControlValueAccessor[],
public renderer: Renderer2,
public hostElement: ElementRef,
) {
super(validators, asyncValidators, valueAccessors);
}
@Input() set appControl(form: FormControl) {
console.log(form);
this.form = form;
}
}
It is very similar to @ronif's Plunker from his question. set appControl
does run, even though I'm passing a value like <input type="text" class="form-control" appControl="firstName">
, and FormControlDirective._rawValidators
seems to always be an empty array, even though the FormGroup
works with the standard FormControlDirective
.
How would I go about 'implementing all default value accessors'?
Upvotes: 2
Views: 1990
Reputation: 2389
In case anyone else runs into something like this, here is the solution I came up with. I wanted to dynamically create a form control model from the child form control element. This is so I didn't have to write an initial model with tons of form control fields while still getting the benefit of the reactive forms model. This is how to extend the FormControlName class:
@Directive({
selector: '[hybridFormControl]'
})
class HybridFormControl Directive extends FormControlName implements ControlValueAccessor, OnChanges {
@Input('hybridFormControl') name: string;
onChange;
onTouched;
constructor(
@Optional() protected formGroupDirective: FormGroupDirective,
@Optional() @Self() @Inject(NG_VALUE_ACCESSOR) valueAccessors: ControlValueAccessor[],
private fb: FormBuilder,
private renderer: Renderer2,
private element: ElementRef
) {
super(formGroupDirective, [], [], valueAccessors, null);
this.valueAccessor = this;
}
ngOnChanges(changes: SimpleChanges): void {
if (!this._registered) {
// dynamically create the form control model on the form group model.
this.formGroup = this.formGroupDirective.form;
this.formGroup.registerControl(name, this.fb.control(''));
this._registered = true;
}
// IMPORTANT - this ties your extended form control to the form control
// model on the form group model that we just created above. Take a look
// at Angular github source code.
super.ngOnChanges(changes);
}
@HostListener('input', ['$event.target.value'])
@HostListener('change', ['$event.target.value'])
onInput(value): void {
this.onChange(modelValue);
}
writeValue(value): void {
const element = this.element.nativeElement;
this.renderer.setProperty(element, 'value', value);
}
registerOnChange(fn): void {
this.onChange = fn;
}
registerOnTouched(fn): void {
this.onTouched = fn;
}
}
And you would use this new hybrid directive like:
@Component({
selector: 'app',
template: `
<form formGroup=[formGroup]>
<input type="text" hybridFormControl="myName">
</form>
`
class AppComponent {
formGroup: FormGroup
constructor(fb: FormBuilder) {
this.form = this.fb.group({});
}
}
You will need to modify this.formGroup.registerControl(name, this.fb.control(''));
line of code if you want to pass validators. I haven't verified this, but hopefully this helps someone else who runs across this question.
Upvotes: 2