All files / src Radio.js

100% Statements 18/18
100% Branches 6/6
100% Functions 3/3
100% Lines 18/18

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                            3x   3x       3x         3x                                                                       5x   5x 5x   5x 9x                 9x   9x 2x   9x 2x     9x                 5x   5x                           1x  
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import InputLabel from './InputLabel'
import CheckableFieldset from './CheckableFieldset'
 
export default class Radio extends Component {
  static propTypes = {
    error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
    label: PropTypes.string,
    className: PropTypes.string,
  }
 
  render() {
    const {error, className, label, ...props} = this.props
 
    const labelClassName = classNames(className, 'rev-Radio', {
      'is-invalid': !!error,
    })
 
    const inputClassName = classNames('rev-Radio-input', {
      'is-invalid-input': !!error,
      'is-invalid': !!error,
    })
 
    return (
      <InputLabel className={labelClassName} error={error}>
        <input {...props} className={inputClassName} type="radio" />
        <span className="rev-Radio-label">{label}</span>
      </InputLabel>
    )
  }
}
 
class RadioFieldset extends Component {
  static propTypes = {
    options: PropTypes.array.isRequired,
    name: PropTypes.string,
    value: PropTypes.any,
    defaultValue: PropTypes.any,
    label: PropTypes.string,
    help: PropTypes.string,
    error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
    onChange: PropTypes.func,
    readOnly: PropTypes.bool,
    className: PropTypes.string,
  }
 
  render() {
    const {
      className,
      options,
      name,
      value,
      defaultValue,
      label,
      help,
      error,
      onChange,
      readOnly,
      ...rest
    } = this.props
 
    const isControlled = !!value
    const hasDefault = !!defaultValue
    /* eslint complexity: [2, 4] */
    const radios = options.map((option) => {
      const props = {
        name,
        error,
        onChange,
        readOnly,
        label: option.label,
        disabled: option.disabled,
      }
 
      props.value = option.value
 
      if (isControlled) {
        props.checked = value === option.value
      }
      if (hasDefault) {
        props.defaultChecked = defaultValue === option.value
      }
 
      return (
        <Radio
          key={option.key || option.value}
          className="rev-RadioFieldset-radio"
          {...props}
        />
      )
    })
 
    const fieldsetClassName = classNames(className, 'rev-RadioFieldset')
 
    return (
      <CheckableFieldset
        {...rest}
        className={fieldsetClassName}
        help={help}
        error={error}
        label={label}
      >
        {radios}
      </CheckableFieldset>
    )
  }
}
 
Radio.Fieldset = RadioFieldset