Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datepicker): changing model value changes the view correctly #1041

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions components/datepicker/datepicker.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, EventEmitter, Input, Output, Self } from '@angular/core';
import { Component, EventEmitter, Input, Output, Self, ViewChild } from '@angular/core';
import { DatePickerInnerComponent } from './datepicker-inner.component';
import { ControlValueAccessor, NgModel } from '@angular/forms';

/* tslint:disable:component-selector-name component-selector-type */
Expand Down Expand Up @@ -66,6 +67,8 @@ export class DatePickerComponent implements ControlValueAccessor {
private _now:Date = new Date();
private _activeDate:Date;

@ViewChild(DatePickerInnerComponent) private datePicker: DatePickerInnerComponent;

@Input()
public get activeDate():Date {
return this._activeDate || this._now;
Expand All @@ -82,7 +85,6 @@ export class DatePickerComponent implements ControlValueAccessor {
}

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

Expand All @@ -92,19 +94,12 @@ export class DatePickerComponent implements ControlValueAccessor {

// todo: support null value
public writeValue(value:any):void {
// todo: fix something sends here new date all the time
// if (value) {
// if (typeof value !== 'Date') {
// value = new Date(value);
// }
//
// this.activeDate = value;
// }
if (value === this._activeDate) {
if (this.datePicker.compare(value, this._activeDate) === 0) {
return;
}
if (value && value instanceof Date) {
this.activeDate = value;
this.datePicker.select(value);
return;
}

Expand Down