Skip to content

Commit

Permalink
add usePanel prop for tabs component
Browse files Browse the repository at this point in the history
- With this commit, tabs can be used without panels.
- If usePanel is given false, the error that occurs when there are no panels is not printed in console.
- usePanel default value is true.
- this commit does not cause any breaking change

reactjs#329
  • Loading branch information
Ozan Turhan committed May 7, 2021
1 parent cf8dd19 commit 322600a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MODE_UNCONTROLLED = 1;

export default class Tabs extends Component {
static defaultProps = {
usePanel: true,
defaultFocus: false,
forceRenderTabPanel: false,
selectedIndex: null,
Expand All @@ -23,6 +24,7 @@ export default class Tabs extends Component {

static propTypes = {
children: childrenPropType,
usePanel: PropTypes.bool,
direction: PropTypes.oneOf(['rtl', 'ltr']),
className: PropTypes.oneOfType([
PropTypes.string,
Expand Down
2 changes: 2 additions & 0 deletions src/components/UncontrolledTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class UncontrolledTabs extends Component {

static propTypes = {
children: childrenPropType,
usePanel: PropTypes.bool,
direction: PropTypes.oneOf(['rtl', 'ltr']),
className: PropTypes.oneOfType([
PropTypes.string,
Expand Down Expand Up @@ -359,6 +360,7 @@ export default class UncontrolledTabs extends Component {
// Delete all known props, so they don't get added to DOM
const {
children, // unused
usePanel,
className,
disabledTabClassName, // unused
domRef,
Expand Down
11 changes: 11 additions & 0 deletions src/components/__tests__/Tabs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ describe('<Tabs />', () => {
</Tabs>,
);
});

it('should support using without panels', () => {
expectToMatchSnapshot(
<Tabs usePanel={false}>
<TabList>
<Tab>Foo</Tab>
<Tab>Bar</Tab>
</TabList>
</Tabs>,
);
});
});

test('should pass through custom properties', () => {
Expand Down
34 changes: 34 additions & 0 deletions src/components/__tests__/__snapshots__/Tabs-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -975,3 +975,37 @@ exports[`<Tabs /> validation should result with warning when tab outside of tabl
exports[`<Tabs /> validation should result with warning when tabs/panels are imbalanced 1`] = `"Warning: Failed prop type: There should be an equal number of 'Tab' and 'TabPanel' in \`Tabs\`. Received 1 'Tab' and 0 'TabPanel'."`;

exports[`<Tabs /> validation should result with warning when tabs/panels are imbalanced and it should ignore non tab children 1`] = `"Warning: Failed prop type: There should be an equal number of 'Tab' and 'TabPanel' in \`Tabs\`. Received 1 'Tab' and 2 'TabPanel'."`;

exports[`<Tabs /> validation should support using without panels 1`] = `
<div
class="react-tabs"
data-tabs="true"
>
<ul
class="react-tabs__tab-list"
role="tablist"
>
<li
aria-controls="react-tabs-1"
aria-disabled="false"
aria-selected="true"
class="react-tabs__tab react-tabs__tab--selected"
id="react-tabs-0"
role="tab"
tabindex="0"
>
Foo
</li>
<li
aria-controls="react-tabs-3"
aria-disabled="false"
aria-selected="false"
class="react-tabs__tab"
id="react-tabs-2"
role="tab"
>
Bar
</li>
</ul>
</div>
`;
3 changes: 1 addition & 2 deletions src/helpers/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export function childrenPropType(props, propName, componentName) {
let tabListFound = false;
const listTabs = [];
const children = props[propName];

deepForEach(children, (child) => {
if (isTabList(child)) {
if (
Expand Down Expand Up @@ -41,7 +40,7 @@ export function childrenPropType(props, propName, componentName) {
}
});

if (!error && tabsCount !== panelsCount) {
if (!error && props.usePanel && tabsCount !== panelsCount) {
error = new Error(
`There should be an equal number of 'Tab' and 'TabPanel' in \`${componentName}\`. ` +
`Received ${tabsCount} 'Tab' and ${panelsCount} 'TabPanel'.`,
Expand Down

0 comments on commit 322600a

Please sign in to comment.