File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed
Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff 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 >
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments