Skip to content

Commit 3e0d868

Browse files
authored
fix: Table of contents - make tabs of horizontal mode configurable and scrollable (#1286)
* 💄 Make tabs of horizontal table of content scrollable * 🚧 Improve type hinting
1 parent 3392fb7 commit 3e0d868

5 files changed

Lines changed: 74 additions & 18 deletions

File tree

src/molecules/Tabs/Tabs.types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ export interface Tab<T> {
1212
disabled?: boolean;
1313
}
1414

15-
export interface Tabs<T> {
15+
export interface TabsScrollProps {
16+
scrollable?: boolean;
17+
amountPerScrollPage?: number;
18+
}
19+
20+
export interface Tabs<T> extends TabsScrollProps {
1621
selected: T;
1722
onChange: (value: T) => void;
1823
onHover?: (hoveredTabValue: T) => void;
1924
options: Tab<T>[];
20-
scrollable?: boolean;
21-
amountPerScrollPage?: number;
2225
centered?: boolean;
2326
animated?: boolean;
2427
}

src/organisms/TableOfContents/TableOfContents.stories.tsx

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import { tokens } from '@equinor/eds-tokens';
55
import { faker } from '@faker-js/faker';
66
import { Meta, StoryFn, StoryObj } from '@storybook/react-vite';
77

8-
import {
9-
TableOfContents,
10-
TableOfContentsProps,
11-
} from 'src/organisms/TableOfContents/TableOfContents';
8+
import { TableOfContents } from 'src/organisms/TableOfContents/TableOfContents';
129
import {
1310
TableOfContentsItemType,
1411
TableOfContentsProvider,
@@ -20,7 +17,12 @@ import styled from 'styled-components';
2017

2118
const { colors } = tokens;
2219

23-
const meta: Meta = {
20+
type StoryProps = TableOfContentsProviderProps & {
21+
mode?: 'vertical' | 'horizontal';
22+
onlyShowSelectedChildren?: boolean;
23+
};
24+
25+
const meta: Meta<StoryProps> = {
2426
title: 'Organisms/TableOfContents',
2527
component: TableOfContents,
2628
parameters: {
@@ -32,7 +34,6 @@ const meta: Meta = {
3234
argTypes: {
3335
items: { control: 'object' },
3436
onlyShowSelectedChildren: { control: 'boolean' },
35-
variant: { control: 'radio', items: ['border', 'buttons'] },
3637
},
3738
args: {
3839
items: [
@@ -48,9 +49,20 @@ const meta: Meta = {
4849
label: 'Third section',
4950
value: 'id-3',
5051
},
52+
{
53+
label: 'Fourth section',
54+
value: 'id-4',
55+
},
56+
{
57+
label: 'Fifth section',
58+
value: 'id-5',
59+
},
60+
{
61+
label: 'Sixth section',
62+
value: 'id-6',
63+
},
5164
],
5265
onlyShowSelectedChildren: false,
53-
variant: 'buttons',
5466
},
5567
};
5668

@@ -118,6 +130,9 @@ const COLORS = [
118130
colors.infographic.substitute__blue_ocean.rgba,
119131
colors.infographic.substitute__blue_sky.rgba,
120132
colors.infographic.substitute__blue_overcast.rgba,
133+
colors.infographic.substitute__green_cucumber.rgba,
134+
colors.infographic.substitute__green_succulent.rgba,
135+
colors.infographic.substitute__green_mint.rgba,
121136
];
122137

123138
function Section({
@@ -141,8 +156,6 @@ function Section({
141156
);
142157
}
143158

144-
type StoryProps = TableOfContentsProviderProps & TableOfContentsProps;
145-
146159
type Story = StoryObj<StoryProps>;
147160

148161
export const Default: Story = {
@@ -235,6 +248,33 @@ export const Horizontal: Story = {
235248
},
236249
};
237250

251+
export const HorizontalScrollable: Story = {
252+
render: (args) => {
253+
return (
254+
<div style={{ width: '40rem', maxWidth: '40rem' }}>
255+
<TableOfContentsProvider items={args.items}>
256+
<HorizontalContainer>
257+
<TableOfContents
258+
mode="horizontal"
259+
tabsProps={{ scrollable: true, amountPerScrollPage: 3 }}
260+
/>
261+
<section>
262+
{args.items.map((item, index) => (
263+
<Section
264+
key={item.value}
265+
label={item.label}
266+
value={item.value}
267+
color={COLORS[index]}
268+
/>
269+
))}
270+
</section>
271+
</HorizontalContainer>
272+
</TableOfContentsProvider>
273+
</div>
274+
);
275+
},
276+
};
277+
238278
const ITEMS_WITH_CHILDREN: TableOfContentsItemType[] = [
239279
{
240280
label: '2023',

src/organisms/TableOfContents/TableOfContents.styles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const Button = styled.button<ButtonProps>`
6565
`;
6666

6767
export const HorizontalItemsContainer = styled.div`
68+
overflow: hidden;
6869
button[role='tab'] {
6970
.count {
7071
background: ${colors.ui.background__light_medium.rgba};

src/organisms/TableOfContents/TableOfContents.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,32 @@ import {
55
TableOfContentsContainer,
66
VerticalItemsContainer,
77
} from './TableOfContents.styles';
8-
import { TableOfContentsMode } from './TableOfContents.types';
98
import { TableOfContentsItem } from './TableOfContentsItem';
109
import { Tabs } from 'src/molecules/Tabs/Tabs';
10+
import { TabsScrollProps } from 'src/molecules/Tabs/Tabs.types';
1111
import { useTableOfContents } from 'src/providers/TableOfContentsProvider';
1212
import { getValues } from 'src/providers/TableOfContentsProvider.utils';
1313

14-
export interface TableOfContentsProps {
15-
mode?: TableOfContentsMode;
14+
interface TableOfContentsBaseProps {
1615
onlyShowSelectedChildren?: boolean;
1716
}
1817

18+
interface TableOfContentsVerticalProps extends TableOfContentsBaseProps {
19+
mode?: 'vertical';
20+
}
21+
22+
interface TableOfContentsHorizontalProps extends TableOfContentsBaseProps {
23+
mode: 'horizontal';
24+
tabsProps?: TabsScrollProps;
25+
}
26+
27+
export type TableOfContentsProps =
28+
| TableOfContentsVerticalProps
29+
| TableOfContentsHorizontalProps;
30+
1931
export const TableOfContents: FC<TableOfContentsProps> = ({
20-
mode = 'vertical',
2132
onlyShowSelectedChildren = true,
33+
...props
2234
}) => {
2335
const { items, selected, setSelected } = useTableOfContents();
2436

@@ -40,7 +52,7 @@ export const TableOfContents: FC<TableOfContentsProps> = ({
4052
/* v8 ignore end */
4153
}, [items, selected]);
4254

43-
if (mode === 'horizontal') {
55+
if (props.mode === 'horizontal') {
4456
return (
4557
<HorizontalItemsContainer>
4658
<Tabs
@@ -49,6 +61,7 @@ export const TableOfContents: FC<TableOfContentsProps> = ({
4961
if (value) setSelected(value);
5062
}}
5163
options={items}
64+
{...props.tabsProps}
5265
/>
5366
</HorizontalItemsContainer>
5467
);

src/organisms/TableOfContents/TableOfContents.types.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)