'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SubFieldContainer = exports.List = 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 _Link = require('../Link');

var _Link2 = _interopRequireDefault(_Link);

var _FieldWrapper = require('./FieldWrapper');

var _FieldWrapper2 = _interopRequireDefault(_FieldWrapper);

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 List = exports.List = _styledComponents2.default.ul.withConfig({
  displayName: 'MultiFieldWrapper__List'
})(['appearance: none;border: 1px solid #e1e1e1;width: 100%;padding: 2px;']);

var SubFieldContainer = exports.SubFieldContainer = _styledComponents2.default.li.withConfig({
  displayName: 'MultiFieldWrapper__SubFieldContainer'
})(['display: flex;flex-direction: row;margin: ', ';'], function (_ref) {
  var theme = _ref.theme;
  return theme.input.margin;
});

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

  function MultiFieldWrapper() {
    var _ref2;

    var _temp, _this, _ret;

    _classCallCheck(this, MultiFieldWrapper);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = MultiFieldWrapper.__proto__ || Object.getPrototypeOf(MultiFieldWrapper)).call.apply(_ref2, [this].concat(args))), _this), _this.renderSubField = function (name, index, fields) {
      return _react2.default.createElement(
        SubFieldContainer,
        { key: index },
        _this.props.renderField(name, index),
        _react2.default.createElement(
          _Link2.default,
          { onClick: function onClick() {
              return fields.remove(index);
            } },
          'remove'
        )
      );
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(MultiFieldWrapper, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          label = _props.label,
          helpText = _props.helpText,
          required = _props.required;

      return _react2.default.createElement(
        _FieldWrapper2.default,
        { label: label, helpText: helpText, required: required },
        _react2.default.createElement(
          List,
          null,
          this.props.fields.map(this.renderSubField)
        ),
        _react2.default.createElement(
          _Link2.default,
          { onClick: function onClick() {
              return _this2.props.fields.push('');
            } },
          'add'
        )
      );
    }
  }]);

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

MultiFieldWrapper.propTypes = {
  fields: _propTypes2.default.shape({
    name: _propTypes2.default.string,
    forEach: _propTypes2.default.func,
    get: _propTypes2.default.func,
    getAll: _propTypes2.default.func,
    insert: _propTypes2.default.func,
    length: _propTypes2.default.number,
    map: _propTypes2.default.func,
    move: _propTypes2.default.func,
    pop: _propTypes2.default.func,
    push: _propTypes2.default.func,
    remove: _propTypes2.default.func,
    removeAll: _propTypes2.default.func,
    shift: _propTypes2.default.func,
    swap: _propTypes2.default.func,
    unshift: _propTypes2.default.func
  }).isRequired,

  renderField: _propTypes2.default.func.isRequired,
  label: _propTypes2.default.string,
  helpText: _propTypes2.default.string,
  required: _propTypes2.default.bool
};
MultiFieldWrapper.defaultProps = {
  label: null,
  helpText: null,
  required: false
};


MultiFieldWrapper.usage = '\n# MultiFieldWrapper\n\nProvides a way to wrap a field for usage in a Redux Form FieldArray. Hooks into functions on the provided `fields` prop to manipulate its parent array of fields using some rendered controls.\n\nExample usage:\n\n```\nexport default class MultiTextInput extends React.Component {\n  renderField = (name, index) => (\n    <Field\n      component={TextBox}\n      name={name}\n    />\n  );\n\n  render() {\n    return (\n      <MultiFieldWrapper\n        name={this.props.name}\n        fields={this.props.fields}\n        renderField={this.renderField}\n        label={this.props.label}\n        helpText={this.props.helpText}\n        required={this.props.required}\n      />\n    );\n  }\n}\n```\n';

exports.default = MultiFieldWrapper;