SidebarNav.jsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import {
  5. GlobalNav,
  6. GlobalItem,
  7. } from '@atlaskit/navigation-next';
  8. import { createSubscribedElement } from '../UnstatedUtils';
  9. import AppContainer from '../../services/AppContainer';
  10. class SidebarNav extends React.Component {
  11. static propTypes = {
  12. currentContentsId: PropTypes.string,
  13. onItemSelected: PropTypes.func,
  14. };
  15. state = {
  16. };
  17. itemSelectedHandler = (contentsId) => {
  18. const { onItemSelected } = this.props;
  19. if (onItemSelected != null) {
  20. onItemSelected(contentsId);
  21. }
  22. }
  23. generatePrimaryItemObj(id, label, icon) {
  24. const isSelected = this.props.currentContentsId === id;
  25. return {
  26. id,
  27. component: ({ className }) => (
  28. <div className={`${className} grw-global-item-container ${isSelected ? 'active' : ''}`}>
  29. <GlobalItem
  30. icon={icon}
  31. label={label}
  32. isSelected={isSelected}
  33. onClick={() => this.itemSelectedHandler(id)}
  34. />
  35. </div>
  36. ),
  37. };
  38. }
  39. generateSecondaryItemObj(id, label, icon, href) {
  40. return {
  41. id,
  42. component: ({ className }) => (
  43. <div className={`${className} grw-global-item-container d-block d-md-none`}>
  44. <a href={href}>
  45. <GlobalItem
  46. icon={icon}
  47. label={label}
  48. />
  49. </a>
  50. </div>
  51. ),
  52. };
  53. }
  54. generateIconFactory(classNames) {
  55. return () => <i className={classNames}></i>;
  56. }
  57. render() {
  58. return (
  59. <GlobalNav
  60. primaryItems={[
  61. this.generatePrimaryItemObj('custom', 'Custom Sidebar', this.generateIconFactory('fa fa-code')),
  62. this.generatePrimaryItemObj('history', 'History', this.generateIconFactory('icon-clock')),
  63. ]}
  64. secondaryItems={[
  65. this.generateSecondaryItemObj('admin', 'Admin', this.generateIconFactory('icon-settings'), '/admin'),
  66. this.generateSecondaryItemObj('help', 'Help', this.generateIconFactory('icon-question'), 'https://docs.growi.org'),
  67. ]}
  68. />
  69. );
  70. }
  71. }
  72. /**
  73. * Wrapper component for using unstated
  74. */
  75. const SidebarNavWrapper = (props) => {
  76. return createSubscribedElement(SidebarNav, props, [AppContainer]);
  77. };
  78. export default withTranslation()(SidebarNavWrapper);