'use strict';

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

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 _Label = require('../../components/Label');

var _Label2 = _interopRequireDefault(_Label);

var _HelpText = require('../../components/HelpText');

var _HelpText2 = _interopRequireDefault(_HelpText);

var _FieldContent = require('./styles/FieldContent');

var _FieldContent2 = _interopRequireDefault(_FieldContent);

var _lodash = require('lodash');

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; }

/**
 * A single Field element. **Important:** Field cannot be used on its own. Please use Field
 * within the Fields component. See: [Fields](#!/Fields)
 */
var Field = function (_React$Component) {
  _inherits(Field, _React$Component);

  function Field() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Field);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Field.__proto__ || Object.getPrototypeOf(Field)).call.apply(_ref, [this].concat(args))), _this), _this.stylesFor = function (gridName) {
      var _this$props = _this.props,
          column = _this$props.column,
          columnSpan = _this$props.columnSpan;


      var endColumn = column + columnSpan - 1;
      return {
        gridArea: '' + gridName + column + ' / ' + gridName + column + ' / ' + gridName + endColumn + ' / ' + gridName + endColumn,
        // align-self determines how the parts vertically align if an adjacent part is taller.
        // labels should stick to the bottom if an adjacent label has two or more lines.
        // content and helpText should stick to the top if an adjacent element is taller than them.
        alignSelf: gridName === 'label' ? 'end' : 'start'
      };
    }, _this.renderLabel = function () {
      var _this$props2 = _this.props,
          label = _this$props2.label,
          disabled = _this$props2.disabled,
          required = _this$props2.required,
          Label = _this$props2.Label,
          children = _this$props2.children;


      if (!label) {
        return null;
      }

      var labelFor = (0, _lodash.get)(children, 'props.id', null);

      var labelProps = {
        disabled: disabled,
        required: required,
        htmlFor: labelFor,
        style: _this.stylesFor('label')
      };

      if (typeof label === 'string') {
        return _react2.default.createElement(
          Label,
          labelProps,
          label
        );
      }
      return _react2.default.cloneElement(label, labelProps);
    }, _this.renderHelpText = function () {
      var _this$props3 = _this.props,
          helpText = _this$props3.helpText,
          error = _this$props3.error,
          HelpText = _this$props3.HelpText;


      if (!helpText) {
        return null;
      }

      var helpTextProps = {
        style: _this.stylesFor('helpText'),
        error: error
      };

      if (typeof helpText === 'string') {
        return _react2.default.createElement(
          HelpText,
          helpTextProps,
          helpText
        );
      }
      return _react2.default.cloneElement(helpText, helpTextProps);
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Field, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          children = _props.children,
          Content = _props.Content,
          alignContent = _props.alignContent;


      return _react2.default.createElement(
        _react2.default.Fragment,
        null,
        this.renderLabel(),
        _react2.default.createElement(
          Content,
          { style: this.stylesFor('content'), align: alignContent },
          children
        ),
        this.renderHelpText()
      );
    }
  }]);

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

Field.propTypes = {
  /**
   * **Automatically supplied within <Fields>**
   * Do not supply this prop if you are using Field within a Fields component.
   * The column index to place this field at within its row.
   */
  column: _propTypes2.default.number,

  /**
   * The number of columns which this field should span within its row.
   */
  columnSpan: _propTypes2.default.number,
  /**
   * (stretch|left|right): the alignment of contents within the Field
   */
  alignContent: _propTypes2.default.oneOf(['stretch', 'left', 'right']),

  /**
   * A string or node to render in the label position. A string will be wrapped with the Label component prop.
   * If you provide a custom node, it will receive the htmlFor prop. Please assign this prop to a <label> element.
   */
  label: _propTypes2.default.node,
  /**
   * Indicates if the field is required by the form.
   */
  required: _propTypes2.default.bool,
  /**
   * Disables the form label.
   */
  disabled: _propTypes2.default.bool,
  /**
   * Contents to render inside the main field area.
   */
  children: _propTypes2.default.node,
  /**
   * A string or node to render in the help text position. A string will be wrapped with the HelpText component prop.
   */
  helpText: _propTypes2.default.node,
  /**
   * A component prop to override the component used to render labels. Defaults to library Label.
   */
  Label: _propTypes2.default.func,
  /**
   * A component prop to override the component used to render help text. Defaults to library HelpText.
   */
  HelpText: _propTypes2.default.func,
  /**
   * A component prop to override the component used to wrap field content. Defaults to library FieldContent.
   */
  Content: _propTypes2.default.func
};
Field.defaultProps = {
  column: 0,
  columnSpan: 1,
  Label: _Label2.default,
  HelpText: _HelpText2.default,
  Content: _FieldContent2.default,
  helpText: null,
  label: null,
  required: false,
  disabled: false
};
exports.default = Field;