Reputation: 193
I created a custom input component for times. I can successfully set the initial value received by [(ngModel)], however any changes to the input field are not being processed. To be more precise: writeValue gets called initially, the setter does not.
The component looks as follows:
@Component({
selector: 'app-timepicker',
templateUrl: './timepicker.component.html',
styleUrls: ['./timepicker.component.scss'],
providers: [{provide: MatFormFieldControl, useExisting: forwardRef(() => NexupTimepickerComponent)}]
})
export class NexupTimepickerComponent implements OnDestroy, ControlValueAccessor {
autofilled: boolean;
// Subject for the changes on the input
stateChanges = new Subject<void>();
// Grouping of the input fields
parts: FormGroup;
public _onChange: any;
constructor(fb: FormBuilder, private fm: FocusMonitor, private elRef: ElementRef<HTMLElement>,
@Optional() @Self() public ngControl: NgControl
) {
this.parts = fb.group({
'hour': '',
'minute': ''
});
// control focus
fm.monitor(elRef.nativeElement, true).subscribe(origin => {
this.focused = !!origin;
this.stateChanges.next();
});
this._onChange = (_: any) => {
};
if (this.ngControl) {
this.ngControl.valueAccessor = this;
}
}
// value
@Input()
get value(): Time | null {
console.log('Getting value');
const n = this.parts.value;
if (n.hour.length === 2 && n.minute.length === 2) {
return new Time(n.hour, n.minute);
}
return null;
}
set value(time: Time | null) {
console.log('Setting value to: ', time);
time = time || new Time('', '');
this.parts.setValue({hour: time.hour, minute: time.minute});
this.stateChanges.next();
}
ngOnDestroy() {
this.stateChanges.complete();
this.fm.stopMonitoring(this.elRef.nativeElement);
}
registerOnChange(fn: any): void {
console.log('registered');
this._onChange = fn;
}
registerOnTouched(fn: any): void {
}
setDisabledState(isDisabled: boolean): void {
}
writeValue(value: any): void {
console.log('received value: ', value);
if ((value !== this.parts.getRawValue()) && value) {
console.log('writing value to: ', value);
this.parts.setValue({hour: value.hour, minute: value.minute});
console.log('new value: ', this.parts.getRawValue());
}
}
}
The HTML in the parent component is this:
<mat-form-field appearance="outline">
<mat-label>End Time</mat-label>
<app-timepicker [required]="true"
[(ngModel)]="endTimeObject" #endTime="ngModel" name="endTime" ngDefaultControl>
</app-timepicker>
</mat-form-field>
Upvotes: 2
Views: 5264
Reputation: 2566
If the setter: set value is not called it's normally cause you don't have change the "value" value, you should have done somethine like this.value = "...";
To do this and for more control I generally add a [formControl]="myControl" on the inner input of my component (if I have one :)) and do something like this in the component :
myControl = new FormControl();
this.myControl.onStatusChange.subscribe( (newValue) => {
this.value = newValue;
});
Futhermore it will be normal that [(ngModel)] will be not update to date cause you're never calling the this._onChange() callback.
The registerOnChage method give you a callback that you must call when you are updating the value from inside the component to notify the ngModel/formControl. I supposed you should add in set value :
set value(time: Time | null) {
console.log('Setting value to: ', time);
time = time || new Time('', '');
const newValue = {hour: time.hour, minute: time.minute};
this.parts.setValue(newValue );
this._onChange(newValue); // call the callback :)
this.stateChanges.next();
}
For more information here you can find a default implementation of the controlValueAccessor. You could extend this class in every component working with ngModel and simple change the value (this.value = 'newValue') and also override the writeValue cause it generally need to change to feet your component needs. https://github.com/xrobert35/asi-ngtools/blob/master/src/components/common/default-control-value-accessor.ts
Upvotes: 1