You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

132 rivejä
3.4 KiB

  1. import React from "react";
  2. // nodejs library that concatenates classes
  3. import classNames from "classnames";
  4. // nodejs library to set properties for components
  5. import PropTypes from "prop-types";
  6. import SwipeableViews from "react-swipeable-views";
  7. // @material-ui/core components
  8. import { makeStyles } from "@material-ui/core/styles";
  9. import Tabs from "@material-ui/core/Tabs";
  10. import Tab from "@material-ui/core/Tab";
  11. // core components
  12. import GridContainer from "components/Grid/GridContainer.js";
  13. import GridItem from "components/Grid/GridItem.js";
  14. import styles from "assets/jss/nextjs-material-kit/components/navPillsStyle.js";
  15. const useStyles = makeStyles(styles);
  16. export default function NavPills(props) {
  17. const [active, setActive] = React.useState(props.active);
  18. const handleChange = (event, active) => {
  19. setActive(active);
  20. };
  21. const handleChangeIndex = index => {
  22. setActive(index);
  23. };
  24. const classes = useStyles();
  25. const { tabs, direction, color, horizontal, alignCenter } = props;
  26. const flexContainerClasses = classNames({
  27. [classes.flexContainer]: true,
  28. [classes.horizontalDisplay]: horizontal !== undefined
  29. });
  30. const tabButtons = (
  31. <Tabs
  32. classes={{
  33. root: classes.root,
  34. fixed: classes.fixed,
  35. flexContainer: flexContainerClasses,
  36. indicator: classes.displayNone
  37. }}
  38. value={active}
  39. onChange={handleChange}
  40. centered={alignCenter}
  41. >
  42. {tabs.map((prop, key) => {
  43. var icon = {};
  44. if (prop.tabIcon !== undefined) {
  45. icon["icon"] = <prop.tabIcon className={classes.tabIcon} />;
  46. }
  47. const pillsClasses = classNames({
  48. [classes.pills]: true,
  49. [classes.horizontalPills]: horizontal !== undefined,
  50. [classes.pillsWithIcons]: prop.tabIcon !== undefined
  51. });
  52. return (
  53. <Tab
  54. label={prop.tabButton}
  55. key={key}
  56. {...icon}
  57. classes={{
  58. root: pillsClasses,
  59. selected: classes[color],
  60. wrapper: classes.tabWrapper
  61. }}
  62. />
  63. );
  64. })}
  65. </Tabs>
  66. );
  67. const tabContent = (
  68. <div className={classes.contentWrapper}>
  69. <SwipeableViews
  70. axis={direction === "rtl" ? "x-reverse" : "x"}
  71. index={active}
  72. onChangeIndex={handleChangeIndex}
  73. >
  74. {tabs.map((prop, key) => {
  75. return (
  76. <div className={classes.tabContent} key={key}>
  77. {prop.tabContent}
  78. </div>
  79. );
  80. })}
  81. </SwipeableViews>
  82. </div>
  83. );
  84. return horizontal !== undefined ? (
  85. <GridContainer>
  86. <GridItem {...horizontal.tabsGrid}>{tabButtons}</GridItem>
  87. <GridItem {...horizontal.contentGrid}>{tabContent}</GridItem>
  88. </GridContainer>
  89. ) : (
  90. <div>
  91. {tabButtons}
  92. {tabContent}
  93. </div>
  94. );
  95. }
  96. NavPills.defaultProps = {
  97. active: 0,
  98. color: "primary"
  99. };
  100. NavPills.propTypes = {
  101. // index of the default active pill
  102. active: PropTypes.number,
  103. tabs: PropTypes.arrayOf(
  104. PropTypes.shape({
  105. tabButton: PropTypes.string,
  106. tabIcon: PropTypes.object,
  107. tabContent: PropTypes.node
  108. })
  109. ).isRequired,
  110. color: PropTypes.oneOf([
  111. "primary",
  112. "warning",
  113. "danger",
  114. "success",
  115. "info",
  116. "rose"
  117. ]),
  118. direction: PropTypes.string,
  119. horizontal: PropTypes.shape({
  120. tabsGrid: PropTypes.object,
  121. contentGrid: PropTypes.object
  122. }),
  123. alignCenter: PropTypes.bool
  124. };