Skip to content

Instantly share code, notes, and snippets.

@lydemann
Created January 2, 2020 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydemann/d6048440e7d7f06e1ae6c286e5cf9931 to your computer and use it in GitHub Desktop.
Save lydemann/d6048440e7d7f06e1ae6c286e5cf9931 to your computer and use it in GitHub Desktop.
data-picker.component.ts
@Component({
selector: 'app-data-picker',
templateUrl: './data-picker.component.html',
styleUrls: ['./data-picker.component.scss'],
})
export class DataPickerComponent implements ControlValueAccessor {
@Input()
public date: Date;
@Input() public minDate: Date;
@Input() public maxDate: Date;
@Input() public showError: Date;
@Input() public errorMessage: string = 'Invalid input';
@Input() public placeholder: string = 'Choose a date';
public dateChange: EventEmitter<Date> = new EventEmitter();
public isDisabled = false;
private onTouched = Function;
// tslint:disable-next-line: member-ordering
public hasError: boolean = this.ngControl.invalid;
constructor(private ngControl: NgControl) {
ngControl.valueAccessor = this;
}
public onDateChange(dateInput: MatDatepickerInputEvent<Date>) {
const date = dateInput.target.value;
this.onChange(date);
this.onTouched();
this.dateChange.next(date);
}
public writeValue(obj: Date): void {
this.date = obj;
}
public registerOnChange(fn: any): void {
this.onChange = fn;
}
public registerOnTouched(fn: any): void {
this.onTouched = fn;
}
public setDisabledState?(isDisabled: boolean): void {
this.isDisabled = isDisabled;
}
// tslint:disable-next-line: no-empty
private onChange = (val: any) => {};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment