@gpa-gemstone/react-interactive
Version:
Interactive UI Components for GPA products
92 lines (91 loc) • 5.45 kB
JavaScript
// ******************************************************************************************************
// TabSelector.tsx - Gbtc
//
// Copyright © 2021, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/20/2021 - Christoph Lackner
// Generated original version of source code.
// ******************************************************************************************************
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 });
var React = __importStar(require("react"));
var helper_functions_1 = require("@gpa-gemstone/helper-functions");
/**
* Component for rendering a dropdown tab selector.
* @param props - configures and manages tab selector.
*/
var TabSelector = function (props) {
// State to manage things like visible tabs and container width.
var containerRef = React.useRef(null);
var width = (0, helper_functions_1.useGetContainerPosition)(containerRef).width;
var _a = React.useState(1), numVisibleTabs = _a[0], setNumVisibleTabs = _a[1];
var _b = React.useState(false), dropDownOpen = _b[0], setDropDownOpen = _b[1];
// Resets dropdown state with the CurrentTab changes.
React.useEffect(function () {
setDropDownOpen(false);
}, [props.CurrentTab]);
// Dynamically calculates number of visible tabs.
React.useEffect(function () {
var totalAllocatedWidth = 40;
var visibleTabCount = 0;
while (visibleTabCount < props.Tabs.length) {
var currentTabWidth = 2 * 17 + (0, helper_functions_1.GetTextWidth)("Segoe UI", '1em', props.Tabs[visibleTabCount].Label) + 1;
if (totalAllocatedWidth + currentTabWidth > (width - 2))
break;
totalAllocatedWidth = totalAllocatedWidth + currentTabWidth;
visibleTabCount = visibleTabCount + 1;
}
setNumVisibleTabs(visibleTabCount);
}, [width, props.Tabs]);
// Determines if there are more tabs to show in dropdown option.
var showDropDown = numVisibleTabs < props.Tabs.length;
if (width < 50)
return React.createElement("div", { style: { width: '100%' }, ref: containerRef }, " ");
return React.createElement("div", { style: { width: '100%' }, ref: containerRef },
React.createElement("ul", { className: "nav nav-tabs", style: { maxHeight: 38 } },
props.Tabs.map(function (t, i) { return i > (numVisibleTabs - 1) ? null :
React.createElement("li", { className: "nav-item", key: i, style: { cursor: 'pointer' } },
React.createElement("a", { className: "nav-link" + (props.CurrentTab === t.Id ? " active" : ""), onClick: function () { return props.SetTab(t.Id); } }, t.Label)); }),
showDropDown ?
React.createElement("li", { className: "nav-item dropdown ".concat(dropDownOpen ? ' show' : ''), style: { zIndex: 1040 } },
React.createElement("a", { className: "nav-link dropdown-toggle", onClick: function () { return setDropDownOpen(function (s) { return !s; }); } }, "..."),
React.createElement("div", { className: "dropdown-menu dropdown-menu-right ".concat(dropDownOpen ? ' show' : '') }, props.Tabs.map(function (t, i) { return i > (numVisibleTabs - 1) ?
React.createElement("a", { className: "dropdown-item ".concat(props.CurrentTab === t.Id ? ' active' : ''), style: { cursor: 'pointer' }, onClick: function () { return props.SetTab(t.Id); }, key: i }, t.Label) : null; }))) : null));
};
exports.default = TabSelector;
;