Skip to content

Commit f9206dd

Browse files
jwshinjwshinjosephperrott
authored andcommitted
fix(datepicker): fix wrong datepicker-input value for non MM/DD/YYYY locales (angular#6798)
* fix date change value for internationalization * change value setting logic + add test
1 parent 9384468 commit f9206dd

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/lib/datepicker/datepicker-input.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
112112
/** The value of the input. */
113113
@Input()
114114
get value(): D | null {
115-
return this._getValidDateOrNull(this._dateAdapter.parse(
116-
this._elementRef.nativeElement.value, this._dateFormats.parse.dateInput));
115+
return this._value;
117116
}
118117
set value(value: D | null) {
119118
if (value != null && !this._dateAdapter.isDateInstance(value)) {
@@ -123,12 +122,14 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
123122
value = this._getValidDateOrNull(value);
124123

125124
let oldDate = this.value;
125+
this._value = value;
126126
this._renderer.setProperty(this._elementRef.nativeElement, 'value',
127127
value ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '');
128128
if (!this._dateAdapter.sameDate(oldDate, value)) {
129129
this._valueChange.emit(value);
130130
}
131131
}
132+
private _value: D | null;
132133

133134
/** The minimum valid date. */
134135
@Input()
@@ -285,6 +286,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
285286
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
286287
this._lastValueValid = !date || this._dateAdapter.isValid(date);
287288
date = this._getValidDateOrNull(date);
289+
this._value = date;
288290
this._cvaOnChange(date);
289291
this._valueChange.emit(date);
290292
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));

src/lib/datepicker/datepicker.spec.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import {MdDatepicker} from './datepicker';
1111
import {MdDatepickerInput} from './datepicker-input';
1212
import {MdInputModule} from '../input/index';
1313
import {MdNativeDateModule} from '../core/datetime/index';
14-
import {DEC, JAN} from '../core/testing/month-constants';
14+
import {DEC, JAN, SEP} from '../core/testing/month-constants';
1515
import {createKeyboardEvent, dispatchEvent} from '@angular/cdk/testing';
1616
import {MdFormFieldModule} from '../form-field/index';
17+
import {MAT_DATE_LOCALE} from '../core/datetime/date-adapter';
18+
import {NativeDateModule} from '../core/datetime/index';
1719

1820
describe('MdDatepicker', () => {
1921
afterEach(inject([OverlayContainer], (container: OverlayContainer) => {
@@ -943,6 +945,45 @@ describe('MdDatepicker', () => {
943945
});
944946

945947
});
948+
949+
describe('internationalization', () => {
950+
let fixture: ComponentFixture<DatepickerWithi18n>;
951+
let testComponent: DatepickerWithi18n;
952+
let input: HTMLInputElement;
953+
954+
beforeEach(async(() => {
955+
TestBed.configureTestingModule({
956+
imports: [
957+
MdDatepickerModule,
958+
MdFormFieldModule,
959+
MdInputModule,
960+
MdNativeDateModule,
961+
NoopAnimationsModule,
962+
NativeDateModule,
963+
FormsModule
964+
],
965+
providers: [{provide: MAT_DATE_LOCALE, useValue: 'de-DE'}],
966+
declarations: [DatepickerWithi18n],
967+
}).compileComponents();
968+
969+
fixture = TestBed.createComponent(DatepickerWithi18n);
970+
fixture.detectChanges();
971+
testComponent = fixture.componentInstance;
972+
input = fixture.nativeElement.querySelector('input') as HTMLInputElement;
973+
}));
974+
975+
it('should have the correct input value even when inverted date format', () => {
976+
let selected = new Date(2017, SEP, 1);
977+
testComponent.date = selected;
978+
fixture.detectChanges();
979+
980+
fixture.whenStable().then(() => {
981+
fixture.detectChanges();
982+
expect(input.value).toBe('01.09.2017');
983+
expect(testComponent.datepickerInput.value).toBe(selected);
984+
});
985+
});
986+
});
946987
});
947988

948989

@@ -1099,3 +1140,15 @@ class DatepickerWithChangeAndInputEvents {
10991140

11001141
onDateInput() {}
11011142
}
1143+
1144+
@Component({
1145+
template: `
1146+
<input [mdDatepicker]="d" [(ngModel)]="date">
1147+
<md-datepicker #d></md-datepicker>
1148+
`
1149+
})
1150+
class DatepickerWithi18n {
1151+
date: Date | null = new Date(2010, JAN, 1);
1152+
@ViewChild('d') datepicker: MdDatepicker<Date>;
1153+
@ViewChild(MdDatepickerInput) datepickerInput: MdDatepickerInput<Date>;
1154+
}

0 commit comments

Comments
 (0)