UNPKG

@ndriadev/react-tools

Version:

A React library of hooks, components, utils and types ready to use

98 lines (97 loc) 3.77 kB
var z = Object.defineProperty; var k = (o, r, t) => r in o ? z(o, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[r] = t; var g = (o, r, t) => (k(o, typeof r != "symbol" ? r + "" : r, t), t); import { j as n } from "./jsx-runtime-jRCOe3WA.js"; import { Suspense as v, memo as l, Children as b, isValidElement as f, cloneElement as w, Component as M } from "react"; const F = [], D = ({ factory: o, componentName: r, beforeLoad: t, afterLoad: i }) => { const h = o.toString(); for (const s of F) if (s.factorySerialized === h) { if (s.response) return i && i(), s.response && s.response(); throw s.error ? s.error : s.promise; } const e = { factorySerialized: String.raw`${h}`, promise: (t && t(), o().then((s) => { "default" in s && (e.response = s.default), r && r in s && (e.response = s[r]); const a = Reflect.ownKeys(s).filter((c) => c !== "__esModule"); throw a.length > 0 && (e.response = s[a[0]]), Error("Invalid import"); }).catch((s) => e.error = s)) }; throw F.push(e), e.promise; }, V = ({ factory: o, componentName: r, fallback: t, beforeLoad: i, afterLoad: h }) => n.jsx(v, { fallback: t, children: n.jsx(D, { factory: o, componentName: r, beforeLoad: i, afterLoad: h }) }); function R({ when: o, fallback: r, children: t }) { return o ? n.jsx(n.Fragment, { children: t }) : r ? n.jsx(n.Fragment, { children: r }) : null; } const $ = l(R), C = ({ children: o, when: r }) => r ? n.jsx(n.Fragment, { children: o }) : null, x = ({ children: o, fallback: r }) => { const t = b.toArray(o).find((i) => f(i) && i.props.when); return t ? n.jsx(n.Fragment, { children: t }) : r !== void 0 ? n.jsxs(n.Fragment, { children: [ " ", r ] }) : null; }, q = { Switch: x, Case: C }, G = { Switch: l(x), Case: l(C) }; function E({ of: o, elementKey: r, fallback: t, filter: i, sort: h, map: e, children: s }) { if (o.length === 0) return t !== void 0 ? n.jsx(n.Fragment, { children: t }) : null; let a = h ? h === !0 ? o.sort() : o.sort(h) : o; return a = i ? a.filter(i) : a, a.map((c, u, y) => { if (e !== void 0) { const p = e(c, u, y), j = r ? typeof r == "function" ? r(p) : typeof p == "object" && p !== null && r in p ? p[r] : r : u, S = s(p, u, j); return f(S) ? w( S, { key: j } ) : null; } const d = r ? typeof r == "function" ? r(c) : typeof c == "object" && c !== null && r in c ? c[r] : r : u, m = s(c, u, d); return f(m) ? w( m, { key: d } ) : null; }); } E.displayName = "For"; const _ = l(E); _.displayName = "ForMemoized"; class H extends M { constructor() { super(...arguments); g(this, "state", { hasError: !1 }); } static getDerivedStateFromError(t) { return { hasError: !0, error: t }; } componentDidCatch(t, i) { this.props.onCatch && this.props.onCatch(t, i), this.setState({ hasError: !0, error: t, info: i }); } retry() { this.setState({ hasError: !1, error: void 0, info: void 0 }); } render() { if (this.state.hasError && this.props.fallback) { if (typeof this.props.fallback == "function") if (f(this.props.fallback)) { const t = this.props.fallback; return n.jsx(t, { error: this.state.error, info: this.state.info, retry: this.retry.bind(this) }); } else return n.jsx(n.Fragment, { children: this.props.fallback(this.state.error, this.state.info, this.retry.bind(this)) }); return n.jsx(n.Fragment, { children: this.props.fallback }); } return this.props.children; } } export { H as ErrorBoundary, E as For, _ as ForMemoized, V as LazyComponent, R as Show, $ as ShowMemoized, q as SwitchCase, G as SwitchCaseMemoized };