All files / molecule/dropdownOption/src index.js

68.42% Statements 13/19
52.63% Branches 20/38
40% Functions 2/5
68.42% Lines 13/19

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                          1x                                             5x 5x 5x 5x             5x               5x               5x                                           5x       5x                       1x                                                           1x 1x                                                                              
import {createRef, forwardRef} from 'react'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import {highlightText} from '@s-ui/js/lib/string'
import AtomCheckbox from '@s-ui/react-atom-checkbox'
import useControlledState from '@s-ui/react-hooks/lib/useControlledState/index.js'
import useMergeRefs from '@s-ui/react-hooks/lib/useMergeRefs/index.js'
 
import handlersFactory from './handlersFactory/index.js'
import {
  BASE_CLASS,
  CLASS_CHECKBOX,
  CLASS_DESCRIPTION,
  CLASS_DISABLED,
  CLASS_HIGHLIGHTED,
  CLASS_HIGHLIGHTED_MARK,
  CLASS_LEFT_ADDON,
  CLASS_TEXT,
  CLASS_WITH_DESCRIPTION,
  CLASS_WITH_DESCRIPTION_AND_ADDON,
  MODIFIER_LINE_WRAP,
  MODIFIER_NO_WRAP,
  MODIFIER_THREE_LINES,
  MODIFIER_TWO_LINES,
  TEXT_WRAP_STYLES
} from './config.js'
 
const MoleculeDropdownOption = forwardRef(
  (
    {
      checkbox,
      checkboxProps,
      children,
      disabled = false,
      highlightQuery,
      highlightValue,
      innerRef,
      leftAddon,
      selectKey = 'Enter',
      onSelect,
      selected,
      defaultSelected = false,
      textWrap,
      value,
      description,
      withTwoLinesText,
      ...props
    },
    forwardedRef
  ) => {
    const hasAddonAndDescription = description && !!leftAddon
    const ref = useMergeRefs(innerRef || createRef(), forwardedRef)
    const [innerSelected, setInnerSelected] = useControlledState(selected, defaultSelected)
    const className = cx(BASE_CLASS, {
      [CLASS_CHECKBOX]: checkbox,
      [CLASS_DISABLED]: disabled,
      [CLASS_WITH_DESCRIPTION]: description,
      [CLASS_WITH_DESCRIPTION_AND_ADDON]: hasAddonAndDescription,
      'is-selected': innerSelected
    })
    const innerClassName = cx([
      CLASS_TEXT,
      (withTwoLinesText || textWrap === TEXT_WRAP_STYLES.TWO_LINES) && `${CLASS_TEXT}--${MODIFIER_TWO_LINES}`,
      textWrap === TEXT_WRAP_STYLES.THREE_LINES && `${CLASS_TEXT}--${MODIFIER_THREE_LINES}`,
      textWrap === TEXT_WRAP_STYLES.LINE_WRAP && `${CLASS_TEXT}--${MODIFIER_LINE_WRAP}`,
      ((!withTwoLinesText && !textWrap) || textWrap === TEXT_WRAP_STYLES.NO_WRAP) &&
        `${CLASS_TEXT}--${MODIFIER_NO_WRAP}`
    ])
    const {handleClick, handleKeyDown, handleFocus} = handlersFactory({
      disabled,
      selectKey,
      onSelect,
      selected: innerSelected,
      setInnerSelected,
      value
    })
    const renderHighlightOption = option => {
      if (typeof option !== 'string') {
        return (
          <span onFocus={handleInnerFocus} className={innerClassName}>
            {option}
          </span>
        )
      }
      const mark = highlightText({
        value: option,
        query: highlightQuery,
        startTag: `<mark class="${cx(CLASS_HIGHLIGHTED_MARK, CLASS_HIGHLIGHTED)}">`,
        endTag: '</mark>'
      })
 
      return (
        <>
          <span onFocus={handleInnerFocus} dangerouslySetInnerHTML={{__html: mark}} className={innerClassName} />
          {highlightValue ? children : null}
        </>
      )
    }
    const handleInnerFocus = ev => {
      innerRef.current && innerRef.current.focus()
    }
 
    return (
      <li
        ref={ref}
        tabIndex="0"
        className={className}
        onClick={handleClick}
        onKeyDown={handleKeyDown}
        onFocus={handleFocus}
        role="option"
        data-value={value}
        aria-label={value}
        aria-checked={innerSelected}
        {...Object.fromEntries(Object.entries(props).filter(([key]) => !['className', 'style'].includes(key)))}
      >
        {checkbox && (
          <AtomCheckbox
            checked={innerSelected}
            disabled={disabled}
            onChange={ev => {
              typeof onSelect === 'function' && onSelect(ev, {value})
            }}
            onFocus={handleInnerFocus}
            {...checkboxProps}
          />
        )}
        {highlightQuery ? (
          renderHighlightOption(highlightValue || children)
        ) : (
          <>
            {leftAddon ? <span className={CLASS_LEFT_ADDON}>{leftAddon}</span> : null}
            <span onFocus={handleInnerFocus} className={innerClassName}>
              {children}
              {hasAddonAndDescription && <span className={CLASS_DESCRIPTION}>{description}</span>}
            </span>
          </>
        )}
        {!hasAddonAndDescription && <span className={CLASS_DESCRIPTION}>{description}</span>}
      </li>
    )
  }
)
 
MoleculeDropdownOption.displayName = 'MoleculeDropdownOption'
MoleculeDropdownOption.propTypes = {
  /** option value */
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object]).isRequired,
  /** Content to be included in the option */
  children: PropTypes.node,
  /** Contains checkbox */
  checkbox: PropTypes.bool,
  /** Additional props to set up the checkbox */
  checkboxProps: PropTypes.object,
  /** Is disabled */
  disabled: PropTypes.bool,
  /** Custom element set at left side of an option */
  leftAddon: PropTypes.node,
  /** onSelect callback (ev, {value}) */
  onSelect: PropTypes.func,
  /** Selected html controlled property */
  selected: PropTypes.bool,
  /** Initial selected **/
  defaultSelected: PropTypes.bool,
  /** Text to be highlighted in the option text if found */
  highlightQuery: PropTypes.string,
  /** Text to be display if used with highlight query with custom content */
  highlightValue: PropTypes.string,
  /* key to provoke the onClick callback. Valid any value defined here → https://www.w3.org/TR/uievents-key/#named-key-attribute-values */
  selectKey: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
  /** Custom ref handler that will be assigned to the "target" element */
  innerRef: PropTypes.object,
  /** Text with css clamp = 2 */
  withTwoLinesText: PropTypes.bool,
  /** Text wrapping options */
  textWrap: PropTypes.oneOf(Object.values(TEXT_WRAP_STYLES)),
  /** Text to be displayed as a description of the value */
  description: PropTypes.string
}
 
export default MoleculeDropdownOption
export {handlersFactory}
export {TEXT_WRAP_STYLES as MoleculeDropdownOptionTextWrapStyles} // Deprecate
export {TEXT_WRAP_STYLES as moleculeDropdownOptionTextWrapStyles}