"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _util = require("../util");

var _field = _interopRequireDefault(require("../field"));

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

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

function _extends() { _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; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function pickerDefined(obj) {
  var newObj = {};
  Object.keys(obj).forEach(function (i) {
    if (typeof obj[i] !== 'undefined') {
      newObj[i] = obj[i];
    }
  });
  return newObj;
}

function preventDefault(e) {
  e.preventDefault();
}
/** Form */


var Form =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Form, _React$Component);

  function Form(props) {
    var _this;

    _classCallCheck(this, Form);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Form).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onChange", function (name, value) {
      _this.props.onChange(_this._formField.getValues(), {
        name: name,
        value: value,
        field: _this._formField
      });
    });

    var options = _objectSpread({}, props.fieldOptions, {
      onChange: _this.onChange
    });

    if (props.field) {
      _this._formField = props.field;
      var onChange = _this._formField.options.onChange;
      options.onChange = _util.func.makeChain(onChange, _this.onChange);
      _this._formField.setOptions && _this._formField.setOptions(options);
    } else {
      if ('value' in props) {
        options.values = props.value;
      }

      _this._formField = new _field["default"](_assertThisInitialized(_this), options);
    }

    props.saveField(_this._formField);
    return _this;
  }

  _createClass(Form, [{
    key: "getChildContext",
    value: function getChildContext() {
      return {
        _formField: this.props.field ? this.props.field : this._formField,
        _formSize: this.props.size
      };
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('value' in nextProps) {
        this._formField.setValues(nextProps.value);
      }

      if ('error' in nextProps) {
        this._formField.setErrors(nextProps.error);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props = this.props,
          className = _this$props.className,
          inline = _this$props.inline,
          size = _this$props.size,
          labelAlign = _this$props.labelAlign,
          labelTextAlign = _this$props.labelTextAlign,
          onSubmit = _this$props.onSubmit,
          children = _this$props.children,
          labelCol = _this$props.labelCol,
          wrapperCol = _this$props.wrapperCol,
          style = _this$props.style,
          prefix = _this$props.prefix,
          rtl = _this$props.rtl,
          Tag = _this$props.component;
      var formClassName = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "form"), true), _defineProperty(_classNames, "".concat(prefix, "inline"), inline), _defineProperty(_classNames, "".concat(prefix).concat(size), size), _defineProperty(_classNames, "".concat(prefix, "top"), labelAlign === 'top'), _defineProperty(_classNames, className, !!className), _classNames));
      return _react["default"].createElement(Tag, _extends({
        role: "grid"
      }, _util.obj.pickOthers(Form.propTypes, this.props), {
        className: formClassName,
        style: style,
        dir: rtl ? 'rtl' : undefined,
        onSubmit: onSubmit
      }), _react["default"].Children.map(children, function (child) {
        if (child && typeof child.type === 'function' && child.type._typeMark === 'form_item') {
          var childrenProps = {
            labelCol: child.props.labelCol ? child.props.labelCol : labelCol,
            wrapperCol: child.props.wrapperCol ? child.props.wrapperCol : wrapperCol,
            labelAlign: child.props.labelAlign ? child.props.labelAlign : labelAlign,
            labelTextAlign: child.props.labelTextAlign ? child.props.labelTextAlign : labelTextAlign,
            size: child.props.size ? child.props.size : size
          };
          return _react["default"].cloneElement(child, pickerDefined(childrenProps));
        }

        return child;
      }));
    }
  }]);

  return Form;
}(_react["default"].Component);

exports["default"] = Form;

_defineProperty(Form, "propTypes", {
  /**
   * 样式前缀
   */
  prefix: _propTypes["default"].string,

  /**
   * 内联表单
   */
  inline: _propTypes["default"].bool,

  /**
   * 单个 Item 的 size 自定义，优先级高于 Form 的 size, 并且当组件与 Item 一起使用时，组件自身设置 size 属性无效。
   * @enumdesc 中, 小
   */
  size: _propTypes["default"].oneOf(['medium', 'small']),

  /**
   * 标签的位置
   * @enumdesc 上, 左, 内
   */
  labelAlign: _propTypes["default"].oneOf(['top', 'left', 'inset']),

  /**
   * 标签的左右对齐方式
   * @enumdesc 左, 右
   */
  labelTextAlign: _propTypes["default"].oneOf(['left', 'right']),

  /**
   * 经 `new Field(this)` 初始化后，直接传给 Form 即可 用到表单校验则不可忽略此项
   */
  field: _propTypes["default"].any,

  /**
   * 保存 Form 自动生成的 field 对象
   */
  saveField: _propTypes["default"].func,

  /**
   * 控制第一级 Item 的 labelCol
   */
  labelCol: _propTypes["default"].object,

  /**
   * 控制第一级 Item 的 wrapperCol
   */
  wrapperCol: _propTypes["default"].object,

  /**
   * form内有 `htmlType="submit"` 的元素的时候会触发
   */
  onSubmit: _propTypes["default"].func,

  /**
   * 子元素
   */
  children: _propTypes["default"].any,

  /**
   * 扩展class
   */
  className: _propTypes["default"].string,

  /**
   * 自定义内联样式
   */
  style: _propTypes["default"].object,

  /**
   * 表单数值
   */
  value: _propTypes["default"].object,

  /**
   * 表单变化回调
   * @param {Object} values 表单数据
   * @param {Object} item 详细
   * @param {String} item.name 变化的组件名
   * @param {String} item.value 变化的数据
   * @param {Object} item.field field 实例
   */
  onChange: _propTypes["default"].func,

  /**
   * 设置标签类型
   */
  component: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].func]),
  fieldOptions: _propTypes["default"].object,
  rtl: _propTypes["default"].bool
});

_defineProperty(Form, "defaultProps", {
  prefix: 'next-',
  onSubmit: preventDefault,
  size: 'medium',
  labelAlign: 'left',
  onChange: _util.func.noop,
  component: 'form',
  saveField: _util.func.noop
});

_defineProperty(Form, "childContextTypes", {
  _formField: _propTypes["default"].object,
  _formSize: _propTypes["default"].string
});