@react-form-builder/core
Version:
React JSON Schema Form Builder to create complex, validated, reusable forms with no deep React knowledge required
145 lines (144 loc) • 4.33 kB
JavaScript
import { makeAutoObservable as r } from "mobx";
import { ActionDefinition as a } from "../features/event/ActionDefinition.js";
import { AsyncFunction as c } from "../utils/AsyncFunction.js";
import { generateUniqueName as h } from "../utils/generateUniqueName.js";
class p {
/**
* Root component of the form.
*/
componentTree;
/**
* @inheritDoc
*/
localization;
/**
* Localization languages of the form.
*/
languages = [];
/**
* The set of action definitions.
*/
actions;
/**
* Properties of the component displaying the error.
*/
errorProps = {};
/**
* The type name of the component displaying the tooltip.
*/
tooltipType;
/**
* The type name of the component displaying the error.
*/
errorType;
/**
* The type name of the component displaying the modal.
*/
modalType;
/**
* @inheritDoc
*/
defaultLanguage;
/**
* The form validator.
*/
formValidator;
/**
* Creates a new instance of Form.
* @param componentTree the root component of the form.
* @param localization the localization of the form.
* @param actions the form custom actions.
* @param languages the localization languages of the form.
* @param defaultLanguage the default localization language of the form.
*/
constructor(e, t, i, n, o) {
this.componentTree = e, this.localization = t, this.actions = i, this.languages = n, this.defaultLanguage = o, this.componentTree.events.onBeforeDelete.subscribe(this.onComponentDataBeforeDelete.bind(this)), this.componentTree.events.onAfterKeyChanged.subscribe(this.onComponentDataAfterKeyChanged.bind(this)), r(this);
}
/**
* @returns the actions names array.
*/
get actionNames() {
return Object.keys(this.actions);
}
/**
* @returns the form validator function.
*/
get formValidatorFunction() {
if (this.formValidator)
return c("formData", this.formValidator);
}
/**
* Initializes form fields.
*/
initFields() {
this.componentTree.allFields.forEach((e) => e.init());
}
/**
* Disposes the form. Disposes all the components and localization.
*/
dispose() {
this.componentTree.dispose();
}
/**
* Removes the action from the form.
* @param name the action name to remove.
*/
removeAction(e) {
delete this.actions[e], this.removeCodeActionBinding(e, this.componentTree.store);
}
/**
* Changes the existing action to the new one, adds the action if the existing action is not found.
* @param oldActionName the existing action name.
* @param newAction the new named action.
*/
updateOrAddAction(e, t) {
this.actions[t.name] = t.actionDefinition, this.rebindActionHandlers(this.componentTree.store, e, t), e !== t.name && delete this.actions[e];
}
/**
* Clones the action.
* @param namedAction the named action to clone.
*/
cloneAction(e) {
if (!this.actions) return;
const t = new Set(this.actionNames), i = JSON.parse(JSON.stringify(e.actionDefinition)), n = h(`${e.name}_`, t);
this.actions[n] = a.createFromObject(i);
}
rebindActionData(e, t) {
e.name = t.name;
const i = e.args;
if (!i) return;
const n = new Set(Object.keys(t.actionDefinition.params));
Object.keys(i).forEach((o) => {
n.has(o) || delete i[o];
});
}
rebindEvents = (e, t, i) => {
e && Object.keys(e).forEach((n) => {
e[n]?.filter((s) => s.type === "code" && s.name === t).forEach((s) => this.rebindActionData(s, i));
});
};
rebindActionHandlers(e, t, i) {
this.rebindEvents(e.events, t, i), this.rebindEvents(e.modal?.events, t, i), e.children?.forEach((n) => {
this.rebindActionHandlers(n, t, i);
});
}
removeEvents(e, t) {
e && Object.keys(e).forEach((i) => {
const n = e[i];
n.length && (e[i] = n.filter((o) => !(o.type === "code" && o.name === t)));
});
}
removeCodeActionBinding(e, t) {
this.removeEvents(t.events, e), this.removeEvents(t.modal?.events, e), t.children?.forEach((i) => this.removeCodeActionBinding(e, i));
}
onComponentDataBeforeDelete(e) {
this.localization.removeLocalization(e.key);
}
onComponentDataAfterKeyChanged(e, { oldKey: t, newKey: i }) {
this.localization.changeComponentKey(t, i);
}
}
export {
p as Form
};
//# sourceMappingURL=Form.js.map