'use strict';

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

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 _generateId = require('../../extensions/generateId');

var _generateId2 = _interopRequireDefault(_generateId);

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 = exports.Container = _styledComponents2.default.div.withConfig({
  displayName: 'BinaryField__Container'
})(['']);

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

  function BinaryField() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, BinaryField);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = BinaryField.__proto__ || Object.getPrototypeOf(BinaryField)).call.apply(_ref, [this].concat(args))), _this), _this.handleClick = function () {
      _this.props.input.onChange(!_this.props.input.value);
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(BinaryField, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          label = _props.label,
          _props$input = _props.input,
          value = _props$input.value,
          disabled = _props$input.disabled,
          Input = _props.Input,
          Label = _props.Label;

      var id = this.props.id;

      if (!id) {
        /*
          because this generation happens at render time,
          the id is not reliable. developers should specify an id through
          props if they want a reliable id.
        */
        id = (0, _generateId2.default)('toggle');
      }

      return _react2.default.createElement(
        Container,
        null,
        _react2.default.createElement(Input, { id: id, type: 'checkbox', value: value, disabled: disabled, onClick: this.handleClick }),
        _react2.default.createElement(
          Label,
          { htmlFor: id, active: value },
          label
        )
      );
    }
  }]);

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

BinaryField.propTypes = {
  input: _propTypes2.default.shape({
    value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.bool]),
    onChange: _propTypes2.default.func,
    disabled: _propTypes2.default.bool
  }),
  label: _propTypes2.default.string,
  id: _propTypes2.default.string,

  Input: _propTypes2.default.func.isRequired,
  Label: _propTypes2.default.func.isRequired
};
BinaryField.defaultProps = {
  input: {
    value: 'false'
  },
  label: null,
  id: null
};


BinaryField.usage = '\n# BinaryField\n\nThis is really more of a base class for inputs which switch between two values (see: Toggle and Checkbox).\n\nProps:\n\n* `input`: supplied by Redux Form\'s Field component, you can also specify this manually. Should contain `value` and `onChange` at least.\n* `label`: a renderable label to go above the component\n* `helpText`: some text to be rendered below the component\n* `disabled`: disables the component\n* `required`: adds a required mark and HTML field validation\n';

exports.default = BinaryField;