Skip to content

Commit

Permalink
fix(material/datepicker): Only update selection when value changed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalmus committed Mar 30, 2021
1 parent 0c83adc commit 48a14b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/material/datepicker/date-range-input-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements
return modelValue.start;
}

protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean {
return super._shouldHandleChangeEvent(change) && change.oldValue?.start != change.selection.start;
}

protected _assignValueToModel(value: D | null) {
if (this._model) {
const range = new DateRange(value, this._model.selection.end);
Expand Down Expand Up @@ -365,6 +369,10 @@ export class MatEndDate<D> extends _MatDateRangeInputBase<D> implements
return modelValue.end;
}

protected _shouldHandleChangeEvent(change: DateSelectionModelChange<DateRange<D>>): boolean {
return super._shouldHandleChangeEvent(change) && change.oldValue?.end != change.selection.end;
}

protected _assignValueToModel(value: D | null) {
if (this._model) {
const range = new DateRange(this._model.selection.start, value);
Expand Down
6 changes: 5 additions & 1 deletion src/material/datepicker/date-selection-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export interface DateSelectionModelChange<S> {

/** Object that triggered the change. */
source: unknown;

/** Previous value */
oldValue?: S;
}

/**
Expand Down Expand Up @@ -69,8 +72,9 @@ export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<
* @param source Object that triggered the selection change.
*/
updateSelection(value: S, source: unknown) {
const oldValue = (this as {selection: S}).selection;
(this as {selection: S}).selection = value;
this._selectionChanged.next({selection: value, source});
this._selectionChanged.next({oldValue: oldValue, selection: value, source});
}

ngOnDestroy() {
Expand Down

0 comments on commit 48a14b4

Please sign in to comment.