All files / src/Breadcrumb Breadcrumb.tsx

94.12% Statements 48/51
77.27% Branches 17/22
100% Functions 11/11
97.92% Lines 47/48

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 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                                      1x   1x   16x   16x   16x               16x     16x     42x   42x 42x                   16x         1x   15x 15x                                             1x   45x   45x 45x   45x                 45x     45x 45x 45x                 45x         1x   45x 45x                                     1x   28x 28x   28x               28x         1x   28x 28x                                       1x   45x   45x 45x   45x               45x                   1x   45x   45x 45x 15x     45x                              
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
 
import { useClassName, createComponent, createElement, createHook } from '../utils';
import { Box, BoxProps } from '../Box';
import { Link, LinkProps } from '../Link';
import { List, ListItemProps } from '../List';
import { Text } from '../Text';
import { Navigation, NavigationProps } from '../Navigation';
 
import * as styles from './styles';
 
export type LocalBreadcrumbProps = {
  hasSeparator?: LocalBreadcrumbItemProps['hasSeparator'];
  separator?: LocalBreadcrumbItemProps['separator'];
};
export type BreadcrumbProps = NavigationProps & LocalBreadcrumbProps;
 
export type BreadcrumbContextOptions = { overrides?: BoxProps['overrides'] };
export const BreadcrumbContext = React.createContext<BreadcrumbContextOptions>({});
 
const useProps = createHook<BreadcrumbProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const { hasSeparator, overrides, separator, ...restProps } = props;
 
    const navigationProps = Navigation.useProps({ ...restProps, overrides });
 
    const className = useClassName({
      style: styles.Breadcrumb,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: navigationProps.className
    });
 
    const context = React.useMemo(() => ({ overrides: props.overrides }), [props.overrides]);
 
    const children = (
      <BreadcrumbContext.Provider value={context}>
        <List isOrdered orientation="horizontal" listStyleType="none" overrides={overrides}>
          {React.Children.map(props.children, (child, index) => {
            Iif (!React.isValidElement(child)) return child;
 
            const isLastChild = React.Children.count(props.children) - 1 === index;
            return React.cloneElement(child, {
              hasSeparator: typeof hasSeparator !== 'undefined' ? hasSeparator : !isLastChild,
              separator,
              ...child.props
            });
          })}
        </List>
      </BreadcrumbContext.Provider>
    );
 
    return { ...navigationProps, 'aria-label': 'Breadcrumb', className, children };
  },
  { themeKey: 'Breadcrumb' }
);
 
export const Breadcrumb = createComponent<BreadcrumbProps>(
  props => {
    const breadcrumbProps = useProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: breadcrumbProps
    });
  },
  {
    attach: { useProps },
    defaultProps: { use: 'nav' },
    themeKey: 'Breadcrumb'
  }
);
 
//////////////////////////////
 
export type LocalBreadcrumbItemProps = {
  hasSeparator?: boolean;
  isCurrent?: boolean;
  separator?: string | React.ReactElement<any>;
};
export type BreadcrumbItemProps = ListItemProps & LocalBreadcrumbItemProps;
 
const useBreadcrumbItemProps = createHook<BreadcrumbItemProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const { hasSeparator, isCurrent, overrides, separator, ...restProps } = props;
 
    const boxProps = List.Item.useProps({ ...restProps, overrides });
    const contextProps = React.useContext(BreadcrumbContext);
 
    const className = useClassName({
      style: styles.BreadcrumbItem,
      styleProps: { ...contextProps, ...props },
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    const children = (
      <React.Fragment>
        {React.Children.count(props.children) > 0
          ? React.Children.map(props.children, child => {
              Iif (!React.isValidElement(child)) return child;
              Eif (child.type === BreadcrumbLink) {
                return React.cloneElement(child, { isCurrent, ...child.props });
              }
              return child;
            })
          : props.children}
        {hasSeparator && <BreadcrumbSeparator>{separator}</BreadcrumbSeparator>}
      </React.Fragment>
    );
 
    return { ...boxProps, className, children };
  },
  { themeKey: 'Breadcrumb.Item' }
);
 
export const BreadcrumbItem = createComponent<BreadcrumbItemProps>(
  props => {
    const breadcrumbItemProps = useBreadcrumbItemProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: breadcrumbItemProps
    });
  },
  {
    attach: { useProps: useBreadcrumbItemProps },
    defaultProps: { use: 'li' },
    themeKey: 'Breadcrumb.Item'
  }
);
 
//////////////////////////////
 
export type LocalBreadcrumbSeparatorProps = {};
export type BreadcrumbSeparatorProps = BoxProps & LocalBreadcrumbSeparatorProps;
 
const useBreadcrumbSeparatorProps = createHook<BreadcrumbSeparatorProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const boxProps = Box.useProps(props);
    const contextProps = React.useContext(BreadcrumbContext);
 
    const className = useClassName({
      style: styles.BreadcrumbSeparator,
      styleProps: { ...contextProps, ...props },
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    return { ...boxProps, className, role: 'presentation', children: boxProps.children || '/' };
  },
  { themeKey: 'Breadcrumb.Separator' }
);
 
export const BreadcrumbSeparator = createComponent<BreadcrumbSeparatorProps>(
  props => {
    const breadcrumbItemProps = useBreadcrumbSeparatorProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: breadcrumbItemProps
    });
  },
  {
    attach: { useProps: useBreadcrumbSeparatorProps },
    themeKey: 'Breadcrumb.Separator'
  }
);
 
//////////////////////////////
 
export type LocalBreadcrumbLinkProps = {
  isCurrent?: boolean;
};
export type BreadcrumbLinkProps = LinkProps & LocalBreadcrumbLinkProps;
 
const useBreadcrumbLinkProps = createHook<BreadcrumbLinkProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const { isCurrent, ...restProps } = props;
 
    const linkProps = isCurrent ? Text.useProps(restProps) : Link.useProps(restProps);
    const contextProps = React.useContext(BreadcrumbContext);
 
    const className = useClassName({
      style: styles.BreadcrumbLink,
      styleProps: { ...contextProps, ...props },
      themeKey,
      themeKeyOverride,
      prevClassName: linkProps.className
    });
 
    return {
      ...linkProps,
      className,
      'aria-current': isCurrent ? 'page' : linkProps['aria-current'],
      href: isCurrent ? undefined : linkProps.href
    };
  },
  { themeKey: 'Breadcrumb.Link' }
);
 
export const BreadcrumbLink = createComponent<BreadcrumbLinkProps>(
  props => {
    const breadcrumbItemProps = useBreadcrumbLinkProps(props);
 
    let use = props.use;
    if (props.isCurrent) {
      use = Text;
    }
 
    return createElement({
      children: props.children,
      component: ReakitBox,
      use,
      htmlProps: breadcrumbItemProps
    });
  },
  {
    attach: { useProps: useBreadcrumbLinkProps },
    defaultProps: {
      use: 'a'
    },
    themeKey: 'Breadcrumb.Link'
  }
);