PromoCursed/node_modules/rc-field-form/lib/FormContext.js
2024-08-20 23:25:37 +04:00

64 lines
2.8 KiB
JavaScript

"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.FormProvider = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var React = _interopRequireWildcard(require("react"));
var FormContext = /*#__PURE__*/React.createContext({
triggerFormChange: function triggerFormChange() {},
triggerFormFinish: function triggerFormFinish() {},
registerForm: function registerForm() {},
unregisterForm: function unregisterForm() {}
});
var FormProvider = exports.FormProvider = function FormProvider(_ref) {
var validateMessages = _ref.validateMessages,
onFormChange = _ref.onFormChange,
onFormFinish = _ref.onFormFinish,
children = _ref.children;
var formContext = React.useContext(FormContext);
var formsRef = React.useRef({});
return /*#__PURE__*/React.createElement(FormContext.Provider, {
value: (0, _objectSpread3.default)((0, _objectSpread3.default)({}, formContext), {}, {
validateMessages: (0, _objectSpread3.default)((0, _objectSpread3.default)({}, formContext.validateMessages), validateMessages),
// =========================================================
// = Global Form Control =
// =========================================================
triggerFormChange: function triggerFormChange(name, changedFields) {
if (onFormChange) {
onFormChange(name, {
changedFields: changedFields,
forms: formsRef.current
});
}
formContext.triggerFormChange(name, changedFields);
},
triggerFormFinish: function triggerFormFinish(name, values) {
if (onFormFinish) {
onFormFinish(name, {
values: values,
forms: formsRef.current
});
}
formContext.triggerFormFinish(name, values);
},
registerForm: function registerForm(name, form) {
if (name) {
formsRef.current = (0, _objectSpread3.default)((0, _objectSpread3.default)({}, formsRef.current), {}, (0, _defineProperty2.default)({}, name, form));
}
formContext.registerForm(name, form);
},
unregisterForm: function unregisterForm(name) {
var newForms = (0, _objectSpread3.default)({}, formsRef.current);
delete newForms[name];
formsRef.current = newForms;
formContext.unregisterForm(name);
}
})
}, children);
};
var _default = exports.default = FormContext;