@@ -6,7 +6,7 @@ import { RovingFocusGroupContext } from '../context/RovingFocuGroupContext';
66
77const RovingFocusItem = forwardRef < HTMLButtonElement , { children : React . ReactNode } > ( ( { children, ...props } , ref ) => {
88 const id = useId ( ) ;
9- const { focusedItemId, setFocusedItemId, addFocusItem, focusItems, groupRef } = useContext ( RovingFocusGroupContext ) ;
9+ const { focusedItemId, setFocusedItemId, addFocusItem, focusItems, groupRef, direction , loop } = useContext ( RovingFocusGroupContext ) ;
1010
1111 useEffect ( ( ) => {
1212 // we check if the item is in the focusItems array, if not we add it
@@ -31,18 +31,46 @@ const RovingFocusItem = forwardRef<HTMLButtonElement, { children: React.ReactNod
3131
3232 const handleKeyDown = ( event : React . KeyboardEvent < HTMLButtonElement > ) => {
3333 switch ( event . key ) {
34- case 'ArrowUp' :
35- case 'ArrowLeft' :
36- // Logic to move focus to the previous item
37- setFocusedItemId ( focusItems [ focusItems . indexOf ( id ) - 1 ] ) ;
38-
34+ case 'ArrowUp' : {
35+ if ( direction === 'vertical' ) {
36+ // Logic to move focus to the previous item
37+ const previousIndex = focusItems . indexOf ( id ) - 1 ;
38+ if ( previousIndex >= 0 ) {
39+ setFocusedItemId ( focusItems [ previousIndex ] ) ;
40+ }
41+ }
3942 break ;
40- case 'ArrowDown' :
41- case 'ArrowRight' :
42- // Logic to move focus to the next item
43- setFocusedItemId ( focusItems [ focusItems . indexOf ( id ) + 1 ] ) ;
44-
43+ }
44+ case 'ArrowLeft' : {
45+ if ( direction === 'horizontal' ) {
46+ // Logic to move focus to the previous item
47+ const previousIndex = focusItems . indexOf ( id ) - 1 ;
48+ if ( previousIndex >= 0 ) {
49+ setFocusedItemId ( focusItems [ previousIndex ] ) ;
50+ }
51+ }
52+ break ;
53+ }
54+ case 'ArrowDown' : {
55+ if ( direction === 'vertical' ) {
56+ // Logic to move focus to the next item
57+ const nextIndex = focusItems . indexOf ( id ) + 1 ;
58+ if ( nextIndex < focusItems . length ) {
59+ setFocusedItemId ( focusItems [ nextIndex ] ) ;
60+ }
61+ }
62+ break ;
63+ }
64+ case 'ArrowRight' : {
65+ if ( direction === 'horizontal' ) {
66+ // Check if it's not the last item before moving focus to the next item
67+ const nextIndex = focusItems . indexOf ( id ) + 1 ;
68+ if ( nextIndex < focusItems . length ) {
69+ setFocusedItemId ( focusItems [ nextIndex ] ) ;
70+ }
71+ }
4572 break ;
73+ }
4674 case 'Tab' :
4775 // Logic to handle tab key if needed
4876 console . log ( 'Tab' ) ;
0 commit comments