@@ -23,10 +23,10 @@ function Tabs(props: Props): JSX.Element {
23
23
const { lazy, block, defaultValue, values, groupId, className} = props ;
24
24
const { tabGroupChoices, setTabGroupChoices} = useUserPreferencesContext ( ) ;
25
25
const [ selectedValue , setSelectedValue ] = useState ( defaultValue ) ;
26
-
27
26
const children = Children . toArray ( props . children ) as ReactElement <
28
27
TabItemProps
29
28
> [ ] ;
29
+ const tabRefs : ( HTMLLIElement | null ) [ ] = [ ] ;
30
30
31
31
if ( groupId != null ) {
32
32
const relevantTabGroupChoice = tabGroupChoices [ groupId ] ;
@@ -39,46 +39,35 @@ function Tabs(props: Props): JSX.Element {
39
39
}
40
40
}
41
41
42
- const changeSelectedValue = ( newValue ) => {
43
- setSelectedValue ( newValue ) ;
44
- if ( groupId != null ) {
45
- setTabGroupChoices ( groupId , newValue ) ;
46
- }
47
- } ;
48
-
49
- const tabRefs : ( HTMLLIElement | null ) [ ] = [ ] ;
42
+ const handleTabChange = ( event ) => {
43
+ const selectedTab = event . target ;
44
+ const selectedTabIndex = tabRefs . indexOf ( selectedTab ) ;
45
+ const selectedTabValue = children [ selectedTabIndex ] . props . value ;
50
46
51
- const focusNextTab = ( tabs , target ) => {
52
- const next = tabs . indexOf ( target ) + 1 ;
47
+ setSelectedValue ( selectedTabValue ) ;
53
48
54
- if ( ! tabs [ next ] ) {
55
- tabs [ 0 ] . focus ( ) ;
56
- } else {
57
- tabs [ next ] . focus ( ) ;
49
+ if ( groupId != null ) {
50
+ setTabGroupChoices ( groupId , selectedTabValue ) ;
58
51
}
59
52
} ;
60
53
61
- const focusPreviousTab = ( tabs , target ) => {
62
- const prev = tabs . indexOf ( target ) - 1 ;
63
-
64
- if ( ! tabs [ prev ] ) {
65
- tabs [ tabs . length - 1 ] . focus ( ) ;
66
- } else {
67
- tabs [ prev ] . focus ( ) ;
68
- }
69
- } ;
54
+ const handleKeydown = ( event ) => {
55
+ let focusElement ;
70
56
71
- const handleKeydown = ( tabs , target , event ) => {
72
57
switch ( event . keyCode ) {
73
58
case keys . right :
74
- focusNextTab ( tabs , target ) ;
59
+ const nextTab = tabRefs . indexOf ( event . target ) + 1 ;
60
+ focusElement = tabRefs [ nextTab ] || tabRefs [ 0 ] ;
75
61
break ;
76
62
case keys . left :
77
- focusPreviousTab ( tabs , target ) ;
63
+ const prevTab = tabRefs . indexOf ( event . target ) - 1 ;
64
+ focusElement = tabRefs [ prevTab ] || tabRefs [ tabRefs . length - 1 ] ;
78
65
break ;
79
66
default :
80
67
break ;
81
68
}
69
+
70
+ focusElement ?. focus ( ) ;
82
71
} ;
83
72
84
73
return (
@@ -96,20 +85,16 @@ function Tabs(props: Props): JSX.Element {
96
85
{ values . map ( ( { value, label} ) => (
97
86
< li
98
87
role = "tab"
99
- tabIndex = { 0 }
88
+ tabIndex = { selectedValue === value ? 0 : - 1 }
100
89
aria-selected = { selectedValue === value }
101
90
className = { clsx ( 'tabs__item' , styles . tabItem , {
102
91
'tabs__item--active' : selectedValue === value ,
103
92
} ) }
104
93
key = { value }
105
94
ref = { ( tabControl ) => tabRefs . push ( tabControl ) }
106
- onKeyDown = { ( event ) => {
107
- handleKeydown ( tabRefs , event . target , event ) ;
108
- } }
109
- onFocus = { ( ) => changeSelectedValue ( value ) }
110
- onClick = { ( ) => {
111
- changeSelectedValue ( value ) ;
112
- } } >
95
+ onKeyDown = { handleKeydown }
96
+ onFocus = { handleTabChange }
97
+ onClick = { handleTabChange } >
113
98
{ label }
114
99
</ li >
115
100
) ) }
0 commit comments