File tree 2 files changed +41
-10
lines changed
2 files changed +41
-10
lines changed Original file line number Diff line number Diff line change @@ -37,21 +37,12 @@ const propTypes = {
37
37
38
38
const Tab = ( props ) => {
39
39
let nodeRef = useRef ( ) ;
40
- const checkFocus = ( ) => {
41
- const { selected, focus } = props ;
42
- if ( selected && focus ) {
43
- nodeRef . current . focus ( ) ;
44
- }
45
- } ;
46
- useEffect ( ( ) => {
47
- checkFocus ( ) ;
48
- } ) ;
49
40
const {
50
41
children,
51
42
className,
52
43
disabled,
53
44
disabledClassName,
54
- focus, // unused
45
+ focus,
55
46
id,
56
47
panelId,
57
48
selected,
@@ -61,6 +52,12 @@ const Tab = (props) => {
61
52
...attributes
62
53
} = props ;
63
54
55
+ useEffect ( ( ) => {
56
+ if ( selected && focus ) {
57
+ nodeRef . current . focus ( ) ;
58
+ }
59
+ } , [ selected , focus ] ) ;
60
+
64
61
return (
65
62
< li
66
63
{ ...attributes }
Original file line number Diff line number Diff line change @@ -609,6 +609,40 @@ describe('<Tabs />', () => {
609
609
assertTabSelected ( 2 ) ;
610
610
} ) ;
611
611
612
+ test ( 'should not focus tab again on rerender' , ( ) => {
613
+ const { rerender } = render (
614
+ < >
615
+ < input data-testid = "input1" />
616
+ { createTabs ( ) }
617
+ </ > ,
618
+ ) ;
619
+ const firstTab = screen . getByTestId ( 'tab1' ) ;
620
+ const inputField = screen . getByTestId ( 'input1' ) ;
621
+
622
+ expect ( firstTab ) . not . toHaveFocus ( ) ;
623
+ expect ( inputField ) . not . toHaveFocus ( ) ;
624
+
625
+ userEvent . click ( firstTab ) ;
626
+
627
+ expect ( firstTab ) . toHaveFocus ( ) ;
628
+ expect ( inputField ) . not . toHaveFocus ( ) ;
629
+
630
+ userEvent . click ( inputField ) ;
631
+
632
+ expect ( firstTab ) . not . toHaveFocus ( ) ;
633
+ expect ( inputField ) . toHaveFocus ( ) ;
634
+
635
+ rerender (
636
+ < >
637
+ < input data-testid = "input1" />
638
+ { createTabs ( ) }
639
+ </ > ,
640
+ ) ;
641
+
642
+ expect ( firstTab ) . not . toHaveFocus ( ) ;
643
+ expect ( inputField ) . toHaveFocus ( ) ;
644
+ } ) ;
645
+
612
646
test ( 'should not change tabs when arrow up/down is pressed and disableUpDownKeys is passed' , ( ) => {
613
647
render (
614
648
createTabs ( {
You can’t perform that action at this time.
0 commit comments