CustomNavigation.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // const refs = useRef([React.createRef()]);
  9. const refs = useRef([null]);
  10. console.log(`customNavigation ${activeTab}`);
  11. const [defaultActiveTab, setDefaultActiveTab] = useState('');
  12. const [sliderWidth, setSliderWidth] = useState(null);
  13. const [sliderMarginLeft, setSliderMarginLeft] = useState(0);
  14. function switchActiveTab(activeTab) {
  15. setActiveTab(activeTab);
  16. }
  17. // Might make this dynamic for px, %, pt, em
  18. function getPercentage(min, max) {
  19. return min / max * 100;
  20. }
  21. const navBar = document.getElementById('grw-custom-navbar');
  22. const navTabs = document.querySelectorAll('ul.grw-custom-navbar > li.grw-custom-navtab');
  23. // const mounted = useRef(false);
  24. useEffect(() => {
  25. console.log(`useEffecet ${activeTab}`);
  26. if (activeTab === '') {
  27. return;
  28. }
  29. // if (mounted.current) {
  30. console.log(`ref = ${refs}`);
  31. console.log(`refs.current[0] = ${refs.current[0]}`);
  32. // console.log(`activeTab = ${elm2.current.offsetWidth}`);
  33. setDefaultActiveTab('');
  34. console.log('Updated!');
  35. if (navBar == null || navTabs == null) {
  36. console.log(`${navBar},${navTabs}`);
  37. return;
  38. }
  39. let tempML = 0;
  40. const styles = [].map.call(navTabs, (el) => {
  41. const width = getPercentage(el.offsetWidth, navBar.offsetWidth);
  42. const marginLeft = tempML;
  43. tempML += width;
  44. return { width, marginLeft };
  45. });
  46. const { width, marginLeft } = styles[props.navTabMapping[activeTab].index];
  47. setSliderWidth(width);
  48. setSliderMarginLeft(marginLeft);
  49. console.log(`sliderWidth = ${sliderWidth}`);
  50. // }
  51. // else {
  52. // // Mounted
  53. // console.log(JSON.stringify(elm.current.getBoundingClientRect()));
  54. // mounted.current = true;
  55. // console.log(`Mounted! ${activeTab}`);
  56. // setDefaultActiveTab(activeTab);
  57. // setSliderWidth(activeTab.offsetWidth);
  58. // }
  59. }, [activeTab]);
  60. // function renderNavSlideHr() {
  61. // if (defaultActiveTab === activeTab) {
  62. // console.log('1st');
  63. // console.log(`defaultActiveTab = ${defaultActiveTab}`);
  64. // return;
  65. // }
  66. // console.log('2nd');
  67. // return <hr className="my-0 grw-nav-slide-hr border-none" style={{ width: `${sliderWidth}%`, marginLeft: `${sliderMarginLeft}%` }} />;
  68. // }
  69. return (
  70. <React.Fragment>
  71. <Nav className="nav-title grw-custom-navbar" id="grw-custom-navbar">
  72. {Object.entries(props.navTabMapping).map(([key, value]) => {
  73. console.log('return');
  74. return (
  75. <NavItem
  76. key={key}
  77. type="button"
  78. className={`p-0 grw-custom-navtab ${activeTab === key && 'active'} ${defaultActiveTab === key && 'default-active-tab'}`}
  79. >
  80. <NavLink ref={refs.current[key]} onClick={() => { switchActiveTab(key) }}>
  81. {value.icon}
  82. {value.i18n}
  83. </NavLink>
  84. </NavItem>
  85. );
  86. })}
  87. </Nav>
  88. {/* {renderNavSlideHr()} */}
  89. <hr className="my-0 grw-nav-slide-hr border-none" style={{ width: `${sliderWidth}%`, marginLeft: `${sliderMarginLeft}%` }} />
  90. <TabContent activeTab={activeTab} className="p-4">
  91. {Object.entries(props.navTabMapping).map(([key, value]) => {
  92. return (
  93. <TabPane key={key} tabId={key}>
  94. {value.tabContent}
  95. </TabPane>
  96. );
  97. })}
  98. </TabContent>
  99. </React.Fragment>
  100. );
  101. };
  102. CustomNavigation.propTypes = {
  103. navTabMapping: PropTypes.object,
  104. };
  105. export default CustomNavigation;