nishant-design-system
Version:
Sense UI components library
126 lines (123 loc) • 4.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TabList = void 0;
var React = _interopRequireWildcard(require("react"));
var _size = require("../../../styles/variables/_size");
var _space = require("../../../styles/variables/_space");
var _classify = require("../../../utils/classify");
var _TabDropdown = require("./TabDropdown");
var _TabListModule = _interopRequireDefault(require("./TabList.module.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const TabList = /*#__PURE__*/React.forwardRef((_ref, forwardRef) => {
let {
classNames,
children,
size,
onSelect,
selectedTab,
menuWidth
} = _ref;
const ref = React.useRef(null);
React.useImperativeHandle(forwardRef, () => ref.current);
const [containerWidth, setContainerWidth] = React.useState(0);
const childrenWithProps = () => {
const childrenArray = React.Children.toArray(children);
const totalChildCount = childrenArray.length;
let tabListWidth = 0;
const menuOptions = [];
let nodes = [];
const tabListNodes = [];
for (let i = 0; i < totalChildCount; i++) {
const child = childrenArray[i];
const {
width = _size.size100,
tabId,
label,
disabled,
iconName,
iconType
} = child.props;
const widthInt = parseInt(width);
tabListWidth = tabListWidth + widthInt;
if (tabListWidth < containerWidth || i === 0) {
const childOnSelect = params => {
onSelect && onSelect(params); // call the tab level onSelect
};
tabListNodes.push( /*#__PURE__*/React.cloneElement(child, {
size,
onSelect: childOnSelect,
selectedTab
}));
} else {
menuOptions.push({
key: tabId,
label,
disabled,
iconLeft: iconName,
iconLeftType: iconType
});
}
}
const menuOnSelect = _ref2 => {
let {
key,
label
} = _ref2;
onSelect && onSelect({
tabId: key,
label
});
};
const selectedKeys = [selectedTab?.tabId || ''];
const isMenuOptionSelected = (() => {
for (let i = 0; i < menuOptions.length; i++) {
if (menuOptions[i].key === selectedTab?.tabId) {
return true;
}
}
return false;
})();
const tabDropDownNode = menuOptions.length ? /*#__PURE__*/React.createElement(_TabDropdown.TabDropdown, {
key: 'tabDropdown' + menuOptions.length,
size: size,
onOptionSelect: menuOnSelect,
props: {
tab: {
iconName: 'ellipsis',
tabId: 'tab-dropdown',
selectedTab: {
tabId: isMenuOptionSelected ? 'tab-dropdown' : ''
}
},
menu: {
isFluid: false,
menuDisabled: false,
options: menuOptions,
selectedKeys,
width: menuWidth
}
}
}) : null;
nodes = [...tabListNodes, tabDropDownNode];
return nodes;
};
React.useLayoutEffect(() => {
if (ref.current && ref.current.offsetWidth) {
const availableContainerWidth = ref.current.offsetWidth - (parseInt(_size.size36) + parseInt(_space.spaceMedium));
setContainerWidth(availableContainerWidth);
}
}, [ref.current]);
return /*#__PURE__*/React.createElement("div", {
ref: ref,
"data-testid": "Tabs",
className: (0, _classify.classify)(_TabListModule.default.tabsContainer, {
[_TabListModule.default.mediumSize]: size === 'medium',
[_TabListModule.default.smallSize]: size === 'small'
}, classNames?.wrapper)
}, containerWidth ? childrenWithProps() : null);
});
exports.TabList = TabList;