UNPKG

@xraph/smartform-react

Version:

React library for rendering and validating SmartForms with shadcn components

508 lines (507 loc) 15.3 kB
import ae, { useMemo as C, createContext as ie, useContext as ce } from "react"; const W = { debug: 0, info: 1, warn: 2, error: 3 }; class N { /** * Create a new logger instance * * @param config Logger configuration or boolean to enable/disable * @param componentName Optional component name for context */ constructor(r = !0, t) { typeof r == "boolean" ? this.config = { enabled: r, level: "debug", showTimestamp: !0 } : this.config = r, this.componentName = t; } /** * Create a new logger instance for a specific component * * @param componentName Component name * @returns A new logger instance with the same config */ forComponent(r) { return new N(this.config, r); } /** * Format a message with optional prefix, timestamp, and component name * * @param message The message to format * @returns Formatted message */ formatMessage(r) { const t = []; return this.config.showTimestamp && t.push(`[${(/* @__PURE__ */ new Date()).toISOString()}]`), this.config.prefix && t.push(`[${this.config.prefix}]`), this.componentName && t.push(`[${this.componentName}]`), t.push(r), t.join(" "); } /** * Check if a log level should be displayed * * @param level The log level to check * @returns Whether the log should be displayed */ shouldLog(r) { return this.config.enabled && W[r] >= W[this.config.level]; } /** * Log a debug message * * @param message Message or object to log * @param args Additional arguments to log */ debug(r, ...t) { this.shouldLog("debug") && (typeof r == "string" ? console.debug(this.formatMessage(r), ...t) : console.debug(this.formatMessage("Debug:"), r, ...t)); } /** * Log a debug message * * @param message Message or object to log * @param args Additional arguments to log */ log(r, ...t) { this.debug(r, ...t); } /** * Log an info message * * @param message Message or object to log * @param args Additional arguments to log */ info(r, ...t) { this.shouldLog("info") && (typeof r == "string" ? console.info(this.formatMessage(r), ...t) : console.info(this.formatMessage("Info:"), r, ...t)); } /** * Log a warning message * * @param message Message or object to log * @param args Additional arguments to log */ warn(r, ...t) { this.shouldLog("warn") && (typeof r == "string" ? console.warn(this.formatMessage(r), ...t) : console.warn(this.formatMessage("Warning:"), r, ...t)); } /** * Log an error message * * @param message Message or object to log * @param args Additional arguments to log */ error(r, ...t) { this.shouldLog("error") && (typeof r == "string" ? console.error(this.formatMessage(r), ...t) : console.error(this.formatMessage("Error:"), r, ...t)); } /** * Log a message with a group * * @param groupName Name of the group * @param callback Function to execute within the group */ group(r, t) { this.config.enabled && (console.group(this.formatMessage(r)), t(), console.groupEnd()); } /** * Log a collapsible group * * @param groupName Name of the group * @param callback Function to execute within the group */ groupCollapsed(r, t) { this.config.enabled && (console.groupCollapsed(this.formatMessage(r)), t(), console.groupEnd()); } /** * Log an object as a table * * @param data Data to display as a table * @param columns Optional column names to include */ table(r, t) { this.config.enabled && console.table(r, t); } /** * Start a timer for performance measurement * * @param label Timer label */ time(r) { this.config.enabled && console.time(this.formatMessage(r)); } /** * End a timer and log the duration * * @param label Timer label */ timeEnd(r) { this.config.enabled && console.timeEnd(this.formatMessage(r)); } } const ue = /at ([A-Za-z0-9_$]+) \(/, le = /at (([A-Z][A-Za-z0-9_$]*)[.:]([a-zA-Z0-9_$]+))/, fe = /\(([^)]+)\)/; function de() { var t, d, a; const r = ((t = new Error("I'm an error. Who's calling me?").stack) == null ? void 0 : t.split(` `)) || []; for (let i = 3; i < r.length; i++) { const c = ((d = r[i]) == null ? void 0 : d.trim()) || ""; if (c.includes("node_modules/") || c.includes("internal/")) continue; const u = c.match(le); if (u) return `${u[2]}.${u[3]}`; const p = c.match(ue); if (p != null && p.length && p[1]) { const m = p[1]; if (m !== "Module" && m !== "Object" && m !== "Function" && m !== "eval" && m !== "process" && m !== "<anonymous>" && !m.startsWith("__")) return m; } const b = c.match(fe); if (b != null && b.length && b[1]) { const v = (a = b[1].split("/").at(-1)) == null ? void 0 : a.split(":")[0]; if (v) return v.replace(/\.[^.]+$/, ""); } } } const z = process.env.NODE_ENV === "production", me = { enabled: !z, // Disabled in production by default level: "debug", showTimestamp: !0, extendedInfo: !z }; class ge extends N { constructor(r = !0) { super(r); } withContext() { const r = de() || "unknown"; return this.forComponent(r); } } const k = new ge(me); function he() { var d, a; const r = ((d = new Error().stack) == null ? void 0 : d.split(` `)) || [], t = /at ([A-Z][a-zA-Z0-9]*)[\s.]/; for (let i = 2; i < r.length; i++) { const c = (a = r[i]) == null ? void 0 : a.match(t); if (c != null && c.length && c[1]) { const u = c[1]; if (u !== "Module" && u !== "Object" && u !== "Function" && u !== "eval" && !u.startsWith("use")) return u; } } } function Re(s) { return C(() => { const r = s || he() || "UnknownComponent"; return k.forComponent(r); }, [s]); } var w = { exports: {} }, _ = {}; /** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var U; function pe() { if (U) return _; U = 1; var s = Symbol.for("react.transitional.element"), r = Symbol.for("react.fragment"); function t(d, a, i) { var c = null; if (i !== void 0 && (c = "" + i), a.key !== void 0 && (c = "" + a.key), "key" in a) { i = {}; for (var u in a) u !== "key" && (i[u] = a[u]); } else i = a; return a = i.ref, { $$typeof: s, type: d, key: c, ref: a !== void 0 ? a : null, props: i }; } return _.Fragment = r, _.jsx = t, _.jsxs = t, _; } var R = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var V; function be() { return V || (V = 1, process.env.NODE_ENV !== "production" && function() { function s(e) { if (e == null) return null; if (typeof e == "function") return e.$$typeof === ne ? null : e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case x: return "Fragment"; case X: return "Profiler"; case G: return "StrictMode"; case K: return "Suspense"; case ee: return "SuspenseList"; case te: return "Activity"; } if (typeof e == "object") switch (typeof e.tag == "number" && console.error( "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue." ), e.$$typeof) { case Z: return "Portal"; case H: return (e.displayName || "Context") + ".Provider"; case B: return (e._context.displayName || "Context") + ".Consumer"; case Q: var n = e.render; return e = e.displayName, e || (e = n.displayName || n.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e; case re: return n = e.displayName || null, n !== null ? n : s(e.type) || "Memo"; case L: n = e._payload, e = e._init; try { return s(e(n)); } catch { } } return null; } function r(e) { return "" + e; } function t(e) { try { r(e); var n = !1; } catch { n = !0; } if (n) { n = console; var o = n.error, l = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object"; return o.call( n, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", l ), r(e); } } function d(e) { if (e === x) return "<>"; if (typeof e == "object" && e !== null && e.$$typeof === L) return "<...>"; try { var n = s(e); return n ? "<" + n + ">" : "<...>"; } catch { return "<...>"; } } function a() { var e = O.A; return e === null ? null : e.getOwner(); } function i() { return Error("react-stack-top-frame"); } function c(e) { if (M.call(e, "key")) { var n = Object.getOwnPropertyDescriptor(e, "key").get; if (n && n.isReactWarning) return !1; } return e.key !== void 0; } function u(e, n) { function o() { $ || ($ = !0, console.error( "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)", n )); } o.isReactWarning = !0, Object.defineProperty(e, "key", { get: o, configurable: !0 }); } function p() { var e = s(this.type); return Y[e] || (Y[e] = !0, console.error( "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release." )), e = this.props.ref, e !== void 0 ? e : null; } function b(e, n, o, l, h, g, A, S) { return o = g.ref, e = { $$typeof: y, type: e, key: n, props: g, _owner: h }, (o !== void 0 ? o : null) !== null ? Object.defineProperty(e, "ref", { enumerable: !1, get: p }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: 0 }), Object.defineProperty(e, "_debugInfo", { configurable: !1, enumerable: !1, writable: !0, value: null }), Object.defineProperty(e, "_debugStack", { configurable: !1, enumerable: !1, writable: !0, value: A }), Object.defineProperty(e, "_debugTask", { configurable: !1, enumerable: !1, writable: !0, value: S }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e; } function m(e, n, o, l, h, g, A, S) { var f = n.children; if (f !== void 0) if (l) if (oe(f)) { for (l = 0; l < f.length; l++) v(f[l]); Object.freeze && Object.freeze(f); } else console.error( "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead." ); else v(f); if (M.call(n, "key")) { f = s(e); var E = Object.keys(n).filter(function(se) { return se !== "key"; }); l = 0 < E.length ? "{key: someKey, " + E.join(": ..., ") + ": ...}" : "{key: someKey}", F[f + l] || (E = 0 < E.length ? "{" + E.join(": ..., ") + ": ...}" : "{}", console.error( `A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, l, f, E, f ), F[f + l] = !0); } if (f = null, o !== void 0 && (t(o), f = "" + o), c(n) && (t(n.key), f = "" + n.key), "key" in n) { o = {}; for (var j in n) j !== "key" && (o[j] = n[j]); } else o = n; return f && u( o, typeof e == "function" ? e.displayName || e.name || "Unknown" : e ), b( e, f, g, h, a(), o, A, S ); } function v(e) { typeof e == "object" && e !== null && e.$$typeof === y && e._store && (e._store.validated = 1); } var T = ae, y = Symbol.for("react.transitional.element"), Z = Symbol.for("react.portal"), x = Symbol.for("react.fragment"), G = Symbol.for("react.strict_mode"), X = Symbol.for("react.profiler"), B = Symbol.for("react.consumer"), H = Symbol.for("react.context"), Q = Symbol.for("react.forward_ref"), K = Symbol.for("react.suspense"), ee = Symbol.for("react.suspense_list"), re = Symbol.for("react.memo"), L = Symbol.for("react.lazy"), te = Symbol.for("react.activity"), ne = Symbol.for("react.client.reference"), O = T.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, M = Object.prototype.hasOwnProperty, oe = Array.isArray, P = console.createTask ? console.createTask : function() { return null; }; T = { "react-stack-bottom-frame": function(e) { return e(); } }; var $, Y = {}, I = T["react-stack-bottom-frame"].bind( T, i )(), D = P(d(i)), F = {}; R.Fragment = x, R.jsx = function(e, n, o, l, h) { var g = 1e4 > O.recentlyCreatedOwnerStacks++; return m( e, n, o, !1, l, h, g ? Error("react-stack-top-frame") : I, g ? P(d(e)) : D ); }, R.jsxs = function(e, n, o, l, h) { var g = 1e4 > O.recentlyCreatedOwnerStacks++; return m( e, n, o, !0, l, h, g ? Error("react-stack-top-frame") : I, g ? P(d(e)) : D ); }; }()), R; } var q; function Ee() { return q || (q = 1, process.env.NODE_ENV === "production" ? w.exports = pe() : w.exports = be()), w.exports; } var ve = Ee(); const J = ie(void 0); function Te({ initialConfig: s, children: r }) { C(() => { s && Object.assign(k.config, s); }, [s]); const t = (a) => { Object.assign(k.config, a); }, d = C(() => ({ logger: k, updateConfig: t }), []); return /* @__PURE__ */ ve.jsx(J.Provider, { value: d, children: r }); } function we() { const s = ce(J); if (s === void 0) throw new Error("useLoggerContext must be used within a LoggerProvider"); return s; } export { W as LOG_LEVELS, k as Logger, Te as LoggerProvider, ge as ReactLoggerWithContext, Re as useLogger, we as useLoggerContext };