All files / breadcrumbs breadcrumbs.js

87.5% Statements 14/16
100% Branches 9/9
50% Functions 2/4
87.5% Lines 14/16

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                                        2x 2x 2x   2x 2x 2x         2x           2x   2x 6x   6x 4x               2x                                         1x          
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
 
// @flow
 
import React, {Children} from 'react';
 
import {LocaleContext} from '../locale/index.js';
import {ChevronRight} from '../icon/index.js';
import type {BreadcrumbsPropsT} from './types.js';
import type {BreadcrumbLocaleT} from './locale.js';
import {StyledRoot, StyledSeparator} from './styled-components.js';
import {getOverrides, mergeOverrides} from '../helpers/overrides.js';
 
type LocaleT = {|locale?: BreadcrumbLocaleT|};
export function BreadcrumbsRoot(props: {|...BreadcrumbsPropsT, ...LocaleT|}) {
  const {overrides = {}} = props;
  const numChildren = Children.count(props.children);
  const childrenWithSeparators = [];
 
  const [Root, baseRootProps] = getOverrides(overrides.Root, StyledRoot);
  const [Icon, baseIconProps] = getOverrides(overrides.Icon, ChevronRight);
  const [Separator, baseSeparatorProps] = getOverrides(
    overrides.Separator,
    StyledSeparator,
  );
 
  const iconOverrides = mergeOverrides(
    {Svg: {style: {verticalAlign: 'text-bottom'}}},
    // $FlowFixMe
    baseIconProps && baseIconProps.overrides,
  );
  // $FlowFixMe
  baseIconProps.overrides = iconOverrides;
 
  Children.forEach(props.children, (child, index) => {
    childrenWithSeparators.push(child);
 
    if (index !== numChildren - 1) {
      childrenWithSeparators.push(
        <Separator {...baseSeparatorProps} key={`separator-${index}`}>
          <Icon {...baseIconProps} />
        </Separator>,
      );
    }
  });
 
  return (
    <Root
      aria-label={
        props.ariaLabel || (props.locale ? props.locale.ariaLabel : '')
      }
      data-baseweb="breadcrumbs"
      {...baseRootProps}
    >
      {childrenWithSeparators}
    </Root>
  );
}
 
function Breadcrumbs(props: BreadcrumbsPropsT) {
  return (
    <LocaleContext.Consumer>
      {locale => <BreadcrumbsRoot {...props} locale={locale.breadcrumbs} />}
    </LocaleContext.Consumer>
  );
}
 
Breadcrumbs.defaultProps = {
  overrides: {},
};
 
export default Breadcrumbs;