Skip to content

Commit f459aac

Browse files
author
Ozan Turhan
committed
add usePanel prop for tabs component
- 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
1 parent cf8dd19 commit f459aac

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

src/components/Tabs.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const MODE_UNCONTROLLED = 1;
1313

1414
export default class Tabs extends Component {
1515
static defaultProps = {
16+
usePanel: true,
1617
defaultFocus: false,
1718
forceRenderTabPanel: false,
1819
selectedIndex: null,
@@ -23,6 +24,7 @@ export default class Tabs extends Component {
2324

2425
static propTypes = {
2526
children: childrenPropType,
27+
usePanel: PropTypes.bool,
2628
direction: PropTypes.oneOf(['rtl', 'ltr']),
2729
className: PropTypes.oneOfType([
2830
PropTypes.string,

src/components/UncontrolledTabs.js

+3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ function determineCanUseActiveElement(environment) {
4343
}
4444
export default class UncontrolledTabs extends Component {
4545
static defaultProps = {
46+
usePanel: true,
4647
className: 'react-tabs',
4748
focus: false,
4849
};
4950

5051
static propTypes = {
5152
children: childrenPropType,
53+
usePanel: PropTypes.bool,
5254
direction: PropTypes.oneOf(['rtl', 'ltr']),
5355
className: PropTypes.oneOfType([
5456
PropTypes.string,
@@ -359,6 +361,7 @@ export default class UncontrolledTabs extends Component {
359361
// Delete all known props, so they don't get added to DOM
360362
const {
361363
children, // unused
364+
usePanel,
362365
className,
363366
disabledTabClassName, // unused
364367
domRef,

src/components/__tests__/Tabs-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,17 @@ describe('<Tabs />', () => {
392392
</Tabs>,
393393
);
394394
});
395+
396+
it('should support using without panels', () => {
397+
expectToMatchSnapshot(
398+
<Tabs usePanel={false}>
399+
<TabList>
400+
<Tab>Foo</Tab>
401+
<Tab>Bar</Tab>
402+
</TabList>
403+
</Tabs>,
404+
);
405+
});
395406
});
396407

397408
test('should pass through custom properties', () => {

src/components/__tests__/__snapshots__/Tabs-test.js.snap

+34
Original file line numberDiff line numberDiff line change
@@ -975,3 +975,37 @@ exports[`<Tabs /> validation should result with warning when tab outside of tabl
975975
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'."`;
976976

977977
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'."`;
978+
979+
exports[`<Tabs /> validation should support using without panels 1`] = `
980+
<div
981+
class="react-tabs"
982+
data-tabs="true"
983+
>
984+
<ul
985+
class="react-tabs__tab-list"
986+
role="tablist"
987+
>
988+
<li
989+
aria-controls="react-tabs-1"
990+
aria-disabled="false"
991+
aria-selected="true"
992+
class="react-tabs__tab react-tabs__tab--selected"
993+
id="react-tabs-0"
994+
role="tab"
995+
tabindex="0"
996+
>
997+
Foo
998+
</li>
999+
<li
1000+
aria-controls="react-tabs-3"
1001+
aria-disabled="false"
1002+
aria-selected="false"
1003+
class="react-tabs__tab"
1004+
id="react-tabs-2"
1005+
role="tab"
1006+
>
1007+
Bar
1008+
</li>
1009+
</ul>
1010+
</div>
1011+
`;

src/helpers/propTypes.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export function childrenPropType(props, propName, componentName) {
88
let tabListFound = false;
99
const listTabs = [];
1010
const children = props[propName];
11-
1211
deepForEach(children, (child) => {
1312
if (isTabList(child)) {
1413
if (
@@ -41,7 +40,7 @@ export function childrenPropType(props, propName, componentName) {
4140
}
4241
});
4342

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

0 commit comments

Comments
 (0)