All files / src/SideNav SideNavItem.tsx

83.33% Statements 10/12
87.5% Branches 7/8
50% Functions 2/4
83.33% Lines 10/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                    1x   117x 117x   117x 117x   117x                       117x                               1x   117x 117x                                  
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
import buildClassNames from 'classnames';
 
import { bindFns, useClassName, createComponent, createElement, createHook } from '../utils';
import { ListItem, ListItemProps } from '../List';
 
import { SideNavContext } from './SideNav';
import { SideNavLevelContext } from './SideNavLevel';
import * as styles from './styles';
 
export type LocalSideNavItemProps = {
  href?: string;
  isActive?: boolean;
  navId?: string;
};
export type SideNavItemProps = ListItemProps & LocalSideNavItemProps;
 
const useProps = createHook<SideNavItemProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const { children, href, isActive, navId, onClick, overrides, ...restProps } = props;
    const listItemProps = ListItem.useProps(restProps);
 
    const { onChangeSelectedId, selectedId, overrides: sideNavOverrides } = React.useContext(SideNavContext);
    const { level } = React.useContext(SideNavLevelContext);
 
    const className = useClassName({
      style: styles.SideNavItem,
      styleProps: {
        ...props,
        isActive: typeof isActive === 'boolean' ? isActive : selectedId === navId,
        level,
        overrides: { ...sideNavOverrides, ...overrides }
      },
      themeKey,
      themeKeyOverride
    });
 
    return {
      ...listItemProps,
      className: buildClassNames(listItemProps.className, href ? undefined : className),
      onClick: href ? undefined : bindFns(onClick, () => onChangeSelectedId(navId)),
      children: href ? (
        <a className={className} href={href} onClick={bindFns(onClick, () => onChangeSelectedId(navId))}>
          {children}
        </a>
      ) : (
        children
      )
    };
  },
  { themeKey: 'SideNav.Item' }
);
 
export const SideNavItem = createComponent<SideNavItemProps>(
  props => {
    const sideNavItemProps = useProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: sideNavItemProps
    });
  },
  {
    attach: {
      useProps
    },
    defaultProps: {
      use: 'li'
    },
    themeKey: 'SideNav.Item'
  }
);