Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/react-aria-components/stories/Tabs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import {Button, Tab, TabList, TabPanel, TabProps, Tabs, TabsProps} from 'react-aria-components';
import React, {useState} from 'react';
import {RouterProvider} from '@react-aria/utils';

export default {
title: 'React Aria Components'
};

export const TabsExample = () => {
let [url, setUrl] = useState('/FoR');

return (
<RouterProvider navigate={setUrl}>
<Tabs selectedKey={url}>
<TabList aria-label="History of Ancient Rome" style={{display: 'flex', gap: 8}}>
<CustomTab id="/FoR" href="/FoR">Founding of Rome</CustomTab>
<CustomTab id="/MaR" href="/MaR">Monarchy and Republic</CustomTab>
<CustomTab id="/Emp" href="/Emp">Empire</CustomTab>
</TabList>
<TabPanel id="/FoR">
Arma virumque cano, Troiae qui primus ab oris.
</TabPanel>
<TabPanel id="/MaR">
Senatus Populusque Romanus.
</TabPanel>
<TabPanel id="/Emp">
Alea jacta est.
</TabPanel>
</Tabs>
</RouterProvider>
);
};

// Has error with invalid aria-controls, bug documented here: https://github.com/adobe/react-spectrum/issues/4781#issuecomment-1641057070
export const TabsRenderProps = () => {
const [tabOrientation, setTabOrientation] = useState<TabsProps['orientation']>('vertical');

return (
<div style={{display: 'flex', flexDirection: 'row', gap: 8}}>
<Button onPress={() => setTabOrientation((current) => current === 'vertical' ? 'horizontal' : 'vertical')}>
Change Orientation
</Button>
<Tabs orientation={tabOrientation}>
{({orientation}) => (
<div>
<div style={{display: 'flex', flexDirection: orientation === 'vertical' ? 'row' : 'column', gap: 8}}>
<TabList
aria-label="History of Ancient Rome"
style={{display: 'flex', flexDirection: orientation === 'vertical' ? 'column' : 'row', gap: 8}}>
<CustomTab id="FoR">Founding of Rome</CustomTab>
<CustomTab id="MaR">Monarchy and Republic</CustomTab>
<CustomTab id="Emp">Empire</CustomTab>
</TabList>
<TabPanel id="FoR">
Arma virumque cano, Troiae qui primus ab oris.
</TabPanel>
<TabPanel id="MaR">
Senatus Populusque Romanus.
</TabPanel>
<TabPanel id="Emp">
Alea jacta est.
</TabPanel>
</div>
</div>
)}
</Tabs>
</div>
);
};

const CustomTab = (props: TabProps) => {
return (
<Tab
{...props}
style={({isSelected}) => ({
borderBottom: '2px solid ' + (isSelected ? 'slateblue' : 'transparent')
})} />
);
};
77 changes: 2 additions & 75 deletions packages/react-aria-components/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
*/

import {action} from '@storybook/addon-actions';
import {Button, Calendar, CalendarCell, CalendarGrid, Cell, Checkbox, Column, ColumnResizer, DateField, DateInput, DatePicker, DateRangePicker, DateSegment, Dialog, DialogTrigger, DropZone, FileTrigger, Group, Header, Heading, Input, Keyboard, Label, Link, ListBox, ListBoxItem, ListBoxProps, Menu, MenuItem, MenuTrigger, Modal, ModalOverlay, NumberField, OverlayArrow, Popover, Radio, RadioGroup, RangeCalendar, ResizableTableContainer, Row, SearchField, Section, Select, SelectValue, Separator, Slider, SliderOutput, SliderThumb, SliderTrack, Switch, Tab, Table, TableBody, TableHeader, TabList, TabPanel, Tabs, TabsProps, Tag, TagGroup, TagList, Text, TextField, TimeField, ToggleButton, Toolbar, Tooltip, TooltipTrigger, useDragAndDrop} from 'react-aria-components';
import {Button, Calendar, CalendarCell, CalendarGrid, Cell, Checkbox, Column, ColumnResizer, DateField, DateInput, DatePicker, DateRangePicker, DateSegment, Dialog, DialogTrigger, DropZone, FileTrigger, Group, Header, Heading, Input, Keyboard, Label, Link, ListBox, ListBoxItem, ListBoxProps, Menu, MenuItem, MenuTrigger, Modal, ModalOverlay, NumberField, OverlayArrow, Popover, Radio, RadioGroup, RangeCalendar, ResizableTableContainer, Row, SearchField, Section, Select, SelectValue, Separator, Slider, SliderOutput, SliderThumb, SliderTrack, Switch, Table, TableBody, TableHeader, Tag, TagGroup, TagList, Text, TextField, TimeField, ToggleButton, Toolbar, Tooltip, TooltipTrigger, useDragAndDrop} from 'react-aria-components';
import {classNames} from '@react-spectrum/utils';
import clsx from 'clsx';
import {FocusRing, isTextDropItem, mergeProps, useButton, useClipboard, useDrag} from 'react-aria';
import {MyListBoxItem} from './utils';
import React, {useRef, useState} from 'react';
import {RouterProvider} from '@react-aria/utils';
import React, {useRef} from 'react';
import styles from '../example/index.css';
import {useListData} from 'react-stately';

Expand Down Expand Up @@ -466,68 +465,6 @@ export const ModalExample = () => (
</DialogTrigger>
);

export const TabsExample = () => {
let [url, setUrl] = useState('/FoR');

return (
<RouterProvider navigate={setUrl}>
<Tabs selectedKey={url}>
<TabList aria-label="History of Ancient Rome" style={{display: 'flex', gap: 8}}>
<CustomTab id="/FoR" href="/FoR">Founding of Rome</CustomTab>
<CustomTab id="/MaR" href="/MaR">Monarchy and Republic</CustomTab>
<CustomTab id="/Emp" href="/Emp">Empire</CustomTab>
</TabList>
<TabPanel id="/FoR">
Arma virumque cano, Troiae qui primus ab oris.
</TabPanel>
<TabPanel id="/MaR">
Senatus Populusque Romanus.
</TabPanel>
<TabPanel id="/Emp">
Alea jacta est.
</TabPanel>
</Tabs>
</RouterProvider>
);
};

// Has error with invalid aria-controls, bug documented here: https://github.com/adobe/react-spectrum/issues/4781#issuecomment-1641057070
export const TabsRenderProps = () => {
const [tabOrientation, setTabOrientation] = useState<TabsProps['orientation']>('vertical');

return (
<div style={{display: 'flex', flexDirection: 'row', gap: 8}}>
<Button onPress={() => setTabOrientation((current) => current === 'vertical' ? 'horizontal' : 'vertical')}>
Change Orientation
</Button>
<Tabs orientation={tabOrientation}>
{({orientation}) => (
<div>
<div style={{display: 'flex', flexDirection: orientation === 'vertical' ? 'row' : 'column', gap: 8}}>
<TabList
aria-label="History of Ancient Rome"
style={{display: 'flex', flexDirection: orientation === 'vertical' ? 'column' : 'row', gap: 8}}>
<CustomTab id="FoR">Founding of Rome</CustomTab>
<CustomTab id="MaR">Monarchy and Republic</CustomTab>
<CustomTab id="Emp">Empire</CustomTab>
</TabList>
<TabPanel id="FoR">
Arma virumque cano, Troiae qui primus ab oris.
</TabPanel>
<TabPanel id="MaR">
Senatus Populusque Romanus.
</TabPanel>
<TabPanel id="Emp">
Alea jacta est.
</TabPanel>
</div>
</div>
)}
</Tabs>
</div>
);
};

const ReorderableTable = ({initialItems}: {initialItems: {id: string, name: string}[]}) => {
let list = useListData({initialItems});

Expand Down Expand Up @@ -764,16 +701,6 @@ function CustomThumb({index, children}) {
);
}

function CustomTab(props) {
return (
<Tab
{...props}
style={({isSelected}) => ({
borderBottom: '2px solid ' + (isSelected ? 'slateblue' : 'transparent')
})} />
);
}

function Draggable() {
let buttonRef = useRef(null);
let {dragProps, isDragging} = useDrag({
Expand Down
2 changes: 1 addition & 1 deletion packages/react-aria-components/test/Tabs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import {act, fireEvent, pointerMap, render, within} from '@react-spectrum/test-utils';
import React from 'react';
import {Tab, TabList, TabPanel, Tabs} from '../';
import {TabsExample} from '../stories/index.stories';
import {TabsExample} from '../stories/Tabs.stories';
import userEvent from '@testing-library/user-event';

let renderTabs = (tabsProps, tablistProps, tabProps, tabpanelProps) => render(
Expand Down