@@ -3884,7 +3884,7 @@ describe("DatePicker", () => {
38843884 describe ( "shouldFocusDayInline state" , ( ) => {
38853885 const dateFormat = "yyyy-MM-dd" ;
38863886
3887- it ( "should not be updated when navigating with ArrowRight key without changing displayed month" , ( ) => {
3887+ it ( "should be set to true when navigating with ArrowRight key without changing displayed month" , ( ) => {
38883888 let instance : DatePicker | null = null ;
38893889 const { container } = render (
38903890 < DatePicker
@@ -3900,7 +3900,8 @@ describe("DatePicker", () => {
39003900 expect ( selectedDayNode ) . toBeTruthy ( ) ;
39013901 fireEvent . keyDown ( selectedDayNode ! , getKey ( KeyType . ArrowRight ) ) ;
39023902 expect ( instance ) . toBeTruthy ( ) ;
3903- expect ( instance ! . state . shouldFocusDayInline ) . toBe ( false ) ;
3903+ // Always set to true for keyboard navigation to ensure focus transfers correctly
3904+ expect ( instance ! . state . shouldFocusDayInline ) . toBe ( true ) ;
39043905 } ) ;
39053906
39063907 it ( "should be set to true when changing displayed month with ArrowRight key" , ( ) => {
@@ -3940,6 +3941,54 @@ describe("DatePicker", () => {
39403941 expect ( instance ) . toBeTruthy ( ) ;
39413942 expect ( instance ! . state . shouldFocusDayInline ) . toBe ( true ) ;
39423943 } ) ;
3944+
3945+ it ( "should maintain keyboard focus when navigating within same month in inline selectsRange mode with showPreviousMonths" , ( ) => {
3946+ // This test verifies the fix for GitHub issue #5750
3947+ // Focus was being lost when navigating within the same month in inline mode
3948+ // with selectsRange and showPreviousMonths enabled
3949+ const startDate = newDate ( "2025-06-01" ) ;
3950+ const endDate = newDate ( "2025-07-01" ) ;
3951+ const div = document . createElement ( "div" ) ;
3952+ document . body . appendChild ( div ) ;
3953+
3954+ const { container } = render (
3955+ < DatePicker
3956+ selectsRange
3957+ inline
3958+ showPreviousMonths
3959+ monthsShown = { 2 }
3960+ startDate = { startDate }
3961+ endDate = { endDate }
3962+ openToDate = { endDate }
3963+ /> ,
3964+ { container : div } ,
3965+ ) ;
3966+
3967+ // Find the start date (June 1) and focus it
3968+ const startDateNode = container . querySelector (
3969+ '.react-datepicker__day--range-start[tabindex="0"]' ,
3970+ ) ;
3971+ expect ( startDateNode ) . toBeTruthy ( ) ;
3972+ act ( ( ) => {
3973+ ( startDateNode as HTMLElement ) ?. focus ( ) ;
3974+ } ) ;
3975+ expect ( document . activeElement ) . toBe ( startDateNode ) ;
3976+
3977+ // Navigate right (to June 2, same month)
3978+ fireEvent . keyDown ( startDateNode ! , getKey ( KeyType . ArrowRight ) ) ;
3979+
3980+ // After navigation, focus should be on June 2, not lost to body
3981+ const newFocusedDay = container . querySelector (
3982+ '.react-datepicker__day[tabindex="0"]' ,
3983+ ) ;
3984+ expect ( newFocusedDay ) . toBeTruthy ( ) ;
3985+ expect ( document . activeElement ) . not . toBe ( document . body ) ;
3986+ expect (
3987+ document . activeElement ?. classList . contains ( "react-datepicker__day" ) ,
3988+ ) . toBe ( true ) ;
3989+
3990+ document . body . removeChild ( div ) ;
3991+ } ) ;
39433992 } ) ;
39443993
39453994 describe ( "Calendar Header Accessibility" , ( ) => {
0 commit comments