import { Directive , ElementRef , forwardRef , HostListener , Renderer2 } from '@angular/core' ; import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ; /**
 * The accessor for writing a date object value and listening to changes on a date input element.
 *
 *  ### Example
 *  `<input type="date" name="myBirthday" ngModel useValueAsDate>`
 */ @ Directive ( { selector : '[useValueAsDate]' , providers : [ { provide : NG_VALUE_ACCESSOR , useExisting : forwardRef ( ( ) => DateValueAccessor ) , multi : true } ] } ) export class DateValueAccessor implements ControlValueAccessor { @ HostListener ( 'input' , [ '$event.target.valueAsDate' ] ) onChange = ( _ ) => { } ; @ HostListener ( 'blur' , [ ] ) onTouched = ( ) => { } ; constructor ( private renderer , private elementRef ) { } writeValue ( date ? ) { this . renderer . setProperty ( this . elementRef . nativeElement , 'valueAsDate' , date ) ; } registerOnChange ( fn ) { this . onChange = fn ; } registerOnTouched ( fn ) { this . onTouched = fn ; } setDisabledState ( isDisabled ) { this . renderer . setProperty ( this . elementRef . nativeElement , 'disabled' , isDisabled ) ; } }