# All files / components/RadioGroup RadioGroup.js

0% Statements 0/69
0% Branches 0/16
0% Functions 0/11
0% Lines 0/27

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import { withProps } from 'recompose';
    import RadioGroupButton from './RadioButton';
    import RadioGroupContainer from './styles/RadioGroupContainer';
    import generateId from 'extensions/generateId';

    class RadioGroup extends React.Component {
      static propTypes = {
        /**
         * An array of choice objects or strings to create buttons from.
         */
        options: PropTypes.oneOfType([
          PropTypes.arrayOf(PropTypes.string),
          PropTypes.objectOf(PropTypes.node),
        ]).isRequired,
        /**
         * A field name for this input.
         */
        name: PropTypes.string,
        /**
         * The currently selected value.
         */
        value: PropTypes.string.isRequired,
        /**
         * Called when the input value changes.
         */
        onChange: PropTypes.func.isRequired,
        /**
         * Indicates whether the user can change the input.
         */
        disabled: PropTypes.bool,
        /**
         * Indicates whether the value is required for form input.
         */
        required: PropTypes.bool,
        /**
         * Adds a class name to the group container element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the group container element.
         */
        id: PropTypes.string,
        /**
         * A component for rendering a container around the buttons
         */
        Container: PropTypes.func,
        /**
         * A component for rendering a button, defaults RadioGroup.Button
         */
        Button: PropTypes.func,
      };

      static defaultProps = {
        disabled: false,
        required: false,
        className: null,
        id: null,
        choices: null,
        Container: RadioGroupContainer,
        Button: RadioGroupButton,
        name: undefined,
      };

      getOptions = () => this.props.options || this.props.choices;
      getName = () => this.props.name || generateId('radioGroup');

      optionsToButtons = () => {
        const { value, onChange, disabled, required, Button } = this.props;
        const options = this.getOptions();
        const name = this.getName();
        if (options instanceof Array) {
          return options.map(choice => (
            <Button
              checked={choice === value}
              name={name}
              label={choice}
              value={choice}
              key={choice}
              disabled={disabled}
              required={required}
              onChange={() => onChange(choice)}
            />
          ));
        } else if (typeof options === 'object') {
          return Object.keys(options).map(key => {
            const choice = options[key];

            return (
              <Button
                checked={key === value}
                name={name}
                value={key}
                label={key}
                children={choice}
                key={key}
                disabled={disabled}
                required={required}
                onChange={() => onChange(key)}
              />
            );
          });
        } else {
          return 'invalid options';
        }
      };

      render() {
        const { className, id, Container } = this.props;
        return (
          <Container className={className} id={id}>
            {this.optionsToButtons()}
          </Container>
        );
      }
    }

    RadioGroup.Vertical = withProps({
      Container: RadioGroupContainer.Vertical,
    })(RadioGroup);
    RadioGroup.Small = withProps({
      Button: RadioGroupButton.Small,
    })(RadioGroup);
    RadioGroup.Large = withProps({
      Button: RadioGroupButton.Large,
    })(RadioGroup);

    RadioGroup.Vertical.Small = RadioGroup.Small.Vertical = withProps({
      Container: RadioGroupContainer.Vertical,
      Button: RadioGroupButton.Small,
    })(RadioGroup);
    RadioGroup.Vertical.Large = RadioGroup.Large.Vertical = withProps({
      Container: RadioGroupContainer.Vertical,
      Button: RadioGroupButton.Large,
    })(RadioGroup);

    RadioGroup.Button = RadioGroupButton;

    export default RadioGroup;