Skip to content

Commit 5b81ac6

Browse files
committed
fix(material/datepicker): Only update selection when value changed (#21846)
1 parent 0c83adc commit 5b81ac6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/material/datepicker/date-range-input-parts.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements
261261
return modelValue.start;
262262
}
263263

264+
protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean {
265+
return super._shouldHandleChangeEvent(change) &&
266+
change.oldValue?.start != change.selection.start;
267+
}
268+
264269
protected _assignValueToModel(value: D | null) {
265270
if (this._model) {
266271
const range = new DateRange(value, this._model.selection.end);
@@ -365,6 +370,10 @@ export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements
365370
return modelValue.end;
366371
}
367372

373+
protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean {
374+
return super._shouldHandleChangeEvent(change) && change.oldValue?.end != change.selection.end;
375+
}
376+
368377
protected _assignValueToModel(value: D | null) {
369378
if (this._model) {
370379
const range = new DateRange(this._model.selection.start, value);

src/material/datepicker/date-selection-model.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export interface DateSelectionModelChange<S> {
4242

4343
/** Object that triggered the change. */
4444
source: unknown;
45+
46+
/** Previous value */
47+
oldValue?: S;
4548
}
4649

4750
/**
@@ -69,8 +72,9 @@ export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<
6972
* @param source Object that triggered the selection change.
7073
*/
7174
updateSelection(value: S, source: unknown) {
75+
const oldValue = (this as {selection: S}).selection;
7276
(this as {selection: S}).selection = value;
73-
this._selectionChanged.next({selection: value, source});
77+
this._selectionChanged.next({selection: value, source, oldValue});
7478
}
7579

7680
ngOnDestroy() {

0 commit comments

Comments
 (0)