UNPKG

@react-bunker/input-mask

Version:

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

458 lines (457 loc) 15.1 kB
var se = Object.defineProperty; var oe = (u, t, r) => t in u ? se(u, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : u[t] = r; var q = (u, t, r) => oe(u, typeof t != "symbol" ? t + "" : t, r); import ce, { useMemo as C, useRef as ie, useEffect as le, useImperativeHandle as fe, createRef as me } from "react"; var A = { exports: {} }, h = {}; /** * @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 de() { if (U) return h; U = 1; var u = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment"); function r(n, a, o) { var i = null; if (o !== void 0 && (i = "" + o), a.key !== void 0 && (i = "" + a.key), "key" in a) { o = {}; for (var l in a) l !== "key" && (o[l] = a[l]); } else o = a; return a = o.ref, { $$typeof: u, type: n, key: i, ref: a !== void 0 ? a : null, props: o }; } return h.Fragment = t, h.jsx = r, h.jsxs = r, h; } var T = {}; /** * @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 D; function pe() { return D || (D = 1, process.env.NODE_ENV !== "production" && function() { function u(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 j: 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 y: return "Portal"; case H: return (e.displayName || "Context") + ".Provider"; case B: return (e._context.displayName || "Context") + ".Consumer"; case Q: var s = e.render; return e = e.displayName, e || (e = s.displayName || s.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e; case re: return s = e.displayName || null, s !== null ? s : u(e.type) || "Memo"; case M: s = e._payload, e = e._init; try { return u(e(s)); } catch { } } return null; } function t(e) { return "" + e; } function r(e) { try { t(e); var s = !1; } catch { s = !0; } if (s) { s = console; var c = s.error, f = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object"; return c.call( s, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", f ), t(e); } } function n(e) { if (e === j) return "<>"; if (typeof e == "object" && e !== null && e.$$typeof === M) return "<...>"; try { var s = u(e); return s ? "<" + s + ">" : "<...>"; } catch { return "<...>"; } } function a() { var e = O.A; return e === null ? null : e.getOwner(); } function o() { return Error("react-stack-top-frame"); } function i(e) { if (I.call(e, "key")) { var s = Object.getOwnPropertyDescriptor(e, "key").get; if (s && s.isReactWarning) return !1; } return e.key !== void 0; } function l(e, s) { function c() { Y || (Y = !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)", s )); } c.isReactWarning = !0, Object.defineProperty(e, "key", { get: c, configurable: !0 }); } function v() { var e = u(this.type); return F[e] || (F[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 g(e, s, c, f, p, d, x, P) { return c = d.ref, e = { $$typeof: k, type: e, key: s, props: d, _owner: p }, (c !== void 0 ? c : null) !== null ? Object.defineProperty(e, "ref", { enumerable: !1, get: v }) : 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: x }), Object.defineProperty(e, "_debugTask", { configurable: !1, enumerable: !1, writable: !0, value: P }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e; } function S(e, s, c, f, p, d, x, P) { var m = s.children; if (m !== void 0) if (f) if (ae(m)) { for (f = 0; f < m.length; f++) _(m[f]); Object.freeze && Object.freeze(m); } 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 _(m); if (I.call(s, "key")) { m = u(e); var R = Object.keys(s).filter(function(ue) { return ue !== "key"; }); f = 0 < R.length ? "{key: someKey, " + R.join(": ..., ") + ": ...}" : "{key: someKey}", $[m + f] || (R = 0 < R.length ? "{" + R.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} />`, f, m, R, m ), $[m + f] = !0); } if (m = null, c !== void 0 && (r(c), m = "" + c), i(s) && (r(s.key), m = "" + s.key), "key" in s) { c = {}; for (var N in s) N !== "key" && (c[N] = s[N]); } else c = s; return m && l( c, typeof e == "function" ? e.displayName || e.name || "Unknown" : e ), g( e, m, d, p, a(), c, x, P ); } function _(e) { typeof e == "object" && e !== null && e.$$typeof === k && e._store && (e._store.validated = 1); } var E = ce, k = Symbol.for("react.transitional.element"), y = Symbol.for("react.portal"), j = 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"), M = Symbol.for("react.lazy"), te = Symbol.for("react.activity"), ne = Symbol.for("react.client.reference"), O = E.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, I = Object.prototype.hasOwnProperty, ae = Array.isArray, w = console.createTask ? console.createTask : function() { return null; }; E = { "react-stack-bottom-frame": function(e) { return e(); } }; var Y, F = {}, z = E["react-stack-bottom-frame"].bind( E, o )(), L = w(n(o)), $ = {}; T.Fragment = j, T.jsx = function(e, s, c, f, p) { var d = 1e4 > O.recentlyCreatedOwnerStacks++; return S( e, s, c, !1, f, p, d ? Error("react-stack-top-frame") : z, d ? w(n(e)) : L ); }, T.jsxs = function(e, s, c, f, p) { var d = 1e4 > O.recentlyCreatedOwnerStacks++; return S( e, s, c, !0, f, p, d ? Error("react-stack-top-frame") : z, d ? w(n(e)) : L ); }; }()), T; } var W; function ve() { return W || (W = 1, process.env.NODE_ENV === "production" ? A.exports = de() : A.exports = pe()), A.exports; } var be = ve(); function Ee(u) { const t = /* @__PURE__ */ new Map(); return (...r) => { const n = JSON.stringify(r); if (t.has(n)) return t.get(n); const a = u(...r); return t.set(n, a), a; }; } const Re = Ee((u, t) => { const r = (n, a) => { if (!t.includes(a)) return n; n.values[a] && n.values[a]++, n.values[a] || (n.values[a] = 1); const o = Math.max(...Object.values(n.values)); return o > n.qty && (n.qty = o, n.value = a), n; }; return u.reduce(r, { qty: 0, value: "", values: {} }); }), V = { currency: (u) => { const t = new Intl.NumberFormat((u == null ? void 0 : u.locale) ?? "pt-br", { currency: "BRL", style: "currency", ...u }), n = t.formatToParts(0).find((i) => i.type === "fraction"), a = n != null && n.value ? (n == null ? void 0 : n.value.length) + 1 : 0, o = "1".concat(Array.from({ length: a }).join("0")); return { set(i) { const l = Number(i); return Number.isNaN(l) ? i : t.format(l); }, clear: (i) => { const l = Number(i.replace(/\D/g, "")) / Number(o); return String(l); } }; }, noSpace: { set: (u) => u.replace(/\s+/, ""), clear: (u) => u.replace(/\s+/, "") }, noSpecialCharacter: { set: (u) => u.replace(/[^A-Za-zÀ-úÜüÖöäÄëï'0-9-. ]/g, ""), clear: (u) => u.replace(/[^A-Za-zÀ-úÜüÖöäÄëï'0-9-. ]/g, "") }, noSpecialCharacterAndNumbers: { set: (u) => u.replace(/[^A-Za-zÀ-úÜüÖöäÄëï .-]/g, ""), clear: (u) => u.replace(/[^A-Za-zÀ-úÜüÖöäÄëï .-]/g, "") } }; class Z { constructor() { // não usar caracteres especiais de regex na 'key' e.g. '.' '*' '+' '?' '^' '$' '(' ')' '[' ']' '{' '}' '|' '\' q(this, "maskConfig", { A: { clean: /[^A-Za-z]/g, match: /[A-Za-z]/g }, /* alphabet .....*/ 0: { clean: /[^0-9]/g, match: /[0-9]/g }, /* numeric ......*/ "#": { clean: /[^A-Za-z0-9]/g, match: /[A-Za-z0-9]/g } /* alphanumeric .*/ }); } set(t, r) { const n = this.findSuitableMask(t, r); if (!n) return r; const a = this.findMajority([...n]), o = [...r.replace(this.maskConfig[a.value].clean, "")]; return [...n].reduce((i, l) => { const v = a.value === l ? o.shift() : "", g = o.length ? l : ""; return i.concat(v || g); }, ""); } clear(t, r) { const n = this.findSuitableMask(t, r); if (!n) return r; const a = this.findMajority([...n]), o = [...n.replace(this.maskConfig[a.value].match, "")]; return [...r].reduce((i, l) => i.length >= a.qty ? i.slice(0, a.qty) : o.includes(l) ? i : i.concat(l), ""); } get(t) { return typeof t == "function" ? t(V) : t ?? ""; } findSuitableMask(t, r) { var a; const n = (o, i, l) => { var k, y; const { value: v } = this.findMajority([...o]); if (!this.maskConfig[v]) return !1; const g = (k = o.match(new RegExp(`[${v}]`, "g"))) == null ? void 0 : k.length, _ = (((y = r.match(this.maskConfig[v].match)) == null ? void 0 : y.length) ?? 0) > (g ?? 0), E = i === l.length - 1; return !_ || E; }; return (a = t.split(",").find(n)) == null ? void 0 : a.trim(); } findMajority(t) { return Re(t, Object.keys(this.maskConfig)); } } const J = new Z(); class ge extends Z { input(t, r, n) { return (a) => { let o = a.currentTarget.value; return typeof t == "object" && (o = t.clear(a.currentTarget.value), a.currentTarget.value = t.set(o)), typeof t == "string" && (o = super.clear(t, a.currentTarget.value), a.currentTarget.value = super.set(t, o)), this.updateUnmasked(o, r), n == null || n(a), a; }; } value(t, r, n) { if (typeof t == "object") { const a = t.set(r); return this.updateUnmasked(t.clear(a), n), a; } if (typeof t == "string") { const a = super.set(t, r); return this.updateUnmasked(super.clear(t, a), n), a; } return this.updateUnmasked(r, n), r; } updateUnmasked(t, r) { if (r != null && r.current) return r.current["rb-value"] = t; const n = requestAnimationFrame(() => { r != null && r.current && (r.current["rb-value"] = t), cancelAnimationFrame(n); }); } } const b = new ge(); function ke(u) { const t = C(() => J.get(u), [u]), r = ie(null); return le(() => { var n, a; (n = r.current) != null && n.value && (r.current.value = b.value(t, r.current.value, r)), (a = r.current) != null && a.defaultValue && (r.current.defaultValue = b.value(t, r.current.defaultValue, r)); }, [t]), C( () => ({ ref: r, bindInputEvent: (n) => b.input(t, r, n), input: () => { var n, a; return { unmasked: (n = r.current) == null ? void 0 : n["rb-value"], masked: (a = r.current) == null ? void 0 : a.value }; }, props: { ref: r, onInput: b.input(t, r) } }), [t] ); } function he({ mask: u = "", ref: t, ...r }) { const n = ke(u); return fe(t, () => n.ref.current), /* @__PURE__ */ be.jsx("input", { ...r, ref: n.ref, onInput: n.bindInputEvent(r.onInput) }); } const ye = Object.assign(he, { masks: V }); function Ae() { const u = C(() => /* @__PURE__ */ new Map(), []); return { inputs: () => new Map( Array.from(u).map(([t, r]) => { var a, o; const n = { unmasked: (a = r.current) == null ? void 0 : a["rb-value"], masked: (o = r.current) == null ? void 0 : o.value }; return [t, n]; }) ), mask: (t, { mask: r, value: n, defaultValue: a, onInput: o }) => (r = J.get(r), n = String(n ?? ""), a = String(a ?? ""), { onInput: b.input(r, u.get(t), o), ref: u.get(t) ?? u.set(t, me()).get(t), ...n && { value: b.value(r, n, u.get(t)) }, ...a && { defaultValue: b.value(r, a, u.get(t)) } }) }; } export { ye as Input, ke as useMask, Ae as useMasks };