Skip to content

Commit

Permalink
fix(datepicker): support reactive forms
Browse files Browse the repository at this point in the history
fixes #893, fixes #1207
  • Loading branch information
valorkin committed Dec 7, 2016
1 parent 0414afa commit 83fe9db
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/datepicker/datepicker.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {
Component, EventEmitter, Input, Output, Self, ViewChild
} from '@angular/core';
import { Component, EventEmitter, Input, Output, ViewChild, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { DatePickerInnerComponent } from './datepicker-inner.component';
import { ControlValueAccessor, NgModel } from '@angular/forms';
import { DatepickerConfig } from './datepicker.config';

export const DATEPICKER_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DatePickerComponent),
multi: true
};

/* tslint:disable:component-selector-name component-selector-type */
@Component({
selector: 'datepicker[ngModel]',
selector: 'datepicker',
template: `
<datepicker-inner [activeDate]="activeDate"
(update)="onUpdate($event)"
Expand Down Expand Up @@ -38,7 +42,7 @@ import { DatepickerConfig } from './datepicker.config';
<yearpicker tabindex="0"></yearpicker>
</datepicker-inner>
`,
providers: [NgModel]
providers: [DATEPICKER_CONTROL_VALUE_ACCESSOR]
})
/* tslint:enable:component-selector-name component-selector-type */
export class DatePickerComponent implements ControlValueAccessor {
Expand Down Expand Up @@ -80,16 +84,12 @@ export class DatePickerComponent implements ControlValueAccessor {
public onChange: any = Function.prototype;
public onTouched: any = Function.prototype;

public cd: NgModel;
protected _now: Date = new Date();
protected _activeDate: Date;
protected config: DatepickerConfig;

public constructor(@Self() cd: NgModel, config: DatepickerConfig) {
public constructor(config: DatepickerConfig) {
this.config = config;
this.cd = cd;
// hack
cd.valueAccessor = this;
this.configureOptions();
}

Expand All @@ -98,7 +98,7 @@ export class DatePickerComponent implements ControlValueAccessor {
}

public onUpdate(event: any): void {
this.cd.viewToModelUpdate(event);
this.onChange(event);
}

public onSelectionDone(event: Date): void {
Expand Down

0 comments on commit 83fe9db

Please sign in to comment.