Skip to content

Commit 48a14b4

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

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ 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) && change.oldValue?.start != change.selection.start;
266+
}
267+
264268
protected _assignValueToModel(value: D | null) {
265269
if (this._model) {
266270
const range = new DateRange(value, this._model.selection.end);
@@ -365,6 +369,10 @@ export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements
365369
return modelValue.end;
366370
}
367371

372+
protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean {
373+
return super._shouldHandleChangeEvent(change) && change.oldValue?.end != change.selection.end;
374+
}
375+
368376
protected _assignValueToModel(value: D | null) {
369377
if (this._model) {
370378
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({oldValue: oldValue, selection: value, source});
7478
}
7579

7680
ngOnDestroy() {

0 commit comments

Comments
 (0)