All files / molecule/buttonGroup/src index.js

89.65% Statements 26/29
64.28% Branches 18/28
100% Functions 7/7
92.3% Lines 24/26

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                          1x 2x 4x     4x 2x         1x                                         2x   2x             2x 2x     2x 2x     2x 2x 2x     2x   2x 2x   2x     4x   4x                               2x                               1x   1x                                                                                                                        
import {Children} from 'react'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import {atomButtonDesigns, atomButtonShapes, atomButtonSizes} from '@s-ui/react-atom-button'
import Injector from '@s-ui/react-primitive-injector'
import Poly from '@s-ui/react-primitive-polymorphic-element'
 
import {DEFAULT_COLUMNS, DISPLAY, SPACED} from './config.js'
import {BASE_CLASS} from './settings.js'
 
const getGroupPosition =
  ({groupPositions, numChildren, spaced, shape}) =>
  index => {
    Iif (spaced && !shape) {
      return groupPositions.UNSET
    } else {
      if (index === 0) return groupPositions.FIRST
      Eif (index === numChildren - 1) return groupPositions.LAST
      return groupPositions.MIDDLE
    }
  }
 
const MoleculeButtonGroup = ({
  as = 'div',
  children,
  columns,
  fullWidth,
  size,
  design,
  display,
  negative,
  groupPositions = {
    FIRST: 'first',
    MIDDLE: 'middle',
    LAST: 'last',
    UNSET: 'unset'
  },
  onClick,
  spaced,
  isVertical,
  shape,
  ...props
}) => {
  const numChildren = children.length
 
  const getGroupPositionByIndex = getGroupPosition({
    spaced,
    groupPositions,
    numChildren,
    shape
  })
 
  const getClassSpaced = ({spaced = SPACED.MEDIUM}) => {
    return `${BASE_CLASS}--spaced-${spaced}`
  }
 
  const getClassDisplay = ({display = DISPLAY.FLEX}) => {
    return `${BASE_CLASS}--display-${display}`
  }
 
  const getClassDisplayColumns = ({display = DISPLAY.FLEX, columns = DEFAULT_COLUMNS}) => {
    Iif (display === DISPLAY.GRID && columns) return `${BASE_CLASS}--col-${columns}`
    return ''
  }
 
  const CLASS_SPACED = getClassSpaced({spaced})
 
  const CLASS_DISPLAY = getClassDisplay({display})
  const CLASS_DISPLAY_COLUMNS = getClassDisplayColumns({display, columns})
 
  const extendedChildren = Children.toArray(children)
    .filter(Boolean)
    .map((child, index) => {
      const groupPosition = getGroupPositionByIndex(index)
 
      return (
        <Injector
          {...props}
          negative={negative}
          size={size}
          design={design}
          groupPosition={groupPosition}
          fullWidth={fullWidth}
          spaced={spaced}
          onClick={onClick}
          shape={shape}
        >
          {child}
        </Injector>
      )
    })
  return (
    <Poly
      className={cx(
        BASE_CLASS,
        fullWidth && `${BASE_CLASS}--fullWidth`,
        display && CLASS_DISPLAY,
        display && CLASS_DISPLAY_COLUMNS,
        spaced && CLASS_SPACED,
        isVertical && `${BASE_CLASS}--vertical`
      )}
    >
      {extendedChildren}
    </Poly>
  )
}
 
MoleculeButtonGroup.displayName = 'MoleculeButtonGroup'
 
MoleculeButtonGroup.propTypes = {
  /** the html element tag type of teh component **/
  as: PropTypes.elementType,
 
  children: PropTypes.arrayOf(PropTypes.element),
 
  /** Number of columns (2, 3 or 4) if display is set to grid */
  columns: PropTypes.oneOf([2, 3, 4]),
 
  /** If buttons should stretch to fit the width of container */
  fullWidth: PropTypes.bool,
 
  /** groupPositions constants (FIRST, MIDDLE, LAST) */
  groupPositions: PropTypes.object,
 
  /**
   * Negative: style for dark backgrounds.
   */
  negative: PropTypes.bool,
 
  /**
   * Design style of button: 'solid' (default), 'outline', 'flat', 'link'
   */
  design: PropTypes.oneOf(Object.values(atomButtonDesigns)),
  /**
   * Size of button 'small' (default), 'large'
   */
  size: PropTypes.oneOf(Object.values(atomButtonSizes)),
 
  /**
   * common click handler fired every inner button is triggered.
   */
  onClick: PropTypes.func,
 
  /**
   * configure the gap between the buttons of the group
   **/
  spaced: PropTypes.oneOf(Object.values(SPACED)),
 
  /**
   * configure the display of component. Flex by default
   **/
  display: PropTypes.oneOf(Object.values(DISPLAY)),
 
  /** buttons should have a vertical layout */
  isVertical: PropTypes.bool,
 
  /** Shape of button */
  shape: PropTypes.oneOf(Object.values(atomButtonShapes))
}
 
export default MoleculeButtonGroup
 
export {
  atomButtonDesigns as moleculeButtonGroupDesigns,
  atomButtonSizes as moleculeButtonGroupSizes,
  atomButtonShapes as moleculeButtonGroupShapes,
  SPACED as moleculeButtonGroupSpaced,
  DISPLAY as moleculeButtonGroupDisplay
}