Skip to content

Commit

Permalink
[v17] Partial rewrite of the slide tabs component (#49458)
Browse files Browse the repository at this point in the history
* Partial rewrite of the slide tabs component

This enables us to put icons there, make the layout tighter and fit to tab list
contents. It also improves accessibility by adding keyboard handling and
changing the structure according to ARIA Authoring Practices Guide.

* Make the ID attribute mandatory

* review

* wip
  • Loading branch information
bl-nero authored Dec 6, 2024
1 parent 7a38323 commit c5b46a3
Show file tree
Hide file tree
Showing 3 changed files with 403 additions and 117 deletions.
121 changes: 102 additions & 19 deletions web/packages/design/src/SlideTabs/SlideTabs.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,40 @@

import React, { useState } from 'react';

import * as Icon from 'design/Icon';
import Flex from 'design/Flex';

import { SlideTabs } from './SlideTabs';

export default {
title: 'Design/SlideTabs',
};

const threeSimpleTabs = [
{ key: 'aws', title: 'aws' },
{ key: 'automatically', title: 'automatically' },
{ key: 'manually', title: 'manually' },
];

const fiveSimpleTabs = [
{ key: 'step1', title: 'step1' },
{ key: 'step2', title: 'step2' },
{ key: 'step3', title: 'step3' },
{ key: 'step4', title: 'step4' },
{ key: 'step5', title: 'step5' },
];

const titlesWithIcons = [
{ key: 'alarm', icon: Icon.AlarmRing, title: 'Clocks' },
{ key: 'bots', icon: Icon.Bots, title: 'Bots' },
{ key: 'check', icon: Icon.Check, title: 'Checkmarks' },
];

export const ThreeTabs = () => {
const [activeIndex, setActiveIndex] = useState(0);
return (
<SlideTabs
tabs={['aws', 'automatically', 'manually']}
tabs={threeSimpleTabs}
onChange={setActiveIndex}
activeIndex={activeIndex}
/>
Expand All @@ -39,42 +62,102 @@ export const FiveTabs = () => {
const [activeIndex, setActiveIndex] = useState(0);
return (
<SlideTabs
tabs={['step1', 'step2', 'step3', 'step4', 'step5']}
tabs={fiveSimpleTabs}
onChange={setActiveIndex}
activeIndex={activeIndex}
/>
);
};

export const Round = () => {
const [activeIndex, setActiveIndex] = useState(0);
const [activeIndex1, setActiveIndex1] = useState(0);
const [activeIndex2, setActiveIndex2] = useState(0);
return (
<SlideTabs
appearance="round"
tabs={['step1', 'step2', 'step3', 'step4', 'step5']}
onChange={setActiveIndex}
activeIndex={activeIndex}
/>
<Flex flexDirection="column" gap={3}>
<SlideTabs
appearance="round"
tabs={fiveSimpleTabs}
onChange={setActiveIndex1}
activeIndex={activeIndex1}
/>
<SlideTabs
tabs={titlesWithIcons}
appearance="round"
onChange={setActiveIndex2}
activeIndex={activeIndex2}
/>
</Flex>
);
};

export const Medium = () => {
const [activeIndex, setActiveIndex] = useState(0);
const [activeIndex1, setActiveIndex1] = useState(0);
const [activeIndex2, setActiveIndex2] = useState(0);
return (
<SlideTabs
tabs={['step1', 'step2', 'step3', 'step4', 'step5']}
size="medium"
appearance="round"
onChange={setActiveIndex}
activeIndex={activeIndex}
/>
<Flex flexDirection="column" gap={3}>
<SlideTabs
tabs={fiveSimpleTabs}
size="medium"
appearance="round"
onChange={setActiveIndex1}
activeIndex={activeIndex1}
/>
<SlideTabs
tabs={titlesWithIcons}
size="medium"
appearance="round"
onChange={setActiveIndex2}
activeIndex={activeIndex2}
/>
</Flex>
);
};

export const Small = () => {
const [activeIndex1, setActiveIndex1] = useState(0);
const [activeIndex2, setActiveIndex2] = useState(0);
return (
<Flex flexDirection="column" gap={3}>
<SlideTabs
tabs={[
{ key: 'alarm', icon: Icon.AlarmRing, ariaLabel: 'alarm' },
{ key: 'bots', icon: Icon.Bots, ariaLabel: 'bots' },
{ key: 'check', icon: Icon.Check, ariaLabel: 'check' },
]}
size="small"
appearance="round"
onChange={setActiveIndex1}
activeIndex={activeIndex1}
fitContent
/>
<SlideTabs
tabs={[
{ key: 'kraken', title: 'Kraken' },
{ key: 'chubacabra', title: 'Chubacabra' },
{ key: 'yeti', title: 'Yeti' },
]}
size="small"
appearance="round"
onChange={setActiveIndex2}
activeIndex={activeIndex2}
fitContent
/>
<SlideTabs
tabs={titlesWithIcons}
size="small"
appearance="round"
onChange={setActiveIndex1}
activeIndex={activeIndex1}
fitContent
/>
</Flex>
);
};

export const LoadingTab = () => {
return (
<SlideTabs
tabs={['aws', 'automatically', 'manually']}
tabs={threeSimpleTabs}
onChange={() => null}
activeIndex={1}
isProcessing={true}
Expand All @@ -85,7 +168,7 @@ export const LoadingTab = () => {
export const DisabledTab = () => {
return (
<SlideTabs
tabs={['aws', 'automatically', 'manually']}
tabs={threeSimpleTabs}
onChange={() => null}
activeIndex={1}
disabled={true}
Expand Down
99 changes: 69 additions & 30 deletions web/packages/design/src/SlideTabs/SlideTabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,120 @@ import { screen } from '@testing-library/react';

import { render, userEvent } from 'design/utils/testing';

import { SlideTabs } from './SlideTabs';
import { SlideTabs, SlideTabsProps } from './SlideTabs';

describe('design/SlideTabs', () => {
it('renders the supplied number of tabs(3)', () => {
render(
<SlideTabs
tabs={['aws', 'automatically', 'manually']}
tabs={[
{ key: 'aws', title: 'aws' },
{ key: 'automatically', title: 'automatically' },
{ key: 'manually', title: 'manually' },
]}
onChange={() => {}}
activeIndex={0}
/>
);

expect(screen.getAllByRole('tab')).toHaveLength(3);

expect(screen.getByLabelText('aws')).toBeInTheDocument();
expect(screen.getByLabelText('automatically')).toBeInTheDocument();
expect(screen.getByLabelText('manually')).toBeInTheDocument();
expect(getTab('aws')).toBeInTheDocument();
expect(getTab('automatically')).toBeInTheDocument();
expect(getTab('manually')).toBeInTheDocument();
});

it('renders the supplied number of tabs(5)', () => {
render(
<SlideTabs
tabs={['aws', 'automatically', 'manually', 'apple', 'purple']}
tabs={[
{ key: 'aws', title: 'aws' },
{ key: 'automatically', title: 'automatically' },
{ key: 'manually', title: 'manually' },
{ key: 'apple', title: 'apple' },
{ key: 'purple', title: 'purple' },
]}
onChange={() => {}}
activeIndex={0}
/>
);

expect(screen.getAllByRole('tab')).toHaveLength(5);

expect(screen.getByLabelText('aws')).toBeInTheDocument();
expect(screen.getByLabelText('automatically')).toBeInTheDocument();
expect(screen.getByLabelText('manually')).toBeInTheDocument();
expect(screen.getByLabelText('apple')).toBeInTheDocument();
expect(screen.getByLabelText('purple')).toBeInTheDocument();
});

it('respects a custom form name', () => {
const { container } = render(
<SlideTabs
name="pineapple"
tabs={['aws', 'automatically', 'manually']}
onChange={() => {}}
activeIndex={0}
/>
);

// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelectorAll('input[name=pineapple]')).toHaveLength(3);
expect(getTab('aws')).toBeInTheDocument();
expect(getTab('automatically')).toBeInTheDocument();
expect(getTab('manually')).toBeInTheDocument();
expect(getTab('apple')).toBeInTheDocument();
expect(getTab('purple')).toBeInTheDocument();
});

test('onChange highlights the tab clicked', async () => {
render(<Component />);

// First tab is selected by default.
expect(screen.getByRole('tab', { name: 'first' })).toHaveClass('selected');
expect(getTab('first')).toHaveClass('selected');

// Select the second tab.
await userEvent.click(screen.getByText('second'));
expect(screen.getByRole('tab', { name: 'second' })).toHaveClass('selected');
expect(getTab('second')).toHaveClass('selected');

expect(getTab('first')).not.toHaveClass('selected');
});

expect(screen.getByRole('tab', { name: 'first' })).not.toHaveClass(
'selected'
test('keyboard navigation and accessibility', async () => {
const user = userEvent.setup();
render(
<Component
tabs={[
{ key: 'id1', title: 'first', controls: 'tabpanel-1' },
{ key: 'id2', title: 'second', controls: 'tabpanel-2' },
]}
/>
);
expect(getTab('first')).not.toHaveFocus();
expect(getTab('second')).not.toHaveFocus();

getTab('first').focus();
expect(getTab('first')).toHaveAttribute('aria-selected', 'true');
expect(getTab('first')).toHaveAttribute('aria-controls', 'tabpanel-1');
expect(getTab('second')).toHaveAttribute('aria-selected', 'false');
expect(getTab('second')).toHaveAttribute('aria-controls', 'tabpanel-2');

await user.keyboard('{Right}');
expect(getTab('first')).toHaveAttribute('aria-selected', 'false');
expect(getTab('second')).toHaveAttribute('aria-selected', 'true');
expect(getTab('second')).toHaveFocus();

// Should be a no-op.
await user.keyboard('{Right}');
expect(getTab('first')).toHaveAttribute('aria-selected', 'false');
expect(getTab('second')).toHaveAttribute('aria-selected', 'true');
expect(getTab('second')).toHaveFocus();

await user.keyboard('{Left}');
expect(getTab('first')).toHaveAttribute('aria-selected', 'true');
expect(getTab('second')).toHaveAttribute('aria-selected', 'false');
expect(getTab('first')).toHaveFocus();

// Should be a no-op.
await user.keyboard('{Left}');
expect(getTab('first')).toHaveAttribute('aria-selected', 'true');
expect(getTab('second')).toHaveAttribute('aria-selected', 'false');
expect(getTab('first')).toHaveFocus();
});
});

const Component = () => {
const Component = (props: Partial<SlideTabsProps>) => {
const [activeIndex, setActiveIndex] = useState(0);

return (
<SlideTabs
onChange={setActiveIndex}
tabs={['first', 'second']}
activeIndex={activeIndex}
{...props}
/>
);
};

const getTab = (name: string) => screen.getByRole('tab', { name });
Loading

0 comments on commit c5b46a3

Please sign in to comment.