UNPKG

@react-form-builder/core

Version:

React JSON Schema Form Builder to create complex, validated, reusable forms with no deep React knowledge required

281 lines (280 loc) 6.23 kB
function u(n) { return !!n?.fnSource; } function c(n) { return n?.localized === !0; } function d(n) { return typeof n?.finalize == "function"; } class f { constructor(t, e) { this.parent = t, this.eventName = e; } parent; eventName; buffer = []; current = null; /** * @inheritDoc */ commonAction(t) { return this.commit(), this.current = { type: "common", name: t }, this; } /** * @inheritDoc */ customAction(t) { return this.commit(), this.current = { type: "custom", name: t }, this; } /** * @inheritDoc */ args(t) { if (!this.current) throw new Error("Call default or custom before args"); return this.current.args = t, this; } /** * @inheritDoc */ prop = (t, e) => this.finalize().prop(t, e); /** * @inheritDoc */ localizedProp = (t, e, i) => this.finalize().localizedProp(t, e, i); /** * @inheritDoc */ computedProp = (t, e) => this.finalize().computedProp(t, e); /** * @inheritDoc */ validation = (t) => this.finalize().validation(t); /** * @inheritDoc */ event = (t) => this.finalize().event(t); /** * @inheritDoc */ component = (t, e) => this.finalize().component(t, e); /** * @inheritDoc */ json = () => this.finalize().json(); /** * @inheritDoc */ style = (t, e) => this.finalize().style(t, e); /** * @inheritDoc */ children = (t) => this.finalize().children(t); finalize() { this.commit(), this.parent.events = {}; for (const t of this.buffer) this.parent.addEvent(this.eventName, t); return this.parent; } commit() { this.current && (this.buffer.push(this.current), this.current = null); } } class m { constructor(t, e) { this.parent = t, this.key = e; } parent; key; /** * @inheritDoc */ args(t) { return this.finalize(t), this.parent; } /** * @inheritDoc */ prop = (t, e) => this.finalize().prop(t, e); /** * @inheritDoc */ localizedProp = (t, e, i) => this.finalize().localizedProp(t, e, i); /** * @inheritDoc */ computedProp = (t, e) => this.finalize().computedProp(t, e); /** * @inheritDoc */ validation = (t) => this.finalize().validation(t); /** * @inheritDoc */ event = (t) => this.finalize().event(t); /** * @inheritDoc */ component = (t, e) => this.finalize().component(t, e); /** * @inheritDoc */ json = () => this.finalize().json(); /** * @inheritDoc */ style = (t, e) => this.finalize().style(t, e); /** * @inheritDoc */ children = (t) => this.finalize().children(t); finalize(t) { return this.parent.addValidation(this.key, t), this.parent; } } class y { constructor(t, e, i) { this.parent = t, this.key = e, this.type = i; } parent; key; type; props = {}; validations = []; events = {}; css = {}; childComponents = []; /** * @inheritDoc */ prop(t, e) { return this.props[t] = { value: e }, this; } /** * @inheritDoc */ localizedProp(t, e, i) { const s = this.props[t]; return c(s) ? s.values[e] = i : this.props[t] = { localized: !0, values: { [e]: i } }, this; } /** * @inheritDoc */ computedProp(t, e) { return this.props[t] = { fnSource: e }, this; } /** * @inheritDoc */ validation(t) { return new m(this, t); } /** * @inheritDoc */ event(t) { return new f(this, t); } buildComponent() { const t = { key: this.key, type: this.type, props: this.props }; return this.validations.length && (t.schema = { validations: this.validations }), Object.keys(this.events).length && (t.events = this.events), Object.keys(this.css).length && (t.css = this.css), this.childComponents.length && (t.children = this.childComponents), t; } addEvent(t, e) { this.events[t] || (this.events[t] = []), this.events[t].push(e); } addValidation(t, e) { this.validations.push({ key: t, args: e }); } /** * @inheritDoc */ style(t, e) { const i = this.css[e ?? "any"] ??= {}; return typeof t == "object" ? (i.object = { ...i.object, ...t }, this) : (i.string = t, this); } /** * @inheritDoc */ children(t) { const e = new p(), i = t(e); return d(i) && i.finalize(), this.childComponents = [ ...this.childComponents, ...e.build().form.children ?? [] ], this; } /** * @inheritDoc */ json = () => this.parent.json(); /** * @inheritDoc */ component = (t, e) => this.parent.component(t, e); } class p { constructor(t) { this.options = t; } options; components = []; current = null; /** * @inheritDoc */ component(t, e) { return this.current && this.components.push(this.current.buildComponent()), this.current = new y(this, t, e), this.current; } /** * @inheritDoc */ json() { const t = this.build(), e = JSON.parse(JSON.stringify(t)), i = {}; return this.toComponentProperties(e.form, i), Object.keys(i).length > 0 && (e.localization = i, e.languages = this.toLanguages(i)), JSON.stringify(e); } build() { this.current && (this.components.push(this.current.buildComponent()), this.current = null); const t = { ...this.options, form: { key: "Screen", type: "Screen" } }; return this.components.length && (t.form.children = this.components), t; } toLanguages(t) { return Object.keys(t).map((e) => { const [i, s] = e.split("-"); return { code: i, dialect: s ?? i }; }); } toComponentProperties(t, e) { const i = t.props ?? {}; Object.entries(i).forEach(([r, o]) => { if (u(o)) { i[r] = { computeType: "function", fnSource: o.fnSource }; return; } c(o) && (i[r] = { computeType: "localization" }, Object.entries(o.values).forEach(([l, a]) => { const h = l; e[h] ??= {}, e[h][t.key] ??= {}, e[h][t.key].component ??= {}, e[h][t.key].component[r] = a; })); }), (t.children ?? []).forEach((r) => { this.toComponentProperties(r, e); }); } } function z(n) { return new p(n); } export { z as buildForm }; //# sourceMappingURL=FormJsonBuilder.js.map