Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tab): tabpanel is tabbable when content has no tabbable content #7894

Merged
merged 12 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions packages/react/src/components/TabContent/TabContent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,54 @@

import React from 'react';
import TabContent from '../TabContent';
import { shallow } from 'enzyme';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

describe('TabContent', () => {
describe('renders as expected', () => {
const wrapper = shallow(
<TabContent>
<div className="child">content</div>
<div className="child">content</div>
</TabContent>
);

it('renders children as expected', () => {
expect(wrapper.props().children.length).toEqual(2);
render(
<TabContent selected>
<div className="child">content</div>
<div className="child">content</div>
</TabContent>
);
expect(screen.getByRole('tabpanel').children.length).toEqual(2);
});

it('sets selected if passed in via props', () => {
wrapper.setProps({ selected: true });
expect(wrapper.props().selected).toEqual(true);
it('sets selected and hidden props with opposite boolean values', () => {
const { rerender } = render(
<TabContent>
<div className="child">content</div>
<div className="child">content</div>
</TabContent>
);
expect(screen.queryByRole('tabpanel')).not.toBeInTheDocument();
rerender(
<TabContent selected>
<div className="child">content</div>
<div className="child">content</div>
</TabContent>
);
expect(screen.getByRole('tabpanel')).toBeVisible();
});

it('sets selected and hidden props with opposite boolean values', () => {
wrapper.setProps({ selected: true });
expect(wrapper.props().hidden).toEqual(false);
it('includes the content container in the tabbable index when no tabble contents is provided', () => {
render(
<TabContent selected>
<p>content</p>
</TabContent>
);
expect(screen.getByRole('tabpanel')).toHaveAttribute('tabindex', '0');
});

it('does not include the content container in the tabbable index when tabble contents is provided', () => {
render(
<TabContent selected>
<a href="https://www.ibm.com/">content</a>
</TabContent>
);
expect(screen.getByRole('tabpanel')).not.toHaveAttribute('tabindex', '0');
});
});
});
25 changes: 23 additions & 2 deletions packages/react/src/components/TabContent/TabContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,45 @@
*/

import PropTypes from 'prop-types';
import React from 'react';
import React, { useState, useRef } from 'react';
import classNames from 'classnames';
import { settings } from 'carbon-components';
import { selectorTabbable } from '../../internal/keyboard/navigation';
import useIsomorphicEffect from '../../internal/useIsomorphicEffect';

const { prefix } = settings;

/**
* Determine if the node within the provided ref contains content that is tabbable.
*/
function useTabbableContent(ref) {
const [hasTabbableContent, setHasTabbableContent] = useState(false);

useIsomorphicEffect(() => {
if (ref.current) {
setHasTabbableContent(ref.current.querySelector(selectorTabbable));
}
});

return hasTabbableContent;
}

const TabContent = (props) => {
const { className, selected, children, ...other } = props;
const tabContentClasses = classNames(`${prefix}--tab-content`, {
[className]: className,
});
const ref = useRef(null);
const hasTabbableContent = useTabbableContent(ref);
return (
<div
role="tabpanel"
{...other}
className={tabContentClasses}
selected={selected}
hidden={!selected}>
hidden={!selected}
ref={ref}
tabIndex={hasTabbableContent ? undefined : 0}>
{children}
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/components/Tabs/Tabs-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { settings } from 'carbon-components';
import classNames from 'classnames';
import './Tabs-story.scss';
import CodeSnippet from '../CodeSnippet';
import Button from '../Button';
import Tabs from '../Tabs';
import Tab from '../Tab';
import TabsSkeleton from '../Tabs/Tabs.Skeleton';
Expand Down Expand Up @@ -128,6 +129,7 @@ export const _Default = () => (
</Tab>
<Tab label="Tab label 2">
<p>Content for second tab goes here.</p>
<Button>With a button</Button>
</Tab>
<Tab label="Tab label 3" disabled>
<p>Content for third tab goes here.</p>
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/internal/useIsomorphicEffect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useEffect, useLayoutEffect } from 'react';

// useLayoutEffect on the client, useEffect on the server
const useIsomorphicEffect =
typeof window !== 'undefined' ? useLayoutEffect : useEffect;

export default useIsomorphicEffect;