Skip to content

fix(material/datepicker): change MatDateRangeInput input validation #19923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/material/datepicker/date-range-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,34 @@ describe('MatDateRangeInput', () => {
expect(end.errors?.matDatepickerMin).toBeFalsy();
});

it('should not revalidate if minDate is unchanged', () => {
const fixture = createComponent(StandardRangePicker);
fixture.componentInstance.minDate = new Date(2020, 3, 2);
fixture.detectChanges();
spyOn(fixture.componentInstance.rangeInput._startInput, '_validatorOnChange').and.callThrough();
fixture.componentInstance.minDate = new Date(2020, 3, 2);
fixture.detectChanges();
expect(fixture.componentInstance.rangeInput._startInput._validatorOnChange)
.not.toHaveBeenCalled();
fixture.componentInstance.minDate = new Date(2020, 3, 3);
fixture.detectChanges();
expect(fixture.componentInstance.rangeInput._startInput._validatorOnChange).toHaveBeenCalled();
});

it('should not revalidate if maxDate is unchanged', () => {
const fixture = createComponent(StandardRangePicker);
fixture.componentInstance.maxDate = new Date(2020, 3, 2);
fixture.detectChanges();
spyOn(fixture.componentInstance.rangeInput._endInput, '_validatorOnChange').and.callThrough();
fixture.componentInstance.maxDate = new Date(2020, 3, 2);
fixture.detectChanges();
expect(fixture.componentInstance.rangeInput._endInput._validatorOnChange)
.not.toHaveBeenCalled();
fixture.componentInstance.maxDate = new Date(2020, 3, 3);
fixture.detectChanges();
expect(fixture.componentInstance.rangeInput._endInput._validatorOnChange).toHaveBeenCalled();
});

it('should set the formatted date value as the input value', () => {
const fixture = createComponent(StandardRangePicker);
fixture.componentInstance.minDate = new Date(2020, 3, 2);
Expand Down
14 changes: 10 additions & 4 deletions src/material/datepicker/date-range-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,23 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
@Input()
get min(): D | null { return this._min; }
set min(value: D | null) {
this._min = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
this._revalidate();
const min = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
if (!this._dateAdapter.sameDate(min, this._min)) {
this._min = min;
this._revalidate();
}
}
private _min: D | null;

/** The maximum valid date. */
@Input()
get max(): D | null { return this._max; }
set max(value: D | null) {
this._max = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
this._revalidate();
const max = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
if (!this._dateAdapter.sameDate(max, this._max)) {
this._max = max;
this._revalidate();
}
}
private _max: D | null;

Expand Down