File tree Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Original file line number Diff line number Diff line change @@ -37,21 +37,12 @@ const propTypes = {
3737
3838const Tab = ( props ) => {
3939 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- } ) ;
4940 const {
5041 children,
5142 className,
5243 disabled,
5344 disabledClassName,
54- focus, // unused
45+ focus,
5546 id,
5647 panelId,
5748 selected,
@@ -61,6 +52,12 @@ const Tab = (props) => {
6152 ...attributes
6253 } = props ;
6354
55+ useEffect ( ( ) => {
56+ if ( selected && focus ) {
57+ nodeRef . current . focus ( ) ;
58+ }
59+ } , [ selected , focus ] ) ;
60+
6461 return (
6562 < li
6663 { ...attributes }
Original file line number Diff line number Diff line change @@ -609,6 +609,40 @@ describe('<Tabs />', () => {
609609 assertTabSelected ( 2 ) ;
610610 } ) ;
611611
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+
612646 test ( 'should not change tabs when arrow up/down is pressed and disableUpDownKeys is passed' , ( ) => {
613647 render (
614648 createTabs ( {
You can’t perform that action at this time.
0 commit comments