@@ -2897,6 +2897,59 @@ describe("DatePicker", () => {
28972897 expect ( instance ! . state . monthSelectedIn ) . toEqual ( undefined ) ;
28982898 } ) ;
28992899
2900+ it ( "should reset monthSelectedIn to 0 when changeMonth is called from custom header" , ( ) => {
2901+ let instance : DatePicker | null = null ;
2902+ let changeMonthFn : ( ( month : number ) => void ) | null = null ;
2903+
2904+ const { container } = render (
2905+ < DatePicker
2906+ ref = { ( node ) => {
2907+ instance = node ;
2908+ } }
2909+ inline
2910+ monthsShown = { 2 }
2911+ selected = { newDate ( "2024-06-15" ) }
2912+ renderCustomHeader = { ( { changeMonth } ) => {
2913+ changeMonthFn = changeMonth ;
2914+ return (
2915+ < div >
2916+ < button
2917+ data-testid = "change-month-btn"
2918+ onClick = { ( ) => changeMonth ( 0 ) }
2919+ >
2920+ Go to January
2921+ </ button >
2922+ </ div >
2923+ ) ;
2924+ } }
2925+ /> ,
2926+ ) ;
2927+
2928+ expect ( instance ) . toBeTruthy ( ) ;
2929+
2930+ // First, select a day in the second month panel to set monthSelectedIn to 1
2931+ const dayButtonsInSecondMonth = container
2932+ . querySelectorAll ( ".react-datepicker__month-container" ) [ 1 ]
2933+ ?. querySelectorAll (
2934+ ".react-datepicker__day:not(.react-datepicker__day--outside-month)" ,
2935+ ) ;
2936+ expect ( dayButtonsInSecondMonth ) . toBeTruthy ( ) ;
2937+ expect ( dayButtonsInSecondMonth ! . length ) . toBeGreaterThan ( 0 ) ;
2938+
2939+ // Click a day in the second month to set monthSelectedIn to 1
2940+ fireEvent . click ( dayButtonsInSecondMonth ! [ 10 ] ! ) ;
2941+ expect ( instance ! . state . monthSelectedIn ) . toEqual ( 1 ) ;
2942+
2943+ // Now call changeMonth from the custom header
2944+ expect ( changeMonthFn ) . toBeTruthy ( ) ;
2945+ act ( ( ) => {
2946+ changeMonthFn ! ( 0 ) ; // Change to January
2947+ } ) ;
2948+
2949+ // monthSelectedIn should be reset to 0
2950+ expect ( instance ! . state . monthSelectedIn ) . toEqual ( 0 ) ;
2951+ } ) ;
2952+
29002953 it ( "should show the popper arrow when showPopperArrow is true" , ( ) => {
29012954 const { container } = render ( < DatePicker showPopperArrow /> ) ;
29022955 const input = safeQuerySelector ( container , "input" ) ;
0 commit comments