TabRegion.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import TabSelector from "./TabSelector";
  4. import Loading from "./Loading";
  5. export interface TabOption {
  6. label: string;
  7. value: string;
  8. }
  9. type PropsType = {
  10. options: TabOption[];
  11. currentTab: string;
  12. setCurrentTab: (x: string) => void;
  13. defaultTab?: string;
  14. addendum?: any;
  15. color?: string | null;
  16. suppressAnimation?: boolean;
  17. };
  18. type StateType = {};
  19. // Manages a tab selector and renders the associated view
  20. export default class TabRegion extends Component<PropsType, StateType> {
  21. defaultTab = () =>
  22. this.props.defaultTab
  23. ? this.props.defaultTab
  24. : this.props.options[0]
  25. ? this.props.options[0].value
  26. : "";
  27. componentDidUpdate(prevProps: PropsType) {
  28. let { options, currentTab } = this.props;
  29. if (prevProps.options !== options) {
  30. if (options.filter((x) => x.value === currentTab).length === 0) {
  31. this.props.setCurrentTab(this.defaultTab());
  32. }
  33. }
  34. }
  35. render() {
  36. return (
  37. <StyledTabRegion suppressAnimation={this.props.suppressAnimation}>
  38. {!this.props.currentTab ? (
  39. <Loading />
  40. ) : (
  41. <>
  42. <TabSelector
  43. options={this.props.options}
  44. color={this.props.color}
  45. currentTab={this.props.currentTab}
  46. setCurrentTab={(x: string) => this.props.setCurrentTab(x)}
  47. addendum={this.props.addendum}
  48. />
  49. <Gap />
  50. <TabContents>{this.props.children}</TabContents>
  51. </>
  52. )}
  53. </StyledTabRegion>
  54. );
  55. }
  56. }
  57. const TabContents = styled.div`
  58. height: calc(100% - 65px);
  59. `;
  60. const Gap = styled.div`
  61. width: 100%;
  62. background: none;
  63. height: 30px;
  64. `;
  65. const StyledTabRegion = styled.div<{ suppressAnimation: boolean }>`
  66. width: 100%;
  67. height: 100%;
  68. animation: ${(props) => (props.suppressAnimation ? "" : "fadeIn 0.25s 0s")};
  69. position: relative;
  70. overflow-y: auto;
  71. overflow: visible;
  72. `;