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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-08-12 11:06:00
 * @LastEditTime: 2021-09-06 09:01:44
 * @FilePath: \young\components\form\form.tsx
 */
import React, { useEffect } from 'react';
import { Form } from 'remax/one';
import classNames from 'classnames';
import { FormContext, HOOK_KEY } from './context';
import useForm from './useForm';
import { getPrefixCls } from './../common';
var prefixCls = getPrefixCls('form');

var AnnarForm = function AnnarForm(props) {
  var initialValues = props.initialValues,
      form = props.form,
      children = props.children,
      onFinish = props.onFinish,
      onFinishFailed = props.onFinishFailed,
      onReset = props.onReset;

  var _useForm = useForm(form),
      _useForm2 = _slicedToArray(_useForm, 1),
      formInstance = _useForm2[0];

  var setFieldsValue = formInstance.setFieldsValue,
      submit = formInstance.submit;

  var _formInstance$getInte = formInstance.getInternalHooks(HOOK_KEY),
      initStore = _formInstance$getInte.initStore,
      setErrors = _formInstance$getInte.setErrors,
      setCallbacks = _formInstance$getInte.setCallbacks;

  useEffect(function () {
    initStore(initialValues);
    setCallbacks({
      onFinish: onFinish,
      onFinishFailed: onFinishFailed
    }); // 卸载时不要忘了手动清理内存 FormStore
  }, []);

  var handleSubmit = function handleSubmit(e) {
    var _a, _b;

    (_a = e === null || e === void 0 ? void 0 : e.preventDefault) === null || _a === void 0 ? void 0 : _a.call(e);
    (_b = e === null || e === void 0 ? void 0 : e.stopPropagation) === null || _b === void 0 ? void 0 : _b.call(e);
    submit();
  };

  var handleChange = function handleChange(e, name) {
    // 适配 e.target 不存在的情况(如radio 和 checkbox), 如果 e.target 不存在则组装一个对象
    var t = (e === null || e === void 0 ? void 0 : e.target) || {
      value: e
    };
    setFieldsValue(_defineProperty({}, name, t === null || t === void 0 ? void 0 : t.value));
  };

  var handleChangeError = function handleChangeError(v, name) {
    setErrors(_defineProperty({}, name, v));
  }; // 这里不用 useMemo 来优化，因为只有每次 providerValue 都是一个新的对象，子组件才会重新渲染（useContext收到更新通知）


  var providerValue = {
    formInstance: formInstance,
    onChange: handleChange,
    onChangeError: handleChangeError
  };
  return /*#__PURE__*/React.createElement(FormContext.Provider, {
    value: providerValue
  }, /*#__PURE__*/React.createElement(Form, {
    className: classNames(prefixCls),
    onSubmit: handleSubmit,
    onReset: onReset
  }, children));
};

export default AnnarForm;