CustomNavigation.jsx 4.1 KB

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