ayongui
Version:
96 lines (95 loc) • 2.98 kB
JavaScript
var o = Object.defineProperty;
var a = (s, t, i) => t in s ? o(s, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : s[t] = i;
var e = (s, t, i) => (a(s, typeof t != "symbol" ? t + "" : t, i), i);
import { useRef as u } from "react";
import { isObject as n } from "../../utils/index.mjs";
class h {
constructor() {
e(this, "store");
e(this, "callbacks");
e(this, "initialValues");
e(this, "fieldEntities");
e(this, "update");
e(this, "updateValue");
e(this, "formSubmit");
e(this, "errorInfo");
e(this, "getForm", () => ({
getFieldValue: this.getFieldValue,
setFieldsValue: this.setFieldsValue,
submit: this.submit,
resetFields: this.resetFields,
getInternalHooks: this.getInternalHooks,
verify: this.verify
}));
e(this, "verify", (t, i, r) => {
});
e(this, "getInternalHooks", (t) => {
const { update: i, submit: r, errorInfo: l } = t;
return i && r && l && (this.formSubmit = r, this.errorInfo = l, this.updateValue = i), {
updateValue: this.updateValue,
setInitialValues: this.setInitialValues,
setCallbacks: this.setCallbacks,
initEntityValue: this.initEntityValue,
registerField: this.registerField,
getFieldValue: this.getFieldValue
};
});
e(this, "submit", () => {
var t, i;
if (this.formSubmit) {
if (this.formSubmit(), (i = (t = this.errorInfo) == null ? void 0 : t.errorFields) != null && i.length)
throw new Error(JSON.stringify(this.errorInfo.errorFields));
return this.store;
}
});
// 获取单个字段的值
e(this, "getFieldValue", (t) => this.store[t]);
// 设置单个字段的值
e(this, "setFieldsValue", (t) => {
this.store = { ...this.store, ...t }, this.updateValue && this.updateValue(this.store, "set");
});
e(this, "setCallbacks", (t) => {
});
// 设置初始值
e(this, "setInitialValues", (t) => {
this.store = { ...t };
});
e(this, "initEntityValue", (t) => {
});
e(this, "registerField", (t) => {
});
e(this, "getFieldEntities", () => {
});
e(this, "notifyObservers", (t, i, r) => {
});
// 刷新所有字段的值
e(this, "resetFields", () => {
for (let t in this.store)
if (n(this.store[t]))
for (let i in this.store[t])
this.store[t][i] = "";
else
this.store[t] = "";
this.updateValue(this.store, "reset");
});
this.store = {}, this.callbacks = /* @__PURE__ */ Object.create(null), this.initialValues = {}, this.fieldEntities = [], this.update = () => {
}, this.updateValue = () => {
}, this.formSubmit = () => {
};
}
}
const V = (s) => {
const t = u();
if (!t.current)
if (s)
t.current = s;
else {
const i = new h();
t.current = i.getForm();
}
return [t.current];
};
export {
h as FormStore,
V as useForm
};