CustomTabContent.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React, { useEffect, useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. TabContent, TabPane,
  5. } from 'reactstrap';
  6. import { ICustomNavTabMappings } from '~/interfaces/ui';
  7. type Props = {
  8. activeTab: string,
  9. navTabMapping: ICustomNavTabMappings,
  10. additionalClassNames?: string[],
  11. }
  12. const CustomTabContent = (props: Props): JSX.Element => {
  13. const { activeTab, navTabMapping, additionalClassNames } = props;
  14. const [activatedContent, setActivatedContent] = useState(new Set([activeTab]));
  15. // add activated content to Set
  16. useEffect(() => {
  17. setActivatedContent(activatedContent.add(activeTab));
  18. }, [activatedContent, activeTab]);
  19. return (
  20. <TabContent activeTab={activeTab} className={additionalClassNames != null ? additionalClassNames.join(' ') : ''}>
  21. {Object.entries(navTabMapping).map(([key, value]) => {
  22. const shouldRender = key === activeTab || activatedContent.has(key);
  23. const { Content } = value;
  24. return (
  25. <TabPane key={key} tabId={key}>
  26. { shouldRender && <Content /> }
  27. </TabPane>
  28. );
  29. })}
  30. </TabContent>
  31. );
  32. };
  33. CustomTabContent.propTypes = {
  34. activeTab: PropTypes.string.isRequired,
  35. navTabMapping: PropTypes.object.isRequired,
  36. additionalClassNames: PropTypes.arrayOf(PropTypes.string),
  37. };
  38. CustomTabContent.defaultProps = {
  39. additionalClassNames: [],
  40. };
  41. export default CustomTabContent;