Skip to content

Commit

Permalink
fix: select date on Today click if today is max date (#7516) (#7517)
Browse files Browse the repository at this point in the history
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
vaadin-bot and web-padawan committed Jul 8, 2024
1 parent 9127df7 commit 0b84429
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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

0 comments on commit 0b84429

Please sign in to comment.