All files / molecule/breadcrumb/src index.js

62.5% Statements 10/16
43.47% Branches 10/23
50% Functions 2/4
62.5% Lines 10/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 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                    1x                                         6x 6x                   6x 6x   6x                           12x     12x                                       1x   1x                                                                                                  
import {forwardRef, Fragment, isValidElement} from 'react'
import PropTypes from 'prop-types'
 
import useControlledState from '@s-ui/react-hooks/lib/useControlledState/index.js'
import ChevronRight from '@s-ui/react-icons/lib/Chevronright'
 
import PrimitiveInjector from '@s-ui/react-primitive-injector'
 
import {BASE_CLASS, breadcrumbClassName, isFunction} from './settings.js'
 
const Breadcrumb = forwardRef(
  (
    {
      items,
      icon: Icon = <ChevronRight svgClass={`${BASE_CLASS}-icon`} />,
      linkFactory: Link = ({to, href, className, children, ...props}) => (
        <a href={to || href} className={className} {...props}>
          {children}
        </a>
      ),
      isScrollable = false,
      isExpanded,
      defaultIsExpanded = false,
      onExpand,
      onCollapse,
      onClick,
      className,
      ...props
    },
    forwardedRef
  ) => {
    const [isExpandedState, setIsExpandedState] = useControlledState(isExpanded, defaultIsExpanded)
    const handleClick = event => {
      setIsExpandedState(!isExpandedState)
      isFunction(onClick) && onClick(event, {value: !isExpandedState})
      if (isExpandedState) {
        isFunction(onCollapse) && onCollapse(event, {value: false})
      } else {
        isFunction(onExpand) && onExpand(event, {value: true})
      }
    }
 
    const numItems = items.length - 1
    const icon = isValidElement(Icon) ? Icon : <Icon />
 
    return (
      <nav {...props} ref={forwardedRef}>
        <div
          className={breadcrumbClassName({
            isExpanded: isExpandedState,
            isScrollable,
            className
          })}
        >
          <button onClick={handleClick} className={`${BASE_CLASS}-btn`}>
            ...
          </button>
          <ul className={`${BASE_CLASS}-list`}>
            {items.map(({url, label, 'aria-current': ariaCurrent, ...rest}, index) => {
              const [Element, elementProps] = url
                ? [Link, {to: url, href: url, className: `${BASE_CLASS}-link`, children: label}]
                : [PrimitiveInjector, {children: typeof label === 'string' ? <span>{label}</span> : label}]
              return (
                <Fragment key={index}>
                  {index !== 0 && index <= numItems && (
                    <li className={`${BASE_CLASS}-icon`} role="presentation" aria-hidden="true">
                      {icon}
                    </li>
                  )}
                  <li className={`${BASE_CLASS}-listItem`} aria-current={ariaCurrent}>
                    <Element {...{...elementProps, ...rest}} />
                  </li>
                </Fragment>
              )
            })}
          </ul>
        </div>
      </nav>
    )
  }
)
 
Breadcrumb.displayName = 'Breadcrumb'
 
Breadcrumb.propTypes = {
  /**
   * Additional class names to extend the component
   */
  className: PropTypes.string,
  /**
   * List of link objects
   */
  items: PropTypes.arrayOf(
    PropTypes.shape({
      /**
       * link text
       */
      label: PropTypes.string.isRequired,
      /**
       * URL for the link
       */
      url: PropTypes.string
    })
  ).isRequired,
  /**
   * Icon node to be used as a separator between items.
   */
  icon: PropTypes.node,
  /**
   * Function for creating links so it allows to customize it
   */
  linkFactory: PropTypes.func,
  /**
   * Boolean that allows us to show the items with a horizontal scroll
   */
  isScrollable: PropTypes.bool,
  /**
   * The controlled value of the expanded l&f of the component
   */
  isExpanded: PropTypes.bool,
  /**
   * The initial value of the expanded l&f of the component
   */
  defaultIsExpanded: PropTypes.bool,
  /** expand handler **/
  onExpand: PropTypes.func,
  /** collapse handler **/
  onCollapse: PropTypes.func,
  /** click handler (expand or collapse) **/
  onClick: PropTypes.func
}
 
export default Breadcrumb