-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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): fix wrong datepicker-input value for non MM/DD/YYYY locales #6798
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,8 +112,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces | |
/** The value of the input. */ | ||
@Input() | ||
get value(): D | null { | ||
return this._getValidDateOrNull(this._dateAdapter.parse( | ||
this._elementRef.nativeElement.value, this._dateFormats.parse.dateInput)); | ||
return this._value; | ||
} | ||
set value(value: D | null) { | ||
if (value != null && !this._dateAdapter.isDateInstance(value)) { | ||
|
@@ -123,12 +122,14 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces | |
value = this._getValidDateOrNull(value); | ||
|
||
let oldDate = this.value; | ||
this._value = value; | ||
this._renderer.setProperty(this._elementRef.nativeElement, 'value', | ||
value ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : ''); | ||
if (!this._dateAdapter.sameDate(oldDate, value)) { | ||
this._valueChange.emit(value); | ||
} | ||
} | ||
private _value: D | null; | ||
|
||
/** The minimum valid date. */ | ||
@Input() | ||
|
@@ -285,6 +286,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces | |
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput); | ||
this._lastValueValid = !date || this._dateAdapter.isValid(date); | ||
date = this._getValidDateOrNull(date); | ||
this._value = date; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just go through the setter? A lot of this logic looks the same. I think you could change this method to: let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
this.value = date;
this._cvaOnChange(this.date);
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement)); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mmalerba
We would only want the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see right, that makes sense |
||
this._cvaOnChange(date); | ||
this._valueChange.emit(date); | ||
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a unit test for this fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if we're going to make this change we need to have tests that ensure the cached value returned by the getter never gets out of sync with the value of the element. We would need to test that
value
is correct after:[value]
bindingngModel
changeformControl
changedocument.query('input').value =
? (this change will probably break this case, but maybe that ok because people shouldn't be doing that)Also note that this will not fix the problem of the datepicker not working in non
MM/DD/YYYY
locales, it will still break as soon as the user inputs something because it has to go throughDateAdapter.parse
. The real fix for this is to integrate the new Angular i18n API that will be part of Angular 5.0 or have people use a different adapter in the mean time such as theMomentDateAdapter
that I'm currently working on. However if this change doesn't break any of the cases I listed, I would be ok with merging it as it would save us some parsing work every time the value is read.