All files / molecule/select/src/components SingleSelection.js

29.16% Statements 7/24
28.57% Branches 4/14
25% Functions 1/4
30.43% Lines 7/23

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                                1x                                                 10x   10x           10x                                               10x                                                                     1x                                         1x      
import {forwardRef} from 'react'
 
import PropTypes from 'prop-types'
 
import AtomInput, {inputTypes} from '@s-ui/react-atom-input'
import MoleculeDropdownList from '@s-ui/react-molecule-dropdown-list'
 
import {
  SELECT_DROPDOWN_LIST_SIZES as dropdownListSizes,
  SELECT_INPUT_SIZES as inputSizes,
  useDropdown
} from '../config.js'
import {CLASS_SEARCH_CONTAINER} from './config.js'
import MoleculeInputSelect from './MoleculeInputSelect.js'
import Search from './Search.js'
 
const MoleculeSelectSingleSelection = forwardRef(
  (
    {
      value = '',
      children,
      isOpen,
      onToggle,
      onTriggerClick,
      onChange,
      leftIcon,
      iconArrowDown,
      refMoleculeSelect,
      refSearch,
      selectSize,
      placeholder,
      id,
      disabled,
      optionsData = {},
      required,
      tabIndex,
      size,
      ...props
    },
    forwardedRef
  ) => {
    const {hasSearch, isFirstOptionFocused, inputSearch, focusedFirstOption, setFocusedFirstOption} = useDropdown()
 
    const handleSelection = (ev, {value}) => {
      onChange(ev, {value})
      onToggle(ev, {isOpen: false})
      forwardedRef?.current?.focus()
    }
 
    const handleKeyDown = ev => {
      if (isFirstOptionFocused()) {
        setFocusedFirstOption(true)
      } else {
        setFocusedFirstOption(false)
      }
 
      switch (ev?.key) {
        case 'Escape':
          onToggle(ev, {isOpen: false})
          forwardedRef?.current?.focus()
          break
        case 'Tab':
          onToggle(ev, {isOpen: false})
          forwardedRef?.current?.focus()
          break
        case 'ArrowUp':
          focusedFirstOption && setTimeout(() => inputSearch?.focus())
          break
        default:
          break
      }
    }
 
    return (
      <>
        <MoleculeInputSelect
          disabled={disabled}
          id={id}
          isOpen={isOpen}
          value={optionsData[value] || ''}
          onClick={onTriggerClick}
          leftIcon={leftIcon}
          iconArrowDown={iconArrowDown}
          placeholder={placeholder}
          autoComplete="off"
          required={required}
          size={selectSize}
          tabIndex={tabIndex}
        >
          <AtomInput ref={forwardedRef} inputMode={inputTypes.NONE} noBorder />
        </MoleculeInputSelect>
        <div className={CLASS_SEARCH_CONTAINER}>
          {hasSearch && <Search ref={refSearch} />}
          <MoleculeDropdownList
            size={size}
            visible={isOpen}
            onSelect={handleSelection}
            value={value}
            onKeyDown={handleKeyDown}
          >
            {children}
          </MoleculeDropdownList>
        </div>
      </>
    )
  }
)
 
MoleculeSelectSingleSelection.propTypes = {
  value: PropTypes.string,
  children: PropTypes.node,
  isOpen: PropTypes.bool,
  onToggle: PropTypes.func,
  onChange: PropTypes.func,
  onTriggerClick: PropTypes.func,
  leftIcon: PropTypes.node,
  iconArrowDown: PropTypes.node,
  refSearch: PropTypes.object,
  refMoleculeSelect: PropTypes.object,
  selectSize: PropTypes.oneOf(Object.values(inputSizes)),
  size: PropTypes.oneOf(Object.values(dropdownListSizes)),
  placeholder: PropTypes.string,
  id: PropTypes.string,
  disabled: PropTypes.bool,
  optionsData: PropTypes.object,
  required: PropTypes.bool,
  tabIndex: PropTypes.number
}
 
MoleculeSelectSingleSelection.displayName = 'MoleculeSelectSingleSelection'
 
export default MoleculeSelectSingleSelection