Skip to content
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

fix: select date on Today click if today is max date #7516

Merged
merged 1 commit into from
Jul 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ export const DatePickerOverlayContentMixin = (superClass) =>

/** @private */
_onTodayTap() {
const today = new Date();
const today = this._getTodayMidnight();

if (Math.abs(this._monthScroller.position - this._differenceInMonths(today, this._originDate)) < 0.001) {
// Select today only if the month scroller is positioned approximately
Expand Down Expand Up @@ -1040,12 +1040,17 @@ export const DatePickerOverlayContentMixin = (superClass) =>

/** @private */
_isTodayAllowed(min, max, isDateDisabled) {
return this._dateAllowed(this._getTodayMidnight(), min, max, isDateDisabled);
}

/** @private */
_getTodayMidnight() {
const today = new Date();
const todayMidnight = new Date(0, 0);
todayMidnight.setFullYear(today.getFullYear());
todayMidnight.setMonth(today.getMonth());
todayMidnight.setDate(today.getDate());
return this._dateAllowed(todayMidnight, min, max, isDateDisabled);
return todayMidnight;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/date-picker/test/overlay-content.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ describe('overlay', () => {
overlay.minDate = tomorrowMidnight;
expect(overlay._todayButton.disabled).to.be.true;
});

it('should select today if today is max', async () => {
overlay.maxDate = todayMidnight;
overlay.scrollToDate(todayMidnight);
await waitForScrollToFinish(overlay);

tap(overlay._todayButton);

expect(overlay.selectedDate.getFullYear()).to.equal(todayMidnight.getFullYear());
expect(overlay.selectedDate.getMonth()).to.equal(todayMidnight.getMonth());
expect(overlay.selectedDate.getDate()).to.equal(todayMidnight.getDate());
});
});
});
});
Expand Down
Loading