import _Field from "@alifd/next/es/field";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import React from "react";
import classnames from 'classnames';
var BaseTableField = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(BaseTableField, _React$Component);
  function BaseTableField(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    _this.field = void 0;
    _this.field = new _Field(_assertThisInitialized(_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.createElement("div", {
      className: classnames(fieldState === "error" ? nextTablePrefix + "field-has-error" : "")
    }, /*#__PURE__*/React.createElement("div", null, this.renderField()), errMsg ? /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
      className: nextTablePrefix + "field-msg"
    }, errMsg)) : null);
  };
  return BaseTableField;
}(React.Component);
export default BaseTableField;