Skip to content

Commit 8199775

Browse files
authored
fix(datepicker): change event dispatched before value is formatted (#19187)
Fixes the `dateChange` event being fired before the value is assigned to the native input value.
1 parent e73875b commit 8199775

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/material/datepicker/datepicker-input-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection
196196
const value = this._getValueFromModel(event.selection);
197197
this._cvaOnChange(value);
198198
this._onTouched();
199+
this._formatValue(value);
199200
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
200201
this.dateChange.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
201-
this._formatValue(value);
202202

203203
if (this._outsideValueChanged) {
204204
this._outsideValueChanged();

src/material/datepicker/datepicker.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,22 @@ describe('MatDatepicker', () => {
14751475
expect(testComponent.onDateInput).toHaveBeenCalledTimes(1);
14761476
});
14771477

1478+
it('should have updated the native input value when the dateChange event is emitted', () => {
1479+
let valueDuringChangeEvent = '';
1480+
1481+
(testComponent.onDateChange as jasmine.Spy).and.callFake(() => {
1482+
valueDuringChangeEvent = inputEl.value;
1483+
});
1484+
1485+
const model = fixture.debugElement.query(By.directive(MatDatepicker))
1486+
.injector.get<MatDateSelectionModel<Date>>(MatDateSelectionModel);
1487+
1488+
model.updateSelection(new Date(2020, 0, 1), null);
1489+
fixture.detectChanges();
1490+
1491+
expect(valueDuringChangeEvent).toBe('1/1/2020');
1492+
});
1493+
14781494
});
14791495

14801496
describe('with ISO 8601 strings as input', () => {

0 commit comments

Comments
 (0)