CustomNavigation.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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] = useState('');
  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. const mounted = useRef(false);
  23. useEffect(() => {
  24. console.log(`2 ${activeTab}`);
  25. if (activeTab === '') {
  26. return;
  27. }
  28. if (mounted.current) {
  29. // Update時の処理
  30. setDefaultActiveTab('');
  31. console.log('Updated!');
  32. const defaultActiveTab = navTabs[0];
  33. const defaultActiveWidth = defaultActiveTab.offsetWidth;
  34. console.log(defaultActiveWidth);
  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. }
  50. else {
  51. // Mount時の処理
  52. mounted.current = true;
  53. console.log('Mounted!');
  54. console.log(`3 ${activeTab}`);
  55. setDefaultActiveTab(activeTab);
  56. setSliderWidth(activeTab.offsetWidth);
  57. }
  58. }, [activeTab]);
  59. function renderNavSlideHr() {
  60. if (defaultActiveTab === activeTab) {
  61. console.log(`defaultActiveTab = ${defaultActiveTab}`);
  62. return;
  63. }
  64. return <hr className="my-0 grw-nav-slide-hr border-none" style={{ width: `${sliderWidth}%`, marginLeft: `${sliderMarginLeft}%` }} />;
  65. }
  66. return (
  67. <React.Fragment>
  68. <Nav className="nav-title grw-custom-navbar" id="grw-custom-navbar">
  69. {Object.entries(props.navTabMapping).map(([key, value]) => {
  70. console.log('return');
  71. console.log(`sliderWidth = ${sliderWidth}% & sliderMarginLeft = ${sliderMarginLeft}%`);
  72. return (
  73. <NavItem key={key} type="button" className={`p-0 grw-custom-navtab ${activeTab === key && 'active'} ${defaultActiveTab === key && 'default-active-tab'}`}>
  74. <NavLink onClick={() => { switchActiveTab(key) }}>
  75. {value.icon}
  76. {value.i18n}
  77. </NavLink>
  78. </NavItem>
  79. );
  80. })}
  81. </Nav>
  82. {renderNavSlideHr()}
  83. {/* <hr className="my-0 grw-nav-slide-hr border-none" style={{ width: `${sliderWidth}%`, marginLeft: `${sliderMarginLeft}%` }} /> */}
  84. <TabContent activeTab={activeTab} className="p-4">
  85. {Object.entries(props.navTabMapping).map(([key, value]) => {
  86. return (
  87. <TabPane key={key} tabId={key}>
  88. {value.tabContent}
  89. </TabPane>
  90. );
  91. })}
  92. </TabContent>
  93. </React.Fragment>
  94. );
  95. };
  96. CustomNavigation.propTypes = {
  97. navTabMapping: PropTypes.object,
  98. };
  99. export default CustomNavigation;