UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

99 lines 5.49 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Tabs = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ const react_1 = __importStar(require("react")); const defaultImports_1 = require("../_common/defaultImports"); const helpers_1 = require("../helpers"); // eslint-disable-next-line @typescript-eslint/no-explicit-any const TabsContext = react_1.default.createContext(null); const useTabsContext = () => { const context = (0, react_1.useContext)(TabsContext); if (context === null) { throw new Error('Tab used without context'); } return context; }; const getTabPanelId = (tabId) => `tabpanel-${tabId}`; const getGeneralTabClasses = (size, fill) => { return { 'ndl-large': size === 'large', 'ndl-small': size === 'small', 'ndl-filled-tab': fill === 'filled', 'ndl-underline-tab': fill === 'underline', }; }; const TabsComponent = (0, helpers_1.forwardRef)(function TabsComponent({ children, size = 'large', fill = 'underline', onChange, value, className, style, as, htmlAttributes, }, ref) { const classes = (0, defaultImports_1.classNames)('ndl-tabs', getGeneralTabClasses(size, fill), className); const Component = as || 'div'; return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, style: style, role: "tablist", ref: ref }, htmlAttributes, { children: (0, jsx_runtime_1.jsx)(TabsContext.Provider, { value: { value, size, fill, onChange }, children: children }) }))); }); TabsComponent.displayName = 'Tabs'; const TabsTab = (0, helpers_1.forwardRef)(function TabsTab({ children, tabId, isDisabled = false, className, style, as, htmlAttributes, }, ref) { const { size, fill, value, onChange } = useTabsContext(); const baseClasses = (0, defaultImports_1.classNames)(Object.assign(Object.assign({}, getGeneralTabClasses(size, fill)), { 'ndl-selected': value === tabId, 'ndl-disabled': isDisabled })); const Component = as || 'button'; // Cast onChange since the compiler isn't smart enough to figure out union of function's parameters // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-3.html#improved-behavior-for-calling-union-types // https://stackoverflow.com/questions/55572797/why-does-typescript-expect-never-as-function-argument-when-retrieving-the-func const castOnChange = onChange; const handleChange = () => !isDisabled && castOnChange(tabId); const classes = (0, defaultImports_1.classNames)('ndl-tab', baseClasses, className); return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ className: classes, style: style, onClick: handleChange, role: "tab", "aria-controls": value === tabId ? getTabPanelId(tabId) : undefined, ref: ref }, htmlAttributes, { children: [children, fill === 'underline' && (0, jsx_runtime_1.jsx)("span", { className: "ndl-tab-underline" })] }))); }); TabsTab.displayName = 'Tabs.Tab'; const TabsTabPanel = react_1.default.forwardRef(function TabsTabPanel({ as, children, value, tabId, style, className, htmlAttributes, }, ref) { const Component = as || 'div'; return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: value === tabId && ((0, jsx_runtime_1.jsx)(Component, Object.assign({ role: "tabpanel", id: getTabPanelId(tabId), style: style, className: (0, defaultImports_1.classNames)(className), ref: ref }, htmlAttributes, { children: children }))) })); }); // To work with forwardRef: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34757#issuecomment-894053907 const Tabs = Object.assign(TabsComponent, { Tab: TabsTab, TabPanel: TabsTabPanel, }); exports.Tabs = Tabs; //# sourceMappingURL=Tabs.js.map