All files / lib/SideNav index.js

84.62% Statements 11/13
67.86% Branches 19/28
66.67% Functions 4/6
84.62% Lines 11/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                1x           1x                   9x 9x 9x   9x             2x                                                               9x                         89x                                 89x                 89x                                                                                                                                                                            
/**
 * @category layout
 * @component side-nav
 * @variations collab-ui-react
 */
 
import React from 'react';
import PropTypes from 'prop-types';
import { Icon, ListItemSection } from '@collab-ui/react';
 
class SideNav extends React.Component {
  state = {
    expanded: this.props.expanded
  };
 
  componentDidUpdate(prevProps) {
    this.props.expanded !== prevProps.expanded
    && this.state.expanded !== this.props.expanded
    && this.setExpanded();
  }
 
  handleNavToggle = () => {
    this.setState({
      expanded: !this.state.expanded
    });
  }
 
  setExpanded = () => {
    this.setState({ expanded: this.props.expanded });
  }
 
  render() {
    const { children, navSectionNode, navSectionTitle, topMenu, expandable, className } = this.props;
    const { expanded } = this.state;
    const topMenuTitle = topMenu ? 'top' : 'sub';
 
    const navSectionTitleText= navSectionTitle
      ? (
        <h3
          className={`cui-side-nav__title--${topMenuTitle}`}
        >
          <button
            className='cui-side-nav__button'
            onClick={() => {expandable ? this.handleNavToggle() : false;}}>
            {navSectionTitle}
            {
              expandable &&
                (
                  expanded
                  ? <Icon name='arrow-up_16' />
                  : <Icon name='arrow-down_16' />
                )
            }
          </button>
        </h3>
      )
      : navSectionNode && React.cloneElement(
        navSectionNode,
        {
          ...expandable && { onClick: () => this.handleNavToggle() }
        },
        [
          ...navSectionNode.props.children,
          ...expandable &&
            <ListItemSection position='right' key='side-nav--expand-section'>
              {
                expanded
                ? <Icon name='arrow-up_12' />
                : <Icon name='arrow-down_12' />
              }
            </ListItemSection>
          ]
      );
 
 
    return (
      <div className={
        'cui-side-nav' +
        ` cui-side-nav--${(!expandable || expanded) ? 'expanded' : 'collapse'}` +
        `${className && ` ${className}` || ''}`
      }>
        {navSectionTitleText}
        {children}
      </div>
    );
  }
}
 
SideNav.propTypes = {
  /** @prop Children nodes to Render inside side navigation | null */
  children: PropTypes.node,
  /** @prop Optional CSS class string | '' */
  className: PropTypes.string,
  /** @prop Set to make the navigation expandable | false */
  expandable: PropTypes.bool,
  /** @prop Set navigation expanded or collapsed | false */
  expanded: PropTypes.bool,
  /** @prop Node to replace NavSection | null */
  navSectionNode: PropTypes.node,
  /** @prop Title for the side navigation | '' */
  navSectionTitle: PropTypes.string,
  /** @prop Sets side navigation as the top level | false */
  topMenu: PropTypes.bool,
};
 
SideNav.defaultProps = {
  children: null,
  expandable: false,
  expanded: false,
  navSectionNode: null,
  navSectionTitle: '',
  topMenu: false,
};
 
SideNav.displayName = 'SideNav';
 
export default SideNav;
 
/**
* @component side-nav
* @section default
* @react
import { List, ListItem, SideNav } from '@collab-ui/react';
import { NavLink } from 'react-router-dom'
 
export default class SideNavDefault extends React.PureComponent {
  render() {
    const anchorNode = <NavLink to='/containers/list-item' />;
 
    return (
      <SideNav navSectionTitle='OVERVIEW' topMenu>
        <List>
          <ListItem label='Platform Introduction' customAnchorNode={anchorNode} />
          <ListItem label='Bots' customAnchorNode={anchorNode} />
          <ListItem label='Widgets' customAnchorNode={anchorNode} />
          <ListItem label='Integration' customAnchorNode={anchorNode} />
          <ListItem label='Guest Issuer' customAnchorNode={anchorNode} />
        </List>
      </SideNav>
    );
  }
}
**/
 
/**
* @component side-nav
* @section expand
* @react
import { List, ListItem, SideNav } from '@collab-ui/react';
import { NavLink } from 'react-router-dom'
 
export default class SideNavExpand extends React.PureComponent {
  render() {
    const anchorNode = <NavLink to='/containers/list-item' />;
 
    return (
      <SideNav navSectionTitle='Guides' expandable expanded={false}>
        <List>
          <ListItem label='Authentication' customAnchorNode={anchorNode} />
          <ListItem label='Admin API' customAnchorNode={anchorNode} />
          <ListItem label='Webhooks' customAnchorNode={anchorNode} />
          <ListItem label='Compliance' customAnchorNode={anchorNode} />
        </List>
      </SideNav>
    );
  }
}
**/
 
/**
* @component side-nav
* @section nested
* @react
import { List, ListItem, SideNav } from '@collab-ui/react';
import { NavLink } from 'react-router-dom'
 
export default class SideNavNested extends React.PureComponent {
  render() {
    const anchorNode = <NavLink to='/containers/list-item' />;
 
    return (
      <SideNav navSectionTitle='Reference' expandable expanded={false}>
        <List>
          <ListItem label='Admins' customAnchorNode={anchorNode} />
          <ListItem label='Devices' customAnchorNode={anchorNode} />
          <ListItem label='Licenses' customAnchorNode={anchorNode} />
          <SideNav navSectionTitle='Messages' className='cui-side-nav__reference' expandable expanded={false}>
            <List>
              <ListItem label='List Messages' customAnchorNode={anchorNode} />
              <ListItem label='Create a Messages' customAnchorNode={anchorNode} />
              <ListItem label='Get Message Details' customAnchorNode={anchorNode} />
              <ListItem label='Delete a Message' customAnchorNode={anchorNode} />
            </List>
          </SideNav>
        </List>
      </SideNav>
    );
  }
}
**/