@@ -8,17 +8,12 @@ import type { TextDirection } from '../../direction-context/DirectionContext';
88import {
99 COMPOSITE_KEYS ,
1010 ARROW_DOWN ,
11- ARROW_KEYS ,
1211 ARROW_LEFT ,
1312 ARROW_RIGHT ,
1413 ARROW_UP ,
1514 END ,
1615 HOME ,
17- HORIZONTAL_KEYS ,
18- HORIZONTAL_KEYS_WITH_EXTRA_KEYS ,
1916 MODIFIER_KEYS ,
20- VERTICAL_KEYS ,
21- VERTICAL_KEYS_WITH_EXTRA_KEYS ,
2217 findNonDisabledListIndex ,
2318 getMaxListIndex ,
2419 getMinListIndex ,
@@ -182,8 +177,8 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
182177 // Stable so that `relayKeyboardEvent` does not invalidate identity-sensitive
183178 // consumers (the `CompositeRootContext` value and trigger data forwarding).
184179 const onKeyDown = useStableCallback ( ( event : React . KeyboardEvent ) => {
185- const RELEVANT_KEYS = enableHomeAndEndKeys ? COMPOSITE_KEYS : ARROW_KEYS ;
186- if ( ! RELEVANT_KEYS . has ( event . key ) ) {
180+ const isHomeOrEnd = event . key === HOME || event . key === END ;
181+ if ( ! COMPOSITE_KEYS . has ( event . key ) || ( ! enableHomeAndEndKeys && isHomeOrEnd ) ) {
187182 return ;
188183 }
189184
@@ -199,17 +194,9 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
199194 const isRtl = direction === 'rtl' ;
200195
201196 const horizontalForwardKey = isRtl ? ARROW_LEFT : ARROW_RIGHT ;
202- const forwardKey = {
203- horizontal : horizontalForwardKey ,
204- vertical : ARROW_DOWN ,
205- both : horizontalForwardKey ,
206- } [ orientation ] ;
207197 const horizontalBackwardKey = isRtl ? ARROW_RIGHT : ARROW_LEFT ;
208- const backwardKey = {
209- horizontal : horizontalBackwardKey ,
210- vertical : ARROW_UP ,
211- both : horizontalBackwardKey ,
212- } [ orientation ] ;
198+ const forwardKey = orientation === 'vertical' ? ARROW_DOWN : horizontalForwardKey ;
199+ const backwardKey = orientation === 'vertical' ? ARROW_UP : horizontalBackwardKey ;
213200
214201 const target = getTarget ( event . nativeEvent ) ;
215202 if ( target != null && isNativeInput ( target ) && ! isElementDisabled ( target ) ) {
@@ -250,25 +237,12 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
250237 } ) ;
251238 }
252239
253- const forwardKeys = {
254- horizontal : [ horizontalForwardKey ] ,
255- vertical : [ ARROW_DOWN ] ,
256- both : [ horizontalForwardKey , ARROW_DOWN ] ,
257- } [ orientation ] ;
258-
259- const backwardKeys = {
260- horizontal : [ horizontalBackwardKey ] ,
261- vertical : [ ARROW_UP ] ,
262- both : [ horizontalBackwardKey , ARROW_UP ] ,
263- } [ orientation ] ;
264-
265- const preventedKeys = isGrid
266- ? RELEVANT_KEYS
267- : {
268- horizontal : enableHomeAndEndKeys ? HORIZONTAL_KEYS_WITH_EXTRA_KEYS : HORIZONTAL_KEYS ,
269- vertical : enableHomeAndEndKeys ? VERTICAL_KEYS_WITH_EXTRA_KEYS : VERTICAL_KEYS ,
270- both : RELEVANT_KEYS ,
271- } [ orientation ] ;
240+ const isForwardKey =
241+ ( orientation !== 'vertical' && event . key === horizontalForwardKey ) ||
242+ ( orientation !== 'horizontal' && event . key === ARROW_DOWN ) ;
243+ const isBackwardKey =
244+ ( orientation !== 'vertical' && event . key === horizontalBackwardKey ) ||
245+ ( orientation !== 'horizontal' && event . key === ARROW_UP ) ;
272246
273247 if ( enableHomeAndEndKeys ) {
274248 if ( event . key === HOME ) {
@@ -278,24 +252,21 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
278252 }
279253 }
280254
281- if (
282- nextIndex === highlightedIndex &&
283- ( forwardKeys . includes ( event . key ) || backwardKeys . includes ( event . key ) )
284- ) {
285- if ( loopFocus && nextIndex === maxIndex && forwardKeys . includes ( event . key ) ) {
255+ if ( nextIndex === highlightedIndex && ( isForwardKey || isBackwardKey ) ) {
256+ if ( loopFocus && nextIndex === maxIndex && isForwardKey ) {
286257 nextIndex = minIndex ;
287258 if ( onLoop ) {
288259 nextIndex = onLoop ( event , highlightedIndex , nextIndex , elementsRef ) ;
289260 }
290- } else if ( loopFocus && nextIndex === minIndex && backwardKeys . includes ( event . key ) ) {
261+ } else if ( loopFocus && nextIndex === minIndex && isBackwardKey ) {
291262 nextIndex = maxIndex ;
292263 if ( onLoop ) {
293264 nextIndex = onLoop ( event , highlightedIndex , nextIndex , elementsRef ) ;
294265 }
295266 } else {
296267 nextIndex = findNonDisabledListIndex ( elementsRef . current , {
297268 startingIndex : nextIndex ,
298- decrement : backwardKeys . includes ( event . key ) ,
269+ decrement : isBackwardKey ,
299270 disabledIndices,
300271 } ) ;
301272 }
@@ -306,7 +277,7 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
306277 event . stopPropagation ( ) ;
307278 }
308279
309- if ( preventedKeys . has ( event . key ) ) {
280+ if ( isGrid || isHomeOrEnd || isForwardKey || isBackwardKey ) {
310281 event . preventDefault ( ) ;
311282 }
312283 onHighlightedIndexChange ( nextIndex , true ) ;
0 commit comments