'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _InputRadio = require('../InputRadio');

var _InputRadio2 = _interopRequireDefault(_InputRadio);

var _ErrorMessage = require('../ErrorMessage');

var _ErrorMessage2 = _interopRequireDefault(_ErrorMessage);

require('./style.css');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var InputRadioGroup = function (_React$Component) {
  _inherits(InputRadioGroup, _React$Component);

  function InputRadioGroup(props) {
    _classCallCheck(this, InputRadioGroup);

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.handleChange = function (event) {
      var selected = event.target.value;
      if (selected !== _this.state.selected) {
        _this.setState({ selected: selected });
        if (typeof _this.props.onChange === 'function') {
          var name = _this.props.name;

          _this.props.onChange({ selected: selected, name: name, event: event });
        }
      }
    };

    _this.state = {
      selected: _this.props.defaultSelected
    };
    return _this;
  }

  InputRadioGroup.prototype.render = function render() {
    var _this2 = this;

    var itemsClasses = (0, _classnames2.default)({
      'ma__input-group__items': true,
      'ma__input-group__items--inline': this.props.inline,
      'ma__input-group__items--outline': this.props.outline
    });
    var titleClasses = (0, _classnames2.default)({
      'ma__input-group__title': true,
      'ma__input-group__title--error': this.props.error,
      'ma__input-group__title--disabled': this.props.disabled
    });

    return _react2.default.createElement(
      _react2.default.Fragment,
      null,
      _react2.default.createElement(
        'fieldset',
        null,
        _react2.default.createElement(
          'div',
          { className: 'ma__input-group' },
          _react2.default.createElement(
            'legend',
            { className: titleClasses },
            this.props.title
          ),
          _react2.default.createElement(
            'div',
            { className: itemsClasses },
            this.props.radioButtons.map(function (radioButton, index) {
              var isChecked = radioButton.value === _this2.state.selected;
              var buttonId = radioButton.id || radioButton.value;
              return (
                /* eslint-disable-next-line react/no-array-index-key */
                _react2.default.createElement(
                  'div',
                  { className: 'ma__input-group__item item-' + _this2.props.radioButtons.length + ' ' + radioButton.class, key: 'InputRadioGroupDiv-' + buttonId + '-' + index },
                  _react2.default.createElement(_InputRadio2.default, _extends({}, radioButton, {
                    name: _this2.props.name,
                    onChange: _this2.handleChange,
                    checked: isChecked,
                    required: _this2.props.required,
                    outline: _this2.props.outline,
                    error: _this2.props.error,
                    disabled: _this2.props.disabled
                    /* eslint-disable-next-line react/no-array-index-key */
                    , key: 'InputRadioGroup-' + buttonId + '-' + index
                  }))
                )
              );
            })
          )
        )
      ),
      this.props.errorMsg && this.props.error && _react2.default.createElement(_ErrorMessage2.default, { status: true, error: this.props.errorMsg, inputID: this.props.name })
    );
  };

  return InputRadioGroup;
}(_react2.default.Component);

InputRadioGroup.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The legend title of the radio button group. */
  title: _propTypes2.default.string.isRequired,
  /** The name of the radio button group */
  name: _propTypes2.default.string.isRequired,
  /** Whether radio input is required or not */
  required: _propTypes2.default.bool,
  /** Whether you want the radio input outlined */
  outline: _propTypes2.default.bool,
  /** The default select radio button option on initial render */
  defaultSelected: _propTypes2.default.string,
  /** Whether the radio group is in error state or not. */
  error: _propTypes2.default.bool,
  /** Error Message content. */
  errorMsg: _propTypes2.default.string,
  /** Display Inputs inline */
  inline: _propTypes2.default.bool,
  /** Whether the radio button group is in a disabled state or not */
  disabled: _propTypes2.default.bool,
  /** On change callback function */
  onChange: _propTypes2.default.func,
  /** An array of radio buttons. */
  radioButtons: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    id: _propTypes2.default.string,
    value: _propTypes2.default.string.isRequired,
    label: _propTypes2.default.string.isRequired,
    /** Allow adding one class to radio button, e.g. "col-medium-1", "col-large-1" */
    class: _propTypes2.default.string
  }))
} : {};

InputRadioGroup.defaultProps = {
  outline: false,
  required: false,
  inline: true,
  error: false,
  disabled: false
};

exports.default = InputRadioGroup;
module.exports = exports['default'];