"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _field = _interopRequireDefault(require("@alifd/next/lib/field"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var BaseTableField = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(BaseTableField, _React$Component);
  function BaseTableField(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    _this.field = void 0;
    _this.field = new _field["default"]((0, _assertThisInitialized2["default"])(_this), {
      onChange: function onChange(name, value) {
        _this.dataChangeHandler(value);
      }
    });
    return _this;
  }
  var _proto = BaseTableField.prototype;
  _proto.componentWillMount = function componentWillMount() {
    var name = this.props.name;
    this.field.setValue(name, this.convertValueToSet(this.props.value));
  };
  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var name = this.props.name;
    this.field.setValue(name, this.convertValueToSet(nextProps.value));
  };
  _proto.validate = function validate() {
    var _this2 = this;
    return new Promise(function (resolve) {
      var name = _this2.props.name;
      _this2.field.validate([name], function (errors) {
        resolve(errors);
      });
    });
  };
  _proto.convertValueToSet = function convertValueToSet(value) {
    return value;
  };
  _proto.dataChangeHandler = function dataChangeHandler(value) {
    var onChange = this.props.onChange;
    onChange && onChange(value);
  };
  _proto.renderField = function renderField() {
    return null;
  };
  _proto.render = function render() {
    var _this$props = this.props,
      nextTablePrefix = _this$props.nextTablePrefix,
      name = _this$props.name;
    var field = this.field;
    var errMsg = field.getError(name);
    var fieldState = field.getState(name);
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])(fieldState === "error" ? nextTablePrefix + "field-has-error" : "")
    }, /*#__PURE__*/_react["default"].createElement("div", null, this.renderField()), errMsg ? /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "field-msg"
    }, errMsg)) : null);
  };
  return BaseTableField;
}(_react["default"].Component);
var _default = BaseTableField;
exports["default"] = _default;