aws-northstar
Version:
NorthStar Design System
88 lines (84 loc) • 4.93 kB
JavaScript
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. *
******************************************************************************************************************** */
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React, { useCallback, useEffect, useMemo } from 'react';
import clsx from 'clsx';
import { makeStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import Tab from '@material-ui/core/Tab';
import MuiTabs from '@material-ui/core/Tabs';
import Container from '../../layouts/Container';
import Box from '../../layouts/Box';
import usePrevious from '../../hooks/usePrevious';
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',
},
},
}));
function TabPanel(_a) {
var { children, value, index, paddingContentArea } = _a, props = __rest(_a, ["children", "value", "index", "paddingContentArea"]);
return (React.createElement(Typography, { component: "div", role: "tabpanel", hidden: value !== index, id: `tabpanel-${index}`, "data-testid": props['data-testid'] },
React.createElement(Box, { py: paddingContentArea ? 3 : undefined }, children)));
}
/**
* Use tabs for organizing discrete blocks of information.
*/
const Tabs = (_a) => {
var { tabs, activeId, variant = 'default', paddingContentArea = true, onChange } = _a, props = __rest(_a, ["tabs", "activeId", "variant", "paddingContentArea", "onChange"]);
const classes = useStyles({});
const [value, setValue] = React.useState(() => {
const tabIndex = tabs.findIndex((tab) => tab.id === activeId);
return tabIndex === -1 ? 0 : tabIndex;
});
const previousActiveId = usePrevious(activeId);
const handleChange = useCallback((_event, index) => {
onChange === null || onChange === void 0 ? void 0 : onChange(tabs[index].id);
setValue(index);
}, [onChange, tabs]);
useEffect(() => {
if (typeof activeId !== 'undefined' && previousActiveId !== activeId) {
// Only fired when activeId change
const tabIndex = tabs.findIndex((tab) => tab.id === activeId);
if (tabIndex !== -1 && tabIndex !== value) {
setValue(tabIndex);
}
}
}, [activeId, previousActiveId, tabs, value]);
const testId = props['data-testid'] || 'tabs';
const headerContent = useMemo(() => (React.createElement(MuiTabs, { variant: "scrollable", indicatorColor: "secondary", TabIndicatorProps: { color: 'primary' }, value: value, onChange: handleChange, className: clsx({ [classes.noBorder]: variant === 'container' }), "data-testid": `${testId}-header` }, tabs.map((tab) => (React.createElement(Tab, { key: tab.id, "data-testid": `${testId}-header-${tab.id}`, className: classes.tab, label: tab.label, disabled: tab.disabled }))))), [tabs, variant, value, handleChange, classes, testId]);
const tabContent = useMemo(() => tabs.map((tab, idx) => (React.createElement(TabPanel, { key: tab.id, "data-testid": `${testId}-content-${tab.id}`, value: value, index: idx, paddingContentArea: paddingContentArea }, tab.content))), [tabs, value, paddingContentArea, testId]);
return variant === 'container' ? (React.createElement(Container, { "data-testid": testId, headerContent: headerContent, headerGutters: false, gutters: paddingContentArea }, tabContent)) : (React.createElement(Box, { "data-testid": testId },
headerContent,
tabContent));
};
export default Tabs;