UNPKG

react-vmodel

Version:

a vue v-model like usage for react with ts prompt

414 lines (413 loc) 12.9 kB
import ce, { useState as le } from "react"; var Y = { exports: {} }, P = {}; /** * @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 oe() { if (U) return P; U = 1; var r = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment"); function n(c, l, s) { var T = null; if (s !== void 0 && (T = "" + s), l.key !== void 0 && (T = "" + l.key), "key" in l) { s = {}; for (var f in l) f !== "key" && (s[f] = l[f]); } else s = l; return l = s.ref, { $$typeof: r, type: c, key: T, ref: l !== void 0 ? l : null, props: s }; } return P.Fragment = t, P.jsx = n, P.jsxs = n, P; } var S = {}; /** * @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 z; function se() { return z || (z = 1, process.env.NODE_ENV !== "production" && function() { function r(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 h: return "Fragment"; case g: return "Profiler"; case k: 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 x: return "Portal"; case Z: return (e.displayName || "Context") + ".Provider"; case A: return (e._context.displayName || "Context") + ".Consumer"; case Q: var u = e.render; return e = e.displayName, e || (e = u.displayName || u.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e; case re: return u = e.displayName || null, u !== null ? u : r(e.type) || "Memo"; case D: u = e._payload, e = e._init; try { return r(e(u)); } catch { } } return null; } function t(e) { return "" + e; } function n(e) { try { t(e); var u = !1; } catch { u = !0; } if (u) { u = console; var i = u.error, m = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object"; return i.call( u, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", m ), t(e); } } function c(e) { if (e === h) return "<>"; if (typeof e == "object" && e !== null && e.$$typeof === D) return "<...>"; try { var u = r(e); return u ? "<" + u + ">" : "<...>"; } catch { return "<...>"; } } function l() { var e = w.A; return e === null ? null : e.getOwner(); } function s() { return Error("react-stack-top-frame"); } function T(e) { if (F.call(e, "key")) { var u = Object.getOwnPropertyDescriptor(e, "key").get; if (u && u.isReactWarning) return !1; } return e.key !== void 0; } function f(e, u) { function i() { I || (I = !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)", u )); } i.isReactWarning = !0, Object.defineProperty(e, "key", { get: i, configurable: !0 }); } function a() { var e = r(this.type); return M[e] || (M[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 o(e, u, i, m, R, y, N, p) { return i = y.ref, e = { $$typeof: b, type: e, key: u, props: y, _owner: R }, (i !== void 0 ? i : null) !== null ? Object.defineProperty(e, "ref", { enumerable: !1, get: a }) : 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: N }), Object.defineProperty(e, "_debugTask", { configurable: !1, enumerable: !1, writable: !0, value: p }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e; } function d(e, u, i, m, R, y, N, p) { var v = u.children; if (v !== void 0) if (m) if (ae(v)) { for (m = 0; m < v.length; m++) _(v[m]); Object.freeze && Object.freeze(v); } 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); if (F.call(u, "key")) { v = r(e); var O = Object.keys(u).filter(function(ue) { return ue !== "key"; }); m = 0 < O.length ? "{key: someKey, " + O.join(": ..., ") + ": ...}" : "{key: someKey}", W[v + m] || (O = 0 < O.length ? "{" + O.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} />`, m, v, O, v ), W[v + m] = !0); } if (v = null, i !== void 0 && (n(i), v = "" + i), T(u) && (n(u.key), v = "" + u.key), "key" in u) { i = {}; for (var C in u) C !== "key" && (i[C] = u[C]); } else i = u; return v && f( i, typeof e == "function" ? e.displayName || e.name || "Unknown" : e ), o( e, v, y, R, l(), i, N, p ); } function _(e) { typeof e == "object" && e !== null && e.$$typeof === b && e._store && (e._store.validated = 1); } var E = ce, b = Symbol.for("react.transitional.element"), x = Symbol.for("react.portal"), h = Symbol.for("react.fragment"), k = Symbol.for("react.strict_mode"), g = Symbol.for("react.profiler"), A = Symbol.for("react.consumer"), Z = 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"), D = Symbol.for("react.lazy"), te = Symbol.for("react.activity"), ne = Symbol.for("react.client.reference"), w = E.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, F = Object.prototype.hasOwnProperty, ae = Array.isArray, j = console.createTask ? console.createTask : function() { return null; }; E = { "react-stack-bottom-frame": function(e) { return e(); } }; var I, M = {}, $ = E["react-stack-bottom-frame"].bind( E, s )(), L = j(c(s)), W = {}; S.Fragment = h, S.jsx = function(e, u, i, m, R) { var y = 1e4 > w.recentlyCreatedOwnerStacks++; return d( e, u, i, !1, m, R, y ? Error("react-stack-top-frame") : $, y ? j(c(e)) : L ); }, S.jsxs = function(e, u, i, m, R) { var y = 1e4 > w.recentlyCreatedOwnerStacks++; return d( e, u, i, !0, m, R, y ? Error("react-stack-top-frame") : $, y ? j(c(e)) : L ); }; }()), S; } process.env.NODE_ENV === "production" ? Y.exports = oe() : Y.exports = se(); var q = Y.exports; function J(r) { const n = String(r).replaceAll('"', "").replaceAll("'", "").split("."), c = []; return n.forEach((l) => { l.replaceAll("]", "").split("[").forEach((s) => { s !== "" && c.push(s); }); }), c; } function X(r, t) { if (t === void 0) return r; if (!(typeof r == "object" && r !== null)) return; let n = []; typeof t == "string" || typeof t == "number" || typeof t == "symbol" ? n = J(t) : n = t; const c = n.shift(); return c !== void 0 ? n.length ? X(r[c], n) : r[c] : r; } const fe = (r) => Array.isArray(r) ? [...r] : typeof r == "object" ? { ...r } : r; function B(r, t, n) { if (t === void 0) return r; if (!(typeof r == "object" && r !== null)) return; let c = []; typeof t == "string" || typeof t == "number" || typeof t == "symbol" ? c = J(t) : c = t; const l = c.shift(), s = fe(r); return l !== void 0 && (c.length ? s[l] = B(r[l], c, n) : s[l] = n), s; } function ie(r, t) { return X(r, t); } function de(r, t, n) { return B(r, t, n); } const G = (r) => { let t = !0, n = !1; return r && ("trueValue" in r ? t = r.trueValue : "value" in r && (t = r.value), "falseValue" in r && (n = r.falseValue)), { trueValue: t, falseValue: n }; }, V = (...r) => { let t, n, c; for (let l = 0; l < r.length; l++) { const s = r[l]; !t && typeof s == "string" ? t = s : !n && typeof s == "object" ? n = s : !c && typeof s == "function" && (c = s); } return c || (c = (l, s) => ({ value: l, onChange: s })), { key: t, options: n, transform: c }; }, H = (r, t) => { const n = (f, a) => { const o = ie(r, f); return a != null && a.getter ? a.getter(o) : o; }, c = (f, a) => (o) => { var b, x, h; if (t === void 0) return; let d = o != null && o.target ? o.target.value : o; const _ = n(f), E = (b = o == null ? void 0 : o.target) == null ? void 0 : b.type; if (a != null && a.checklist) { const k = Array.isArray(_) ? _.slice() : [], g = k.indexOf(a == null ? void 0 : a.value), A = !a.checked; g !== -1 ? A || k.splice(g, 1) : A && k.push(a == null ? void 0 : a.value), d = k; } else if ( // inputType === "checkbox" || // inputType === "radio" || a != null && a.checkbox ) { const k = ((x = o.target) == null ? void 0 : x.checked) ?? ((h = o.target) == null ? void 0 : h.value) ?? o ?? !(a != null && a.checked), { trueValue: g, falseValue: A } = G(a); d = k ? g : A; } if (typeof d == "string" && (a != null && a.trim) && (d = d.trim()), (E === "number" || a != null && a.parseNumber) && (d = Number(d)), f !== void 0) { const k = de(r, f, d); t(k); } else t(d); }; function l(...f) { const { key: a, options: o, transform: d } = V(...f), _ = n(a, o), E = c(a, o); return d(_, E); } l.number = (f) => ({ value: n(f), onChange: c(f, { parseNumber: !0 }) }), l.trim = (f) => ({ value: n(f), onChange: c(f, { trim: !0 }) }); function s(...f) { const { key: a, options: o } = V(...f), d = n(a), { trueValue: _ } = G(o), E = d === _, b = { checked: E, onChange: c(a, { ...o, checked: E, checkbox: !0 }) }; return { ...b, value: (o == null ? void 0 : o.value) ?? b.checked }; } l.checked = s; function T(...f) { const { key: a, options: o } = V(...f), d = n(a); let _ = !1; const E = o == null ? void 0 : o.value; return Array.isArray(d) && (_ = !!d.find((b) => b === E)), { value: E, checked: _, onChange: c(a, { ...o, checked: _, checklist: !0 }) }; } return l.checklist = T, l; }, ve = (r) => { const [t, n] = le(r); return [H(t, n), t, n]; }; function Ee({ children: r, value: t, onChange: n }) { const c = H(t, n); return /* @__PURE__ */ q.jsx(q.Fragment, { children: r(c, t, (l) => { n && n(l); }) }); } export { Ee as Model, H as makeVModel, ve as useVModel };