Skip to content

Commit 1e550b8

Browse files
committed
fix(material/datepicker): Only update selection when value changed (#21846)
1 parent ff69e19 commit 1e550b8

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

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

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

264+
protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean {
265+
if (!super._shouldHandleChangeEvent(change)) {
266+
return false;
267+
}
268+
else {
269+
return !change.oldValue?.start ?
270+
!!change.selection.start :
271+
!change.selection.start || !this._dateAdapter.compareDate(change.oldValue.start, change.selection.start);
272+
}
273+
}
274+
264275
protected _assignValueToModel(value: D | null) {
265276
if (this._model) {
266277
const range = new DateRange(value, this._model.selection.end);
@@ -365,6 +376,17 @@ export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements
365376
return modelValue.end;
366377
}
367378

379+
protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean {
380+
if (!super._shouldHandleChangeEvent(change)) {
381+
return false;
382+
}
383+
else {
384+
return !change.oldValue?.end ?
385+
!!change.selection.end :
386+
!change.selection.end || !this._dateAdapter.compareDate(change.oldValue.end, change.selection.end);
387+
}
388+
}
389+
368390
protected _assignValueToModel(value: D | null) {
369391
if (this._model) {
370392
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() {

tools/public_api_guard/material/datepicker.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export declare class DateRange<D> {
1313
}
1414

1515
export interface DateSelectionModelChange<S> {
16+
oldValue?: S;
1617
selection: S;
1718
source: unknown;
1819
}
@@ -405,6 +406,7 @@ export declare class MatEndDate<D> extends _MatDateRangeInputBase<D> implements
405406
protected _assignValueToModel(value: D | null): void;
406407
protected _getValueFromModel(modelValue: DateRange<D>): D | null;
407408
_onKeydown(event: KeyboardEvent): void;
409+
protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean;
408410
ngDoCheck(): void;
409411
ngOnInit(): void;
410412
static ngAcceptInputType_disabled: BooleanInput;
@@ -516,6 +518,7 @@ export declare class MatStartDate<D> extends _MatDateRangeInputBase<D> implement
516518
protected _assignValueToModel(value: D | null): void;
517519
protected _formatValue(value: D | null): void;
518520
protected _getValueFromModel(modelValue: DateRange<D>): D | null;
521+
protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean;
519522
getMirrorValue(): string;
520523
ngDoCheck(): void;
521524
ngOnInit(): void;

0 commit comments

Comments
 (0)