@appbuckets/react-ui
Version:
Just Another React UI Framework
177 lines (174 loc) • 4.87 kB
JavaScript
import { __rest, __read, __assign } from 'tslib';
import * as React from 'react';
import clsx from 'clsx';
import {
createShorthandFactory,
useAutoControlledValue,
useElementType,
} from '@appbuckets/react-ui-core';
import { useSharedClassName } from '../utils/customHook.js';
import '../BucketTheme/BucketTheme.js';
import { useWithDefaultProps } from '../BucketTheme/BucketContext.js';
import Column from '../Column/Column.js';
import Menu from '../Menu/Menu.js';
import Row from '../Row/Row.js';
import TabPanel from './TabPanel.js';
/* --------
* Component Render
* -------- */
var Tabs = function (receivedProps) {
var _a;
var props = useWithDefaultProps('tabs', receivedProps);
var _b = useSharedClassName(props),
className = _b.className,
_c = _b.rest,
userDefinedActiveIndex = _c.activeIndex;
_c.content;
var defaultActiveIndex = _c.defaultActiveIndex,
layout = _c.layout,
menu = _c.menu,
onTabChange = _c.onTabChange,
panels = _c.panels,
renderActiveOnly = _c.renderActiveOnly;
_c.vertical;
var rest = __rest(_c, [
'activeIndex',
'content',
'defaultActiveIndex',
'layout',
'menu',
'onTabChange',
'panels',
'renderActiveOnly',
'vertical',
]);
/** Control active tab */
var _d = __read(
useAutoControlledValue(0, {
prop: userDefinedActiveIndex,
defaultProp: defaultActiveIndex,
}),
2
),
activeIndex = _d[0],
trySetActiveIndex = _d[1];
/** Get the component element type */
var ElementType = useElementType(Tabs, receivedProps, props);
/** Build the element class list */
var classes = clsx('tabs', className);
// ----
// Define Handlers
// ----
var handleMenuItemClick = function (e, menuItemProps) {
/** Get menu item index */
var index = menuItemProps.index;
/** Fire user defined callback */
if (onTabChange) {
onTabChange(e, __assign(__assign({}, props), { activeIndex: index }));
}
/** Try to set the new index */
trySetActiveIndex(index !== null && index !== void 0 ? index : 0);
};
// ----
// Define SubComponent Render
// ----
/** Current Active Panel */
var activePanel = Array.isArray(panels)
? (_a = panels[activeIndex]) === null || _a === void 0
? void 0
: _a.panel
: undefined;
/** Rendered Panels Element */
var renderTabsElement = function () {
/** If no active panel, or no Array, return empty component */
if (!Array.isArray(panels) || !activePanel) {
return null;
}
/** If must render active index only return a single panel */
if (renderActiveOnly) {
return TabPanel.create(activePanel, {
autoGenerateKey: false,
overrideProps: { active: true },
});
}
/** Else, return a tab panels collection */
return panels.map(function (_a, index) {
var panel = _a.panel;
return TabPanel.create(panel, {
autoGenerateKey: true,
overrideProps: {
active: index === activeIndex,
},
});
});
};
/** Menu Element */
var renderMenuElement = function () {
/** If no panels, don't render the menu */
if (!Array.isArray(panels)) {
return null;
}
/** Get Triggers */
var triggers = panels.map(function (panel) {
return panel.trigger;
});
/** Return the menu */
return Menu.create(menu, {
autoGenerateKey: false,
overrideProps: {
items: triggers,
onItemClick: handleMenuItemClick,
activeIndex: activeIndex,
},
});
};
// ----
// Render Vertical Layout Element
// ----
if (menu === null || menu === void 0 ? void 0 : menu.vertical) {
var menuColumn = React.createElement(
Column,
{
width: layout === null || layout === void 0 ? void 0 : layout.menuWidth,
},
renderMenuElement()
);
return React.createElement(
ElementType,
__assign({}, rest, { className: classes }),
React.createElement(
Row,
null,
(layout === null || layout === void 0 ? void 0 : layout.menuOn) ===
'left' && menuColumn,
React.createElement(
Column,
{
width:
layout === null || layout === void 0 ? void 0 : layout.panelWidth,
},
renderTabsElement()
),
(layout === null || layout === void 0 ? void 0 : layout.menuOn) ===
'right' && menuColumn
)
);
}
// ----
// Render Horizontal Layout Element
// ----
return React.createElement(
ElementType,
__assign({}, rest, { className: classes }),
renderMenuElement(),
renderTabsElement()
);
};
Tabs.displayName = 'Tabs';
Tabs.create = createShorthandFactory(Tabs, function (panels) {
return {
panels: panels,
};
});
Tabs.Panel = TabPanel;
export { Tabs as default };