Skip to content

Commit

Permalink
Merge pull request Hacker0x01#3737 from AthiraKadampatta/3723-fix-tod…
Browse files Browse the repository at this point in the history
…ay-click-behaviour

Fixes Hacker0x01#3723: Calendar does not jump back to today's date when using inline and Today button
  • Loading branch information
martijnrusschen committed Sep 22, 2022
2 parents 9a981eb + 6f1a556 commit 9d0ccd2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/calendar.jsx
Expand Up @@ -691,14 +691,19 @@ export default class Calendar extends React.Component {
);
};

handleTodayButtonClick = (e) => {
this.props.onSelect(getStartOfToday(), e);
this.props.setPreSelection && this.props.setPreSelection(getStartOfToday());
};

renderTodayButton = () => {
if (!this.props.todayButton || this.props.showTimeSelectOnly) {
return;
}
return (
<div
className="react-datepicker__today-button"
onClick={(e) => this.props.onSelect(getStartOfToday(), e)}
onClick={(e) => this.handleTodayButtonClick(e)}
>
{this.props.todayButton}
</div>
Expand Down
26 changes: 26 additions & 0 deletions test/datepicker_test.js
Expand Up @@ -319,6 +319,32 @@ describe("DatePicker", () => {
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});

it("should update the preSelection state when Today button is clicked after selecting a different day for inline mode", () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker
todayButton="Today"
selected={utils.newDate()}
inline
onChange={(d) => {
var date = d;
}}
/>
);

var today = getSelectedDayNode(datePicker);
var anyOtherDay = today.nextElementSibling || today.previousElementSibling;
TestUtils.Simulate.click(anyOtherDay); // will update the preSelection to next or previous day

var todayBtn = datePicker.calendar.componentNode.querySelector(
".react-datepicker__today-button"
);
TestUtils.Simulate.click(todayBtn); // will update the preSelection

expect(
utils.formatDate(datePicker.state.preSelection, "yyyy-MM-dd")
).to.equal(utils.formatDate(utils.newDate(), "yyyy-MM-dd"));
});

it("should hide the calendar when pressing enter in the date input", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
Expand Down

0 comments on commit 9d0ccd2

Please sign in to comment.