'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _RadioButton = require('./RadioButton');

var _RadioButton2 = _interopRequireDefault(_RadioButton);

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 Container = _styledComponents2.default.div.withConfig({
  displayName: 'RadioGroup__Container'
})(['display: flex;flex-direction: row;align-items: stretch;flex: 1 1 auto;min-height: 53px;']);

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

  function RadioGroup() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, RadioGroup);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = RadioGroup.__proto__ || Object.getPrototypeOf(RadioGroup)).call.apply(_ref, [this].concat(args))), _this), _this.choicesToButtons = function () {
      var _this$props = _this.props,
          choices = _this$props.choices,
          value = _this$props.value,
          _onChange = _this$props.onChange,
          disabled = _this$props.disabled,
          required = _this$props.required;

      if (choices instanceof Array) {
        return choices.map(function (choice) {
          return _react2.default.createElement(_RadioButton2.default, {
            checked: choice === value,
            name: name,
            label: choice,
            value: choice,
            key: choice,
            disabled: disabled,
            required: required,
            onChange: function onChange() {
              return _onChange(choice);
            }
          });
        });
      } else if ((typeof choices === 'undefined' ? 'undefined' : _typeof(choices)) === 'object') {
        return Object.keys(choices).map(function (key) {
          var choice = choices[key];

          return _react2.default.createElement(_RadioButton2.default, {
            checked: key === value,
            name: name,
            value: key,
            label: key,
            content: choice,
            key: key,
            disabled: disabled,
            required: required,
            onChange: function onChange() {
              return _onChange(key);
            }
          });
        });
      } else {
        return 'invalid choices';
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(RadioGroup, [{
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        Container,
        null,
        this.choicesToButtons()
      );
    }
  }]);

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

RadioGroup.propTypes = {
  choices: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.string), _propTypes2.default.objectOf(_propTypes2.default.node)]).isRequired,
  name: _propTypes2.default.string.isRequired,
  value: _propTypes2.default.string.isRequired,
  onChange: _propTypes2.default.func.isRequired,

  disabled: _propTypes2.default.bool,
  required: _propTypes2.default.bool
};
RadioGroup.defaultProps = {
  disabled: false,
  required: false
};


RadioGroup.usage = '\n# RadioGroup\n\nRadioGroup is a fully functional input. It generates its own radio buttons from the options you supply. You can use it manually by passing in a valid `input` prop which contains a `value` and `onChange` handler. It\'s a controlled component, so it won\'t update its own value.\n\n```\n<RadioGroup value="a" onChange={handleChange} choices={[\'a\', \'b\']} />\n```\n';

exports.default = RadioGroup;