UNPKG

aws-northstar

Version:
116 lines (112 loc) 6.56 kB
"use strict"; /** ******************************************************************************************************************* 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = __importStar(require("react")); const clsx_1 = __importDefault(require("clsx")); const styles_1 = require("@material-ui/core/styles"); const Typography_1 = __importDefault(require("@material-ui/core/Typography")); const Tab_1 = __importDefault(require("@material-ui/core/Tab")); const Tabs_1 = __importDefault(require("@material-ui/core/Tabs")); const Container_1 = __importDefault(require("../../layouts/Container")); const Box_1 = __importDefault(require("../../layouts/Box")); const usePrevious_1 = __importDefault(require("../../hooks/usePrevious")); const useStyles = (0, styles_1.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_1.default.createElement(Typography_1.default, { component: "div", role: "tabpanel", hidden: value !== index, id: `tabpanel-${index}`, "data-testid": props['data-testid'] }, react_1.default.createElement(Box_1.default, { 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_1.default.useState(() => { const tabIndex = tabs.findIndex((tab) => tab.id === activeId); return tabIndex === -1 ? 0 : tabIndex; }); const previousActiveId = (0, usePrevious_1.default)(activeId); const handleChange = (0, react_1.useCallback)((_event, index) => { onChange === null || onChange === void 0 ? void 0 : onChange(tabs[index].id); setValue(index); }, [onChange, tabs]); (0, react_1.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 = (0, react_1.useMemo)(() => (react_1.default.createElement(Tabs_1.default, { variant: "scrollable", indicatorColor: "secondary", TabIndicatorProps: { color: 'primary' }, value: value, onChange: handleChange, className: (0, clsx_1.default)({ [classes.noBorder]: variant === 'container' }), "data-testid": `${testId}-header` }, tabs.map((tab) => (react_1.default.createElement(Tab_1.default, { 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 = (0, react_1.useMemo)(() => tabs.map((tab, idx) => (react_1.default.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_1.default.createElement(Container_1.default, { "data-testid": testId, headerContent: headerContent, headerGutters: false, gutters: paddingContentArea }, tabContent)) : (react_1.default.createElement(Box_1.default, { "data-testid": testId }, headerContent, tabContent)); }; exports.default = Tabs;