@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
73 lines • 4.11 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "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/>.
*/
import React, { useContext } from 'react';
import { classNames } from '../_common/defaultImports';
import { forwardRef } from '../helpers';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const TabsContext = React.createContext(null);
const useTabsContext = () => {
const context = 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 = forwardRef(function TabsComponent({ children, size = 'large', fill = 'underline', onChange, value, className, style, as, htmlAttributes, }, ref) {
const classes = classNames('ndl-tabs', getGeneralTabClasses(size, fill), className);
const Component = as || 'div';
return (_jsx(Component, Object.assign({ className: classes, style: style, role: "tablist", ref: ref }, htmlAttributes, { children: _jsx(TabsContext.Provider, { value: { value, size, fill, onChange }, children: children }) })));
});
TabsComponent.displayName =
'Tabs';
const TabsTab = forwardRef(function TabsTab({ children, tabId, isDisabled = false, className, style, as, htmlAttributes, }, ref) {
const { size, fill, value, onChange } = useTabsContext();
const baseClasses = 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 = classNames('ndl-tab', baseClasses, className);
return (_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' && _jsx("span", { className: "ndl-tab-underline" })] })));
});
TabsTab.displayName = 'Tabs.Tab';
const TabsTabPanel = React.forwardRef(function TabsTabPanel({ as, children, value, tabId, style, className, htmlAttributes, }, ref) {
const Component = as || 'div';
return (_jsx(_Fragment, { children: value === tabId && (_jsx(Component, Object.assign({ role: "tabpanel", id: getTabPanelId(tabId), style: style, className: 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,
});
export { Tabs };
//# sourceMappingURL=Tabs.js.map