/*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ import { makeStyles } from '@material-ui/core/styles'; import Box from 'aws-northstar/layouts/Box'; import Container from 'aws-northstar/layouts/Container'; import MuiTabs, { TabsProps as MuiTabsProps } from '@material-ui/core/Tabs'; import React, { ReactElement, ReactNode } from 'react'; import Tab, { TabProps as MuiTabProps } from '@material-ui/core/Tab'; import Typography from '@material-ui/core/Typography'; import clsx from 'clsx'; const useStyles = makeStyles((theme) => ({ tab: { marginRight: '-1px', borderRight: `1px solid ${theme.palette.grey[400]}`, padding: '5px 20px', '&:last-child': { borderRight: 'none', }, }, noBorder: { '& .MuiTabs-scroller': { borderBottom: 'none', }, }, })); export interface TabItem { label: string | ReactNode; id: string; content: ReactNode; disabled?: boolean; } export type TabPropsOverrides = Omit; export type TabsPropsOverrides = Omit; export interface TabsProps { /** * Array of objects, each having the following properties:
* - id [string]: The tab id, this value will be set to activeTabId when the tab is selected.
* - label [string]: Tab label shown in the UI.
* - content [ReactNode]: Tab content to render in the container.
* - disabled [boolean]: Whether this item is disabled. */ tabs: TabItem[]; /** Id of the currently active tab. Updates are triggered upon the change event. */ activeId?: string; /** * Visual of the tabs:
* - default: can be used in any context
* - container: version with borders, designed to be used along with other containers */ variant?: 'default' | 'container'; /** * Whether to render padding within the content area * */ paddingContentArea?: boolean; /** * Fired whenever the user selects a different tab. The event detail contains the current activeTabId. */ onChange?: (activeTabId: string) => void; tabProps?: TabPropsOverrides; tabsProps?: TabsPropsOverrides; } interface TabPanelProps { children?: React.ReactNode; index: number; value: number; paddingContentArea: boolean; } function TabPanel({ children, value, index, paddingContentArea }: TabPanelProps) { return ( ); } /** * Use tabs for organizing discrete blocks of information. */ export const Tabs = ({ tabs, activeId = '', variant = 'default', paddingContentArea = true, tabProps, tabsProps, onChange, }: TabsProps): ReactElement => { const classes = useStyles({}); const tabIndex = tabs.findIndex((tab) => tab.id === activeId); const [value, setValue] = React.useState(tabIndex === -1 ? 0 : tabIndex); const handleChange = (_event: React.ChangeEvent<{}>, index: number) => { onChange?.(tabs[index].id); setValue(index); }; const headerContent = ( {tabs.map((tab) => ( ))} ); const tabContent = tabs.map((tab, idx) => ( {tab.content} )); return variant === 'container' ? ( {tabContent} ) : ( <> {headerContent} {tabContent} ); }; export default Tabs;