File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -122,6 +122,46 @@ describe("when the grid is focused", () => {
122122 } ) ;
123123} ) ;
124124
125+ describe ( "when navigation is disabled" , ( ) => {
126+ beforeEach ( ( ) => {
127+ jest . useFakeTimers ( ) ;
128+ } ) ;
129+
130+ afterEach ( ( ) => {
131+ jest . useRealTimers ( ) ;
132+ } ) ;
133+
134+ test ( "keyboard navigation stays within the visible month" , async ( ) => {
135+ const defaultMonth = new Date ( 2025 , 6 , 1 ) ;
136+ const lastDay = new Date ( 2025 , 6 , 31 ) ;
137+ const previousDay = new Date ( 2025 , 6 , 30 ) ;
138+
139+ render (
140+ < DayPicker
141+ disableNavigation
142+ defaultMonth = { defaultMonth }
143+ selected = { lastDay }
144+ mode = "single"
145+ /> ,
146+ ) ;
147+
148+ await user . tab ( ) ;
149+ await user . tab ( ) ;
150+ await user . tab ( ) ;
151+
152+ const lastDayButton = dateButton ( lastDay ) ;
153+ const previousDayButton = dateButton ( previousDay ) ;
154+
155+ expect ( activeElement ( ) ) . toBe ( lastDayButton ) ;
156+
157+ await user . keyboard ( "{ArrowRight}" ) ;
158+ expect ( activeElement ( ) ) . toBe ( lastDayButton ) ;
159+
160+ await user . keyboard ( "{ArrowLeft}" ) ;
161+ expect ( activeElement ( ) ) . toBe ( previousDayButton ) ;
162+ } ) ;
163+ } ) ;
164+
125165describe ( "when a day is mouse entered" , ( ) => {
126166 const handleDayMouseEnter = jest . fn ( ) ;
127167 const handleDayMouseLeave = jest . fn ( ) ;
Original file line number Diff line number Diff line change @@ -80,6 +80,15 @@ export function useFocus<T extends DayPickerProps>(
8080 ) ;
8181 if ( ! nextFocus ) return ;
8282
83+ if ( props . disableNavigation ) {
84+ const isNextInCalendar = calendar . days . some ( ( day ) =>
85+ day . isEqualTo ( nextFocus ) ,
86+ ) ;
87+ if ( ! isNextInCalendar ) {
88+ return ;
89+ }
90+ }
91+
8392 calendar . goToDay ( nextFocus ) ;
8493 setFocused ( nextFocus ) ;
8594 } ;
You can’t perform that action at this time.
0 commit comments