All files / molecule/accordion/src AccordionItemHeader.js

100% Statements 17/17
96.55% Branches 28/29
100% Functions 3/3
100% Lines 17/17

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                                        1x                                                         29x 29x 3x 3x 3x 6x       29x 29x 29x       29x 29x 29x 29x 29x                                                                                                               1x   1x                                                            
import {forwardRef} from 'react'
import {isFragment} from 'react-is'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import Injector from '@s-ui/react-primitive-injector'
import Poly from '@s-ui/react-primitive-polymorphic-element'
 
import {useAccordionContext} from './context/index.js'
import AccordionItemHeaderChildrenDefault from './AccordionItemHeaderChildrenDefault.js'
import {
  BASE_CLASS_ELEMENT,
  BASE_CLASS_ITEM_HEADER,
  getBehavior,
  getIcon,
  HEADER_ICON_POSITION,
  HEADER_LABEL_WRAPS
} from './settings.js'
 
const AccordionItemHeader = forwardRef(
  (
    {
      as: As = 'h1',
      id,
      panelId,
      icon: iconProp,
      iconPosition: iconPositionProp,
      children = <AccordionItemHeaderChildrenDefault />,
      animationDuration: animationDurationProp,
      value,
      label,
      labelWrap: labelWrapProp,
      level,
      disabled,
      onClick
    },
    forwardedRef
  ) => {
    const {
      values,
      onChange,
      behavior,
      setValues,
      headerIconExpanded: iconExpandedContext,
      headerIconCollapsed: iconCollapsedContext,
      headerIconPosition: iconPositionContext,
      headerLabelWrap: labelWrapContext,
      animationDuration: animationDurationContext
    } = useAccordionContext({value})
    const handleClick = event => {
      const response = getBehavior(behavior)({value, values})
      setValues(response.values)
      ;[onChange, onClick].forEach(onHandler => {
        typeof onHandler === 'function' && onHandler(event, response)
      })
    }
 
    const isFragmentProp = isFragment(<As />)
    const isExpanded = values.includes(value)
    const icon = getIcon(
      {icon: iconProp, isExpanded},
      {iconExpanded: iconExpandedContext, iconCollapsed: iconCollapsedContext}
    )
    const iconPosition = iconPositionProp || iconPositionContext
    const animationDuration = animationDurationProp || animationDurationContext
    const labelWrap = labelWrapProp || labelWrapContext
    const isHeadingElement = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(As)
    return (
      <Poly
        as={As}
        {...{
          ...(!isFragmentProp && {
            className: cx(BASE_CLASS_ITEM_HEADER, BASE_CLASS_ELEMENT),
            ref: forwardedRef
          }),
          ...(!isHeadingElement && !isFragmentProp && {role: 'heading'}),
          ...(!isHeadingElement && level && !isFragmentProp && {'aria-level': level}),
          ...(!isFragmentProp && {'data-expanded': isExpanded}),
          ...(!isFragmentProp && {
            style: {
              transition: `border-radius 0s linear ${isExpanded ? 0 : animationDuration}ms`
            }
          })
        }}
      >
        <button
          type="button"
          id={id}
          className={cx(
            `${BASE_CLASS_ITEM_HEADER}Button`,
            `${BASE_CLASS_ITEM_HEADER}Button--icon-position-${iconPosition}`
          )}
          aria-pressed={isExpanded}
          aria-controls={panelId}
          {...{
            ...(disabled && {'aria-disabled': disabled, disabled}),
            ...(label && {'aria-label': label})
          }}
          onClick={handleClick}
        >
          <Injector
            headerIconExpanded={iconExpandedContext}
            headerIconCollapsed={iconCollapsedContext}
            headerIconPosition={iconPositionContext}
            headerLabelWrap={labelWrapContext}
            disabled={disabled}
            animationDuration={animationDuration}
            icon={icon}
            iconPosition={iconPosition}
            values={values}
            value={value}
            isExpanded={values.includes(value)}
            label={label}
            labelWrap={labelWrap}
          >
            {children}
          </Injector>
        </button>
      </Poly>
    )
  }
)
 
AccordionItemHeader.displayName = 'AccordionItemHeader'
 
AccordionItemHeader.propTypes = {
  /** The elementType of the wrapper **/
  as: PropTypes.elementType,
  /** The animation duration in ms **/
  animationDuration: PropTypes.number,
  /** child element **/
  children: PropTypes.node,
  /** element enabled or not **/
  disabled: PropTypes.bool,
  /** unique identifier **/
  id: PropTypes.string,
  /** unique identifier of the controlled panel **/
  panelId: PropTypes.string,
  /** The header Icn element **/
  icon: PropTypes.node,
  /** where the icon is header positioned */
  iconPosition: PropTypes.oneOf(Object.values(HEADER_ICON_POSITION)),
  /** appropriate for the information architecture of the page **/
  label: PropTypes.string.isRequired,
  /** Defines the wrap behavior of the label */
  labelWrap: PropTypes.oneOf(Object.values(HEADER_LABEL_WRAPS)),
  /** the unique value of the element **/
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
  /** the heading level **/
  level: PropTypes.oneOf(['1', '2', '3', '4', '5', '6', 1, 2, 3, 4, 5, 6]),
  /** header clicking handler **/
  onClick: PropTypes.func
}
 
export default AccordionItemHeader