Skip to content

Commit d52612e

Browse files
Merge pull request #3737 from AthiraKadampatta/3723-fix-today-click-behaviour
Fixes #3723: Calendar does not jump back to today's date when using inline and Today button
2 parents df2ac5c + c7443e4 commit d52612e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/calendar.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,14 +691,19 @@ export default class Calendar extends React.Component {
691691
);
692692
};
693693

694+
handleTodayButtonClick = (e) => {
695+
this.props.onSelect(getStartOfToday(), e);
696+
this.props.setPreSelection && this.props.setPreSelection(getStartOfToday());
697+
};
698+
694699
renderTodayButton = () => {
695700
if (!this.props.todayButton || this.props.showTimeSelectOnly) {
696701
return;
697702
}
698703
return (
699704
<div
700705
className="react-datepicker__today-button"
701-
onClick={(e) => this.props.onSelect(getStartOfToday(), e)}
706+
onClick={(e) => this.handleTodayButtonClick(e)}
702707
>
703708
{this.props.todayButton}
704709
</div>

test/datepicker_test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,32 @@ describe("DatePicker", () => {
319319
).to.equal(utils.formatDate(data.copyM, data.testFormat));
320320
});
321321

322+
it("should update the preSelection state when Today button is clicked after selecting a different day for inline mode", () => {
323+
var datePicker = TestUtils.renderIntoDocument(
324+
<DatePicker
325+
todayButton="Today"
326+
selected={utils.newDate()}
327+
inline
328+
onChange={(d) => {
329+
var date = d;
330+
}}
331+
/>
332+
);
333+
334+
var today = getSelectedDayNode(datePicker);
335+
var anyOtherDay = today.nextElementSibling || today.previousElementSibling;
336+
TestUtils.Simulate.click(anyOtherDay); // will update the preSelection to next or previous day
337+
338+
var todayBtn = datePicker.calendar.componentNode.querySelector(
339+
".react-datepicker__today-button"
340+
);
341+
TestUtils.Simulate.click(todayBtn); // will update the preSelection
342+
343+
expect(
344+
utils.formatDate(datePicker.state.preSelection, "yyyy-MM-dd")
345+
).to.equal(utils.formatDate(utils.newDate(), "yyyy-MM-dd"));
346+
});
347+
322348
it("should hide the calendar when pressing enter in the date input", () => {
323349
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
324350
var dateInput = datePicker.input;

0 commit comments

Comments
 (0)