All files / src index.js

100% Statements 33/33
100% Branches 20/20
100% Functions 10/10
100% Lines 30/30
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                        6x   6x   6x   6x   6x   6x   6x   6x   6x       134x 134x 9x   134x 114x   134x 8x   134x     6x 82x     6x                         88x 88x 88x   134x   88x   88x 5x     83x       134x                                                 6x  
import React, { Children } from 'react'
import PropTypes from 'prop-types'
 
import {
  ThroughProvider,
  throughContainer,
  throughAgent,
  createAdvAgent,
  throughInterface,
} from 'react-through'
 
 
export const breadcrumbsThroughArea = 'breadcrumbs'
 
export const breadcrumbsBearingKey = 'to'
 
export const withBreadcrumbs = throughInterface(breadcrumbsThroughArea)
 
export const withBreadcrumbsItem = throughAgent(breadcrumbsThroughArea, breadcrumbsBearingKey)
 
export const withBreadcrumbsContainer = throughContainer(breadcrumbsThroughArea)
 
export const Dummy = () => null
 
export const Item = () => null
 
export const BreadcrumbsProvider = ThroughProvider
 
export const BreadcrumbsItem = createAdvAgent(breadcrumbsThroughArea, breadcrumbsBearingKey)
 
 
function prepareProps(props, rename, duplicate, remove) {
  const p = Object.assign({}, props)
  Object.keys(duplicate).forEach(k => {
    p[duplicate[k]] = p[k]
  })
  Object.keys(rename).forEach(k => {
    p[rename[k]] = p[k]; delete p[k]
  })
  Object.keys(remove).forEach(k => {
    delete p[k]
  })
  return p
}
 
const defaultCompare = (a, b) => (
  a[breadcrumbsBearingKey].length - b[breadcrumbsBearingKey].length
)
 
const Breadcrumbs_ = (props) => {
  const {
    container: Container = 'span',
    containerProps,
    hideIfEmpty = false,
    item: Item = 'a',
    finalItem: FinalItem = Item,
    finalProps = {},
    separator,
    duplicateProps: duplicate = {},
    removeProps: remove  = {},
    renameProps: rename = (Item === 'a' ? {to: 'href'} : {}),
    compare
  } = props
  const data = props[breadcrumbsThroughArea]
  const itemsValue = Object
    .keys(data)
    .map(k => data[k])
    .sort(compare || defaultCompare)
  const count = itemsValue.length
 
  if (hideIfEmpty && count === 0) {
    return null
  }
 
  return (
    <Container {...containerProps}>
 
      {itemsValue.map((itemValue, i) => {
        return i+1 < count ? (
 
          separator ? (
            <span key={i}>
              <Item {...prepareProps(itemValue, rename, duplicate, remove)} />
              {separator}
            </span>
          ) : (
            <Item key={i} {...prepareProps(itemValue, rename, duplicate, remove)} />
          )
 
        ) : (
 
          <FinalItem key={i}
            {...prepareProps(itemValue, rename, duplicate, remove)}
            {...finalProps}
          />
 
        )
      })}
 
    </Container>
  )
}
 
export const Breadcrumbs = withBreadcrumbsContainer(Breadcrumbs_)