tdesign-react
Version:
TDesign Component for React
168 lines (164 loc) • 5.63 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _slicedToArray } from '../../_chunks/dep-48805ab8.js';
import { _ as _toConsumableArray } from '../../_chunks/dep-87d110df.js';
import { _ as _createClass, a as _classCallCheck } from '../../_chunks/dep-46885238.js';
import { _ as _defineProperty } from '../../_chunks/dep-cb0a3966.js';
import { useRef, useState } from 'react';
import { l as log } from '../../_chunks/dep-b908e1fe.js';
import '../../_chunks/dep-026a4c6b.js';
import '../../_chunks/dep-eca3a3de.js';
var HOOK_MARK = "TD_FORM_INTERNAL_HOOKS";
var FormStore = /*#__PURE__*/_createClass(function FormStore(forceReRender) {
var _this = this;
_classCallCheck(this, FormStore);
_defineProperty(this, "prevStore", {});
_defineProperty(this, "store", {});
_defineProperty(this, "taskQueue", []);
_defineProperty(this, "flashQueue", function () {
_this.taskQueue.forEach(function (task) {
_this[task.name].apply(_this, _toConsumableArray(task.args));
});
_this.taskQueue = [];
});
_defineProperty(this, "getForm", function () {
return {
submit: function submit() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this.taskQueue.push({
args: args,
name: "submit"
});
},
reset: function reset() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
_this.taskQueue.push({
args: args,
name: "reset"
});
},
validate: null,
validateOnly: null,
clearValidate: function clearValidate() {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
_this.taskQueue.push({
args: args,
name: "clearValidate"
});
},
setFields: function setFields() {
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
_this.taskQueue.push({
args: args,
name: "setFields"
});
},
setFieldsValue: function setFieldsValue() {
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
args[_key5] = arguments[_key5];
}
_this.taskQueue.push({
args: args,
name: "setFieldsValue"
});
},
setValidateMessage: function setValidateMessage() {
for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
args[_key6] = arguments[_key6];
}
_this.taskQueue.push({
args: args,
name: "setValidateMessage"
});
},
getValidateMessage: function getValidateMessage() {
for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
args[_key7] = arguments[_key7];
}
_this.taskQueue.push({
args: args,
name: "getValidateMessage"
});
},
getFieldValue: null,
getFieldsValue: null,
_init: true,
store: _this.store,
getInternalHooks: _this.getInternalHooks
};
});
_defineProperty(this, "getInternalHooks", function (key) {
if (key === HOOK_MARK) {
return {
setForm: function setForm(formInstance) {
Object.keys(formInstance).forEach(function (key2) {
_this[key2] = formInstance[key2];
});
},
flashQueue: _this.flashQueue,
notifyWatch: _this.notifyWatch,
registerWatch: _this.registerWatch,
getPrevStore: function getPrevStore() {
return _this.prevStore;
},
setPrevStore: function setPrevStore(store) {
_this.prevStore = store;
}
};
}
log.warn("Form", "`getInternalHooks` is internal usage. Should not call directly.");
return null;
});
_defineProperty(this, "watchList", []);
_defineProperty(this, "registerWatch", function (callback) {
_this.watchList.push(callback);
return function () {
_this.watchList = _this.watchList.filter(function (fn) {
return fn !== callback;
});
};
});
_defineProperty(this, "notifyWatch", function () {
var namePath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
if (_this.watchList.length) {
var _this$getFieldsValue;
var values = (_this$getFieldsValue = _this.getFieldsValue) === null || _this$getFieldsValue === void 0 ? void 0 : _this$getFieldsValue.call(_this, [namePath]);
_this.watchList.forEach(function (callback) {
callback(values, namePath);
});
}
});
this.forceRootUpdate = forceReRender;
});
function useForm(form) {
var formRef = useRef(/* @__PURE__ */Object.create({}));
var _useState = useState({}),
_useState2 = _slicedToArray(_useState, 2),
forceUpdate = _useState2[1];
if (!formRef.current._init) {
if (form) {
formRef.current = form;
formRef.current.store = {};
} else {
var forceReRender = function forceReRender() {
forceUpdate({});
};
var formStore = new FormStore(forceReRender);
formRef.current = formStore.getForm();
}
}
return [formRef.current];
}
export { HOOK_MARK, useForm as default };
//# sourceMappingURL=useForm.js.map