All files / molecule/radioButtonGroup/src index.js

100% Statements 9/9
100% Branches 2/2
100% Functions 3/3
100% Lines 9/9

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              1x 5x 5x 1x 1x   5x       10x                                 1x   1x                                                
import PropTypes from 'prop-types'
 
import useControlledState from '@s-ui/react-hooks/lib/useControlledState'
import Injector, {combineProps} from '@s-ui/react-primitive-injector'
 
import {BASE_CLASS} from './settings.js'
 
const MoleculeRadioButtonGroup = ({id, value, defaultValue, onChange, children, name, ...props}) => {
  const [innerValue, setInnerValue] = useControlledState(value, defaultValue)
  const handleChangeGroup = (e, {name, value}) => {
    setInnerValue(value)
    typeof onChange === 'function' && onChange(e, {name, value})
  }
  return (
    <div className={BASE_CLASS} id={id}>
      <Injector
        combine={(injectedProps, {checked, onChange, ...ownProps}) => {
          return combineProps(
            {
              checked: ownProps.value === innerValue,
              onChange: handleChangeGroup,
              name,
              ...props
            },
            ownProps
          )
        }}
      >
        {children}
      </Injector>
    </div>
  )
}
 
MoleculeRadioButtonGroup.displayName = 'MoleculeRadioButtonGroup'
 
MoleculeRadioButtonGroup.propTypes = {
  /* children */
  children: PropTypes.any,
 
  /* name */
  name: PropTypes.string,
 
  /* The DOM id global attribute. */
  id: PropTypes.string,
 
  /* This Boolean attribute prevents the user from interacting with the input */
  disabled: PropTypes.bool,
 
  /* onChange callback */
  onChange: PropTypes.func,
 
  /* Controlled value assigned to the radio button */
  value: PropTypes.string,
 
  /* Initial value assigned to the radio button */
  defaultValue: PropTypes.string
}
 
export default MoleculeRadioButtonGroup