All files / molecule/stepper/src/Step Step.js

84.61% Statements 11/13
73.91% Branches 17/23
50% Functions 1/2
84.61% Lines 11/13

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                            1x                                                             58x 58x 58x 58x 58x 58x 58x       58x                                                                                                         1x   1x                                                        
import {forwardRef, useRef} from 'react'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import useMergeRefs from '@s-ui/react-hooks/lib/useMergeRefs/index.js'
import Injector from '@s-ui/react-primitive-injector'
import Poly from '@s-ui/react-primitive-polymorphic-element'
 
import {useStepsContext} from '../context/index.js'
import {naturalNumber} from '../prop-types.js'
import DefaultStep from './DefaultStep.js'
import {BASE_CLASS_STEP} from './settings.js'
 
const Step = forwardRef(
  (
    {
      as,
      children = <DefaultStep />,
      label,
      current,
      visited,
      step,
      icon,
      visitedIcon,
      showLabel,
      currentIcon,
      hasConnector: hasConnectorProp,
      onClick
    },
    forwardedRef
  ) => {
    const {
      as: asContext,
      alignment,
      design,
      steps,
      useContextRef,
      useContextUnRef,
      justifyContent,
      icon: iconContext,
      visitedIcon: visitedIconContext,
      currentIcon: currentIconContext,
      hasConnector: hasConnectorContext,
      onChange
    } = useStepsContext()
    const innerRef = useRef()
    const ref = useMergeRefs(forwardedRef, innerRef, useContextRef)
    const As = as || asContext || 'li'
    const hasConnector = hasConnectorProp === undefined ? hasConnectorContext : hasConnectorProp
    useContextUnRef(innerRef)
    const onClickHandler = event => {
      typeof onClick === 'function' && onClick(event, {step})
      typeof onChange === 'function' && onChange(event, {step})
    }
    return (
      <>
        <Poly
          as={As}
          ref={ref}
          role="listitem"
          data-step={`${step}`}
          {...(current && {'aria-current': 'step'})}
          className={cx(
            BASE_CLASS_STEP,
            [
              `${BASE_CLASS_STEP}--design-${design}`,
              `${BASE_CLASS_STEP}--alignment-${alignment}`,
              `${BASE_CLASS_STEP}--justifyContent-${justifyContent}`
            ],
            {
              [`${BASE_CLASS_STEP}--current`]: current,
              [`${BASE_CLASS_STEP}--visited`]: visited
            }
          )}
          onClick={onClickHandler}
        >
          <Injector
            alignment={alignment}
            design={design}
            label={label}
            step={step}
            steps={steps}
            showLabel={showLabel}
            current={current}
            visited={visited}
            icon={icon || iconContext}
            visitedIcon={visitedIcon || visitedIconContext}
            currentIcon={currentIcon || currentIconContext}
          >
            {children}
          </Injector>
        </Poly>
        {hasConnector && steps !== step - 1 && (
          <Poly
            role="separator"
            {...(steps === step && {'aria-hidden': true})}
            as={As}
            className={cx(`${BASE_CLASS_STEP}Connector`)}
          >
            <div className={cx(`${BASE_CLASS_STEP}ConnectorLine`)} />
          </Poly>
        )}
      </>
    )
  }
)
 
Step.displayName = 'Step'
 
Step.propTypes = {
  /** element tag **/
  as: PropTypes.element,
  /** inner content **/
  children: PropTypes.node,
  /** the number of the step in the list **/
  step: naturalNumber,
  /** show label or not */
  showLabel: PropTypes.bool,
  /** the text label of the step **/
  label: PropTypes.string,
  /** stepper points to that step or not **/
  current: PropTypes.bool,
  /** stepper step is higher to that step or not **/
  visited: PropTypes.bool,
  /** react-node icon passed to all inner steps **/
  icon: PropTypes.node,
  /** react-node icon passed to all inner visited steps **/
  visitedIcon: PropTypes.node,
  /** react-node icon passed to inner current steps **/
  currentIcon: PropTypes.node,
  /** has or not a connector element between steps **/
  hasConnector: PropTypes.bool,
  /** change handler to get the step fired **/
  onClick: PropTypes.func
}
 
export default Step