Skip to content

Commit 8256a47

Browse files
WesSouzaarturbien
authored andcommitted
docs(tabs): categorize under Controls
This also moves TabBody and Tab inside Tabs
1 parent 24630c8 commit 8256a47

File tree

8 files changed

+78
-172
lines changed

8 files changed

+78
-172
lines changed
File renamed without changes.

src/Tab/Tab.tsx renamed to src/Tabs/Tab.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,25 @@ const StyledTab = styled.button<TabProps>`
6969
}
7070
`;
7171

72-
const Tab = forwardRef<HTMLButtonElement, TabProps>(function Tab(
73-
{ value, onClick, selected = false, children, ...otherProps },
74-
ref
75-
) {
76-
return (
77-
<StyledTab
78-
aria-selected={selected}
79-
selected={selected}
80-
onClick={(e: React.MouseEvent<HTMLButtonElement>) => onClick?.(e, value)}
81-
ref={ref}
82-
role='tab'
83-
{...otherProps}
84-
>
85-
{children}
86-
</StyledTab>
87-
);
88-
});
72+
const Tab = forwardRef<HTMLButtonElement, TabProps>(
73+
({ value, onClick, selected = false, children, ...otherProps }, ref) => {
74+
return (
75+
<StyledTab
76+
aria-selected={selected}
77+
selected={selected}
78+
onClick={(e: React.MouseEvent<HTMLButtonElement>) =>
79+
onClick?.(e, value)
80+
}
81+
ref={ref}
82+
role='tab'
83+
{...otherProps}
84+
>
85+
{children}
86+
</StyledTab>
87+
);
88+
}
89+
);
90+
91+
Tab.displayName = 'Tab';
8992

9093
export { Tab, TabProps };
File renamed without changes.

src/TabBody/TabBody.tsx renamed to src/Tabs/TabBody.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ const StyledTabBody = styled.div`
1818
padding: 16px;
1919
font-size: 1rem;
2020
`;
21-
const TabBody = forwardRef<HTMLDivElement, TabBodyProps>(function TabBody(
22-
{ children, ...otherProps },
23-
ref
24-
) {
25-
return (
26-
<StyledTabBody ref={ref} {...otherProps}>
27-
{children}
28-
</StyledTabBody>
29-
);
30-
});
21+
const TabBody = forwardRef<HTMLDivElement, TabBodyProps>(
22+
({ children, ...otherProps }, ref) => {
23+
return (
24+
<StyledTabBody ref={ref} {...otherProps}>
25+
{children}
26+
</StyledTabBody>
27+
);
28+
}
29+
);
30+
31+
TabBody.displayName = 'TabBody';
3132

3233
export { TabBody, TabBodyProps };

src/Tabs/Tabs.mdx

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/Tabs/Tabs.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Wrapper = styled.div`
2020
`;
2121

2222
export default {
23-
title: 'Tabs',
23+
title: 'Controls/Tabs',
2424
component: Tabs,
2525
subcomponents: { Tab, TabBody },
2626
decorators: [story => <Wrapper>{story()}</Wrapper>]

src/Tabs/Tabs.tsx

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import React, { forwardRef, useMemo } from 'react';
22

33
import styled from 'styled-components';
44
import { noOp } from '../common/utils';
5-
import { TabProps } from '../Tab/Tab';
65
import { CommonStyledProps } from '../types';
6+
import { TabProps } from './Tab';
77

88
type TabsProps = {
99
value?: TabProps['value'];
@@ -56,45 +56,55 @@ function splitToChunks<T>(array: T[], parts: number) {
5656
return result;
5757
}
5858

59-
const Tabs = forwardRef<HTMLDivElement, TabsProps>(function Tabs(
60-
{ value, onChange = noOp, children, rows = 1, ...otherProps },
61-
ref
62-
) {
63-
// split tabs into equal rows and assign key to each row
64-
const tabRowsToRender = useMemo(() => {
65-
const childrenWithProps =
66-
React.Children.map(children, child => {
67-
if (!React.isValidElement(child)) {
68-
return null;
69-
}
70-
const tabProps = {
71-
selected: child.props.value === value,
72-
onClick: onChange
73-
};
74-
return React.cloneElement(child, tabProps);
75-
}) ?? [];
59+
const Tabs = forwardRef<HTMLDivElement, TabsProps>(
60+
({ value, onChange = noOp, children, rows = 1, ...otherProps }, ref) => {
61+
// split tabs into equal rows and assign key to each row
62+
const tabRowsToRender = useMemo(() => {
63+
const childrenWithProps =
64+
React.Children.map(children, child => {
65+
if (!React.isValidElement(child)) {
66+
return null;
67+
}
68+
const tabProps = {
69+
selected: child.props.value === value,
70+
onClick: onChange
71+
};
72+
return React.cloneElement(child, tabProps);
73+
}) ?? [];
7674

77-
const tabRows = splitToChunks(childrenWithProps, rows).map((tabs, i) => ({
78-
key: i,
79-
tabs
80-
}));
75+
const tabRows = splitToChunks(childrenWithProps, rows).map((tabs, i) => ({
76+
key: i,
77+
tabs
78+
}));
8179

82-
// move row containing currently selected tab to the bottom
83-
const currentlySelectedRowIndex = tabRows.findIndex(tabRow =>
84-
tabRow.tabs.some(tab => tab.props.selected)
80+
// move row containing currently selected tab to the bottom
81+
const currentlySelectedRowIndex = tabRows.findIndex(tabRow =>
82+
tabRow.tabs.some(tab => tab.props.selected)
83+
);
84+
tabRows.push(tabRows.splice(currentlySelectedRowIndex, 1)[0]);
85+
86+
return tabRows;
87+
}, [children, onChange, rows, value]);
88+
89+
return (
90+
<StyledTabs
91+
{...otherProps}
92+
isMultiRow={rows > 1}
93+
role='tablist'
94+
ref={ref}
95+
>
96+
{tabRowsToRender.map(row => (
97+
<Row key={row.key}>{row.tabs}</Row>
98+
))}
99+
</StyledTabs>
85100
);
86-
tabRows.push(tabRows.splice(currentlySelectedRowIndex, 1)[0]);
101+
}
102+
);
103+
104+
Tabs.displayName = 'Tabs';
87105

88-
return tabRows;
89-
}, [children, onChange, rows, value]);
106+
export * from './Tab';
90107

91-
return (
92-
<StyledTabs {...otherProps} isMultiRow={rows > 1} role='tablist' ref={ref}>
93-
{tabRowsToRender.map(row => (
94-
<Row key={row.key}>{row.tabs}</Row>
95-
))}
96-
</StyledTabs>
97-
);
98-
});
108+
export * from './TabBody';
99109

100110
export { Tabs, TabsProps };

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export * from './ScrollView/ScrollView';
2424
export * from './Select/Select';
2525
export * from './Separator/Separator';
2626
export * from './Slider/Slider';
27-
export * from './Tab/Tab';
28-
export * from './TabBody/TabBody';
2927
export * from './Table/Table';
3028
export * from './TableBody/TableBody';
3129
export * from './TableDataCell/TableDataCell';

0 commit comments

Comments
 (0)