'use strict';

Object.defineProperty(exports, "__esModule", {
  value: 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 _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 _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

var _Radio = require('./Radio');

var _Radio2 = _interopRequireDefault(_Radio);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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 RadioGroup = function (_React$Component) {
  _inherits(RadioGroup, _React$Component);

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

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(RadioGroup).call(this, props));

    _this.state = {
      value: _this.props.value
    };

    _this.componentWillMount = function () {
      if (typeof _this.state.value !== 'undefined') {
        _this.checkItem(_this.state.value, _this.props.options);
      }
    };

    _this.componentWillReceiveProps = function (nextProps) {
      if (nextProps.value && nextProps.value !== _this.state.value) {
        _this.setState({ value: nextProps.value }, function () {
          this.checkItem(nextProps.value, nextProps.options);
        });
      }
    };

    _this.handleChange = function (event, value) {
      event.persist();
      if (value !== _this.state.value) {
        _this.setState({ value: value }, function () {
          this.checkItem(value, this.props.options);
        });
        if (typeof _this.props.changeCallback === 'function') {
          _this.props.changeCallback(event, value);
        }
      }
    };

    _this.checkItem = function (value, options) {
      var index = _this.getIndex(value, options);
      if (index >= 0) {
        options[index].checked = true;
      }
    };

    _this.getIndex = function (value, options) {
      var optionIndex = -1;
      options.map(function (radio, index) {
        if (radio.value === value) {
          optionIndex = index;
        }
      });

      return optionIndex;
    };

    _this.getOptions = function () {
      var groupName = _this.props.name;
      var groupLabelPosition = _this.props.labelPosition;
      var _this$props = _this.props;
      var options = _this$props.options;
      var label = _this$props.label;
      var name = _this$props.name;
      var value = _this$props.value;
      var required = _this$props.required;
      var labelPosition = _this$props.labelPosition;
      var changeCallback = _this$props.changeCallback;

      var other = _objectWithoutProperties(_this$props, ['options', 'label', 'name', 'value', 'required', 'labelPosition', 'changeCallback']);

      return _this.props.options.map(function (radio, index) {
        return _react2.default.createElement(_Radio2.default, _extends({
          key: radio.value,
          value: radio.value,
          label: radio.label,
          name: groupName,
          checked: _this.state.value === radio.value,
          labelPosition: groupLabelPosition,
          optClass: radio.optClass,
          changeCallback: _this.handleChange
        }, other));
      });
    };

    return _this;
  }

  _createClass(RadioGroup, [{
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var radioGroupClass = cx(_style2.default['radio-group'], this.props.optClass);

      return _react2.default.createElement(
        'div',
        { className: radioGroupClass },
        this.props.required ? _react2.default.createElement(
          'span',
          { className: _style2.default.asterisk },
          '*'
        ) : null,
        this.props.label ? _react2.default.createElement(
          'label',
          { className: _style2.default['radio-group-label'] },
          this.props.label
        ) : null,
        this.getOptions()
      );
    }
  }]);

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

RadioGroup.defaultProps = {
  disabled: false,
  required: false,
  labelPosition: 'right'
};
RadioGroup.propTypes = {
  /**
   * Text shown above the radio group.
   */
  label: _react2.default.PropTypes.string,
  /**
   * The name that will be applied to all radio buttons inside it.
   */
  name: _react2.default.PropTypes.string.isRequired,
  /**
   * Whether the radio group is required.
   */
  required: _react2.default.PropTypes.bool,
  /**
   * Whether the radio group is disabled.
   */
  disabled: _react2.default.PropTypes.bool,
  /**
   * A list of options for the radio group.
   */
  options: _react2.default.PropTypes.array.isRequired,
  /**
   * Which option is checked.
   */
  value: _react2.default.PropTypes.string,
  /**
   * Where the label will be placed for all radio buttons. This will override any labelPosition properties defined for an individual radio button.
   */
  labelPosition: _react2.default.PropTypes.oneOf(['left', 'right']),
  /**
   * A callback function to be called when an option is changed.
   */
  changeCallback: _react2.default.PropTypes.func
};
exports.default = RadioGroup;