|
| 1 | +import { PropsWithTestId } from '@leanstacks/react-common'; |
| 2 | +import { useSearchParams } from 'react-router-dom'; |
| 3 | +import classNames from 'classnames'; |
| 4 | + |
| 5 | +import { toNumberBetween } from 'utils/numbers'; |
| 6 | +import { SearchParam } from 'utils/constants'; |
| 7 | +import Tab, { TabProps } from './Tab'; |
| 8 | +import TabContent, { TabContentProps } from './TabContent'; |
| 9 | + |
| 10 | +/** |
| 11 | + * The `TabVariant` describes variations of display behavior for `Tabs`. |
| 12 | + */ |
| 13 | +type TabVariant = 'fullWidth' | 'standard'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Properties for the `Tabs` React component. |
| 17 | + * @param {TabProps[]} tabs - An array of `Tab` component properties. |
| 18 | + * @param {TabConent[]} tabContents - An array of `TabContent` component properties. |
| 19 | + * @param {TabVariant} [variant='standard'] - Optional. The tab display behavior. |
| 20 | + * Default: `standard`. |
| 21 | + * @see {@link PropsWithTestId} |
| 22 | + */ |
| 23 | +interface TabsProps extends PropsWithTestId { |
| 24 | + tabs: Omit<TabProps, 'isActive' | 'onClick'>[]; |
| 25 | + tabContents: TabContentProps[]; |
| 26 | + variant?: TabVariant; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * The `Tabs` component is a wrapper for rendering tabbed content. |
| 31 | + * |
| 32 | + * Supply one to many `TabProps` objects in the `tabs` property describing each |
| 33 | + * `Tab` to render. Supply one to many `TabContentProps` objects in the `tabContents` property |
| 34 | + * describing each `TabContent` to render. |
| 35 | + * |
| 36 | + * The number of `tabs` and `tabContents` items should be equal. The order of each array |
| 37 | + * matters. The first item in the `tabs` array should correspond to content in the first |
| 38 | + * item in the `tabContents` array and so on. |
| 39 | + * |
| 40 | + * *Example:* |
| 41 | + * ``` |
| 42 | + * <Tabs |
| 43 | + * tabs={[ |
| 44 | + * { label: 'List', testId: 'tab-list' }, |
| 45 | + * { label: 'Detail', testId: 'tab-detail' }, |
| 46 | + * ]} |
| 47 | + * tabContents={[{ children: <MyList /> }, { children: <Outlet />, className: 'my-6' }]} |
| 48 | + * /> |
| 49 | + * ``` |
| 50 | + * @param {TabsProps} - Component properties |
| 51 | + * @returns {JSX.Element} JSX |
| 52 | + */ |
| 53 | +const Tabs = ({ |
| 54 | + tabs, |
| 55 | + tabContents, |
| 56 | + testId = 'tabs', |
| 57 | + variant = 'standard', |
| 58 | +}: TabsProps): JSX.Element => { |
| 59 | + const [searchParams, setSearchParams] = useSearchParams(); |
| 60 | + |
| 61 | + // obtain activeTabIndex from query string |
| 62 | + const activeTabIndex = toNumberBetween(searchParams.get(SearchParam.tab), 0, tabs.length - 1, 0); |
| 63 | + |
| 64 | + /** |
| 65 | + * Set the active tab index. |
| 66 | + * @param {number} index - A tab index. |
| 67 | + */ |
| 68 | + const setTab = (index: number = 0): void => { |
| 69 | + const tabIndex = toNumberBetween(index, 0, tabs.length - 1, 0); |
| 70 | + if (tabIndex !== activeTabIndex) { |
| 71 | + searchParams.set(SearchParam.tab, tabIndex.toString()); |
| 72 | + setSearchParams(searchParams); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div data-testid={testId}> |
| 78 | + <div className="flex gap-4 border-b border-b-neutral-500/10" data-testid={`${testId}-tabs`}> |
| 79 | + {tabs.map(({ className, ...tabProps }, index) => ( |
| 80 | + <Tab |
| 81 | + {...tabProps} |
| 82 | + className={classNames({ className, 'flex-grow': variant === 'fullWidth' })} |
| 83 | + isActive={activeTabIndex === index} |
| 84 | + onClick={() => setTab(index)} |
| 85 | + key={index} |
| 86 | + /> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + <div data-testid={`${testId}-content`}> |
| 90 | + <TabContent {...tabContents[activeTabIndex]} /> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
| 95 | + |
| 96 | +export default Tabs; |
0 commit comments