CustomNavigation.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. Nav, NavItem, NavLink, TabContent, TabPane,
  5. } from 'reactstrap';
  6. const CustomNavigation = (props) => {
  7. const [activeTab, setActiveTab] = useState(Object.keys(props.navTabMapping)[0]);
  8. console.log(`1 ${activeTab}`);
  9. // [TODO: set default active tab by gw4079]
  10. // const [defaultActiveTab, setDefaultActiveTab] = (null);
  11. const [sliderWidth, setSliderWidth] = useState(null);
  12. const [sliderMarginLeft, setSliderMarginLeft] = useState(0);
  13. function switchActiveTab(activeTab) {
  14. setActiveTab(activeTab);
  15. }
  16. // Might make this dynamic for px, %, pt, em
  17. function getPercentage(min, max) {
  18. return min / max * 100;
  19. }
  20. const navBar = document.getElementById('grw-custom-navbar');
  21. const navTabs = document.querySelectorAll('ul.grw-custom-navbar > li.grw-custom-navtab');
  22. useEffect(() => {
  23. console.log(`2 ${activeTab}`);
  24. if (activeTab === '') {
  25. return;
  26. }
  27. // const defaultActiveTab = navTabs[0];
  28. // const defaultActiveWidth = defaultActiveTab.offsetWidth;
  29. // console.log(defaultActiveWidth);
  30. console.log(`3 ${activeTab}`);
  31. if (navBar == null || navTabs == null) {
  32. console.log(`${navBar},${navTabs}`);
  33. return;
  34. }
  35. let tempML = 0;
  36. console.log(`4 ${activeTab}`);
  37. const styles = [].map.call(navTabs, (el) => {
  38. const width = getPercentage(el.offsetWidth, navBar.offsetWidth);
  39. const marginLeft = tempML;
  40. tempML += width;
  41. return { width, marginLeft };
  42. });
  43. const { width, marginLeft } = styles[props.navTabMapping[activeTab].index];
  44. setSliderWidth(width);
  45. setSliderMarginLeft(marginLeft);
  46. console.log(`5 ${activeTab}`);
  47. }, [activeTab]);
  48. return (
  49. <React.Fragment>
  50. <Nav className="nav-title grw-custom-navbar" id="grw-custom-navbar">
  51. {Object.entries(props.navTabMapping).map(([key, value]) => {
  52. console.log('return');
  53. return (
  54. <NavItem key={key} type="button" className={`p-0 grw-custom-navtab ${activeTab === key && 'active'}`}>
  55. <NavLink onClick={() => { switchActiveTab(key) }}>
  56. {value.icon}
  57. {value.i18n}
  58. </NavLink>
  59. </NavItem>
  60. );
  61. })}
  62. </Nav>
  63. <hr className="my-0 grw-nav-slide-hr border-none" style={{ width: `${sliderWidth}%`, marginLeft: `${sliderMarginLeft}%` }} />
  64. <TabContent activeTab={activeTab} className="p-4">
  65. {Object.entries(props.navTabMapping).map(([key, value]) => {
  66. return (
  67. <TabPane key={key} tabId={key}>
  68. {value.tabContent}
  69. </TabPane>
  70. );
  71. })}
  72. </TabContent>
  73. </React.Fragment>
  74. );
  75. };
  76. CustomNavigation.propTypes = {
  77. navTabMapping: PropTypes.object,
  78. };
  79. export default CustomNavigation;