"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; 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); }

var _button = _interopRequireDefault(require("../../button"));

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

var _ = _interopRequireDefault(require(".."));

var _input = _interopRequireDefault(require("../../input"));

var _radio = _interopRequireDefault(require("../../radio"));

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

var _reactDom = _interopRequireDefault(require("react-dom"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": 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 _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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

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

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

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

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

var FormItem = _["default"].Item;
var RadioGroup = _radio["default"].Group;
var formItemLayout = {
  labelCol: {
    span: 6
  },
  wrapperCol: {
    span: 14
  }
};

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

  var _super = _createSuper(BasicDemo);

  function BasicDemo(props) {
    var _this;

    _classCallCheck(this, BasicDemo);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "field", void 0);

    _defineProperty(_assertThisInitialized(_this), "validate", void 0);

    _this.field = new _field["default"](_assertThisInitialized(_this));

    _this.validate = function () {
      _this.field.validate(['sex']);
    };

    return _this;
  }

  _createClass(BasicDemo, [{
    key: "userExists",
    value: function userExists(rule, value) {
      return new Promise(function (resolve, reject) {
        if (!value) {
          resolve(undefined);
        } else {
          setTimeout(function () {
            if (value === 'frank') {
              reject(new Error('Sorry, this username is already occupied.'));
            } else {
              resolve(undefined);
            }
          }, 500);
        }
      });
    }
  }, {
    key: "checkPass",
    value: function checkPass(rule, value, callback) {
      var validate = this.field.validate;

      if (value) {
        validate(['rePasswd']);
      }

      callback();
    }
  }, {
    key: "checkPass2",
    value: function checkPass2(rule, value, callback) {
      var getValue = this.field.getValue;

      if (value && value !== getValue('passwd')) {
        return callback('Inconsistent password input twice!');
      }

      return callback();
    }
  }, {
    key: "render",
    value: function render() {
      var _this$field = this.field,
          getState = _this$field.getState,
          getValue = _this$field.getValue,
          getError = _this$field.getError;
      return /*#__PURE__*/_react["default"].createElement(_["default"], _extends({}, formItemLayout, {
        field: this.field
      }), /*#__PURE__*/_react["default"].createElement(FormItem, {
        label: "Username:",
        hasFeedback: true,
        required: true,
        validator: this.userExists.bind(this),
        help: // @ts-ignore
        getState('username') === 'loading' ? 'Checking ...' : getError('username')
      }, /*#__PURE__*/_react["default"].createElement(_input["default"], {
        placeholder: "Input frank",
        name: "username"
      }), /*#__PURE__*/_react["default"].createElement("p", null, "Hello ", getValue('username'))), /*#__PURE__*/_react["default"].createElement(FormItem, {
        label: "Password:",
        hasFeedback: true,
        required: true,
        requiredMessage: "Please enter password",
        validator: this.checkPass.bind(this)
      }, /*#__PURE__*/_react["default"].createElement(_input["default"], {
        htmlType: "password",
        name: "passwd"
      })), /*#__PURE__*/_react["default"].createElement(FormItem, {
        label: "Check your password:",
        hasFeedback: true,
        required: true,
        requiredMessage: "Enter your password again",
        validator: this.checkPass2.bind(this)
      }, /*#__PURE__*/_react["default"].createElement(_input["default"], {
        htmlType: "password",
        placeholder: "Enter the same password twice",
        name: "rePasswd"
      })), /*#__PURE__*/_react["default"].createElement(FormItem, {
        label: "Gender:",
        hasFeedback: true,
        required: true,
        requiredMessage: "Please select your gender"
      }, /*#__PURE__*/_react["default"].createElement(RadioGroup, {
        name: "sex"
      }, /*#__PURE__*/_react["default"].createElement(_radio["default"], {
        value: "male"
      }, "Male"), /*#__PURE__*/_react["default"].createElement(_radio["default"], {
        value: "female"
      }, "Female"))), /*#__PURE__*/_react["default"].createElement(FormItem, {
        wrapperCol: {
          offset: 6
        }
      }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
        onClick: this.validate
      }, "Validate by Field"), /*#__PURE__*/_react["default"].createElement(_["default"].Submit, {
        validate: true,
        type: "primary",
        onClick: function onClick(v, e) {
          return console.log(v, e);
        },
        style: {
          margin: '0 10px'
        }
      }, "Submit"), /*#__PURE__*/_react["default"].createElement(_["default"].Reset, null, "Reset")));
    }
  }]);

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

_reactDom["default"].render( /*#__PURE__*/_react["default"].createElement(BasicDemo, null), document.getElementById('form-demo-12'));