UNPKG

@jgamaraalv/react-auto-complete

Version:

An amazing auto complete for your React application

790 lines (786 loc) 25.9 kB
import ee, { createContext as fr, useContext as dr, useState as ke, useRef as vr, useEffect as pr } from "react"; const _r = "_autocomplete__container_umggu_1", gr = "_input_umggu_11", hr = "_list_umggu_19", Er = "_list__item_umggu_32", Y = { autocomplete__container: _r, input: gr, list: hr, list__item: Er, "list__item__text--highlighted": "_list__item__text--highlighted_umggu_45" }, M = (...i) => (...E) => i.forEach((u) => u == null ? void 0 : u(...E)); function mr(i, E, u) { let _ = i.target; for (; _ != null; ) { if (_ === E.current) return; _ = _.parentElement; } u(); } function Rr(i, E) { (i.code === "Escape" || i.key === "Escape") && E(); } const Le = fr(null); function re() { const i = dr(Le); if (!i) throw new Error("This component must be used within <Autocomplete/>"); return i; } var Ie = { exports: {} }, D = {}; /* object-assign (c) Sindre Sorhus @license MIT */ var Z, Ae; function $e() { if (Ae) return Z; Ae = 1; var i = Object.getOwnPropertySymbols, E = Object.prototype.hasOwnProperty, u = Object.prototype.propertyIsEnumerable; function _(v) { if (v == null) throw new TypeError("Object.assign cannot be called with null or undefined"); return Object(v); } function w() { try { if (!Object.assign) return !1; var v = new String("abc"); if (v[5] = "de", Object.getOwnPropertyNames(v)[0] === "5") return !1; for (var b = {}, s = 0; s < 10; s++) b["_" + String.fromCharCode(s)] = s; var c = Object.getOwnPropertyNames(b).map(function(d) { return b[d]; }); if (c.join("") !== "0123456789") return !1; var p = {}; return "abcdefghijklmnopqrst".split("").forEach(function(d) { p[d] = d; }), Object.keys(Object.assign({}, p)).join("") === "abcdefghijklmnopqrst"; } catch { return !1; } } return Z = w() ? Object.assign : function(v, b) { for (var s, c = _(v), p, d = 1; d < arguments.length; d++) { s = Object(arguments[d]); for (var y in s) E.call(s, y) && (c[y] = s[y]); if (i) { p = i(s); for (var g = 0; g < p.length; g++) u.call(s, p[g]) && (c[p[g]] = s[p[g]]); } } return c; }, Z; } /** @license React v17.0.2 * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Se; function br() { if (Se) return D; Se = 1, $e(); var i = ee, E = 60103; if (D.Fragment = 60107, typeof Symbol == "function" && Symbol.for) { var u = Symbol.for; E = u("react.element"), D.Fragment = u("react.fragment"); } var _ = i.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, w = Object.prototype.hasOwnProperty, v = { key: !0, ref: !0, __self: !0, __source: !0 }; function b(s, c, p) { var d, y = {}, g = null, P = null; p !== void 0 && (g = "" + p), c.key !== void 0 && (g = "" + c.key), c.ref !== void 0 && (P = c.ref); for (d in c) w.call(c, d) && !v.hasOwnProperty(d) && (y[d] = c[d]); if (s && s.defaultProps) for (d in c = s.defaultProps, c) y[d] === void 0 && (y[d] = c[d]); return { $$typeof: E, type: s, key: g, ref: P, props: y, _owner: _.current }; } return D.jsx = b, D.jsxs = b, D; } var Q = {}; /** @license React v17.0.2 * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var De; function yr() { return De || (De = 1, function(i) { process.env.NODE_ENV !== "production" && function() { var E = ee, u = $e(), _ = 60103, w = 60106; i.Fragment = 60107; var v = 60108, b = 60114, s = 60109, c = 60110, p = 60112, d = 60113, y = 60120, g = 60115, P = 60116, W = 60121, ne = 60122, ae = 60117, ie = 60129, oe = 60131; if (typeof Symbol == "function" && Symbol.for) { var m = Symbol.for; _ = m("react.element"), w = m("react.portal"), i.Fragment = m("react.fragment"), v = m("react.strict_mode"), b = m("react.profiler"), s = m("react.provider"), c = m("react.context"), p = m("react.forward_ref"), d = m("react.suspense"), y = m("react.suspense_list"), g = m("react.memo"), P = m("react.lazy"), W = m("react.block"), ne = m("react.server.block"), ae = m("react.fundamental"), m("react.scope"), m("react.opaque.id"), ie = m("react.debug_trace_mode"), m("react.offscreen"), oe = m("react.legacy_hidden"); } var ue = typeof Symbol == "function" && Symbol.iterator, We = "@@iterator"; function Ne(e) { if (e === null || typeof e != "object") return null; var r = ue && e[ue] || e[We]; return typeof r == "function" ? r : null; } var k = E.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function C(e) { { for (var r = arguments.length, t = new Array(r > 1 ? r - 1 : 0), n = 1; n < r; n++) t[n - 1] = arguments[n]; Ve("error", e, t); } } function Ve(e, r, t) { { var n = k.ReactDebugCurrentFrame, l = n.getStackAddendum(); l !== "" && (r += "%s", t = t.concat([l])); var f = t.map(function(o) { return "" + o; }); f.unshift("Warning: " + r), Function.prototype.apply.call(console[e], console, f); } } var Fe = !1; function Ue(e) { return !!(typeof e == "string" || typeof e == "function" || e === i.Fragment || e === b || e === ie || e === v || e === d || e === y || e === oe || Fe || typeof e == "object" && e !== null && (e.$$typeof === P || e.$$typeof === g || e.$$typeof === s || e.$$typeof === c || e.$$typeof === p || e.$$typeof === ae || e.$$typeof === W || e[0] === ne)); } function Me(e, r, t) { var n = r.displayName || r.name || ""; return e.displayName || (n !== "" ? t + "(" + n + ")" : t); } function se(e) { return e.displayName || "Context"; } function x(e) { if (e == null) return null; if (typeof e.tag == "number" && C("Received an unexpected object in getComponentName(). This is likely a bug in React. Please file an issue."), typeof e == "function") return e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case i.Fragment: return "Fragment"; case w: return "Portal"; case b: return "Profiler"; case v: return "StrictMode"; case d: return "Suspense"; case y: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case c: var r = e; return se(r) + ".Consumer"; case s: var t = e; return se(t._context) + ".Provider"; case p: return Me(e, e.render, "ForwardRef"); case g: return x(e.type); case W: return x(e._render); case P: { var n = e, l = n._payload, f = n._init; try { return x(f(l)); } catch { return null; } } } return null; } var I = 0, ce, le, fe, de, ve, pe, _e; function ge() { } ge.__reactDisabledLog = !0; function qe() { { if (I === 0) { ce = console.log, le = console.info, fe = console.warn, de = console.error, ve = console.group, pe = console.groupCollapsed, _e = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: ge, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } I++; } } function Be() { { if (I--, I === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: u({}, e, { value: ce }), info: u({}, e, { value: le }), warn: u({}, e, { value: fe }), error: u({}, e, { value: de }), group: u({}, e, { value: ve }), groupCollapsed: u({}, e, { value: pe }), groupEnd: u({}, e, { value: _e }) }); } I < 0 && C("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var q = k.ReactCurrentDispatcher, B; function N(e, r, t) { { if (B === void 0) try { throw Error(); } catch (l) { var n = l.stack.trim().match(/\n( *(at )?)/); B = n && n[1] || ""; } return ` ` + B + e; } } var G = !1, V; { var Ge = typeof WeakMap == "function" ? WeakMap : Map; V = new Ge(); } function he(e, r) { if (!e || G) return ""; { var t = V.get(e); if (t !== void 0) return t; } var n; G = !0; var l = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var f; f = q.current, q.current = null, qe(); try { if (r) { var o = function() { throw Error(); }; if (Object.defineProperty(o.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(o, []); } catch (T) { n = T; } Reflect.construct(e, [], o); } else { try { o.call(); } catch (T) { n = T; } e.call(o.prototype); } } else { try { throw Error(); } catch (T) { n = T; } e(); } } catch (T) { if (T && n && typeof T.stack == "string") { for (var a = T.stack.split(` `), O = n.stack.split(` `), h = a.length - 1, R = O.length - 1; h >= 1 && R >= 0 && a[h] !== O[R]; ) R--; for (; h >= 1 && R >= 0; h--, R--) if (a[h] !== O[R]) { if (h !== 1 || R !== 1) do if (h--, R--, R < 0 || a[h] !== O[R]) { var j = ` ` + a[h].replace(" at new ", " at "); return typeof e == "function" && V.set(e, j), j; } while (h >= 1 && R >= 0); break; } } } finally { G = !1, q.current = f, Be(), Error.prepareStackTrace = l; } var S = e ? e.displayName || e.name : "", Te = S ? N(S) : ""; return typeof e == "function" && V.set(e, Te), Te; } function Ee(e, r, t) { return he(e, !1); } function He(e) { var r = e.prototype; return !!(r && r.isReactComponent); } function F(e, r, t) { if (e == null) return ""; if (typeof e == "function") return he(e, He(e)); if (typeof e == "string") return N(e); switch (e) { case d: return N("Suspense"); case y: return N("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case p: return Ee(e.render); case g: return F(e.type, r, t); case W: return Ee(e._render); case P: { var n = e, l = n._payload, f = n._init; try { return F(f(l), r, t); } catch { } } } return ""; } var me = {}, Re = k.ReactDebugCurrentFrame; function U(e) { if (e) { var r = e._owner, t = F(e.type, e._source, r ? r.type : null); Re.setExtraStackFrame(t); } else Re.setExtraStackFrame(null); } function Je(e, r, t, n, l) { { var f = Function.call.bind(Object.prototype.hasOwnProperty); for (var o in e) if (f(e, o)) { var a = void 0; try { if (typeof e[o] != "function") { var O = Error((n || "React class") + ": " + t + " type `" + o + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[o] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw O.name = "Invariant Violation", O; } a = e[o](r, o, n, t, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (h) { a = h; } a && !(a instanceof Error) && (U(l), C("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", n || "React class", t, o, typeof a), U(null)), a instanceof Error && !(a.message in me) && (me[a.message] = !0, U(l), C("Failed %s type: %s", t, a.message), U(null)); } } } var $ = k.ReactCurrentOwner, H = Object.prototype.hasOwnProperty, Ke = { key: !0, ref: !0, __self: !0, __source: !0 }, be, ye, J; J = {}; function ze(e) { if (H.call(e, "ref")) { var r = Object.getOwnPropertyDescriptor(e, "ref").get; if (r && r.isReactWarning) return !1; } return e.ref !== void 0; } function Xe(e) { if (H.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function Ze(e, r) { if (typeof e.ref == "string" && $.current && r && $.current.stateNode !== r) { var t = x($.current.type); J[t] || (C('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref', x($.current.type), e.ref), J[t] = !0); } } function Qe(e, r) { { var t = function() { be || (be = !0, C("%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://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "key", { get: t, configurable: !0 }); } } function er(e, r) { { var t = function() { ye || (ye = !0, C("%s: `ref` 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://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "ref", { get: t, configurable: !0 }); } } var rr = function(e, r, t, n, l, f, o) { var a = { $$typeof: _, type: e, key: r, ref: t, props: o, _owner: f }; return a._store = {}, Object.defineProperty(a._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(a, "_self", { configurable: !1, enumerable: !1, writable: !1, value: n }), Object.defineProperty(a, "_source", { configurable: !1, enumerable: !1, writable: !1, value: l }), Object.freeze && (Object.freeze(a.props), Object.freeze(a)), a; }; function tr(e, r, t, n, l) { { var f, o = {}, a = null, O = null; t !== void 0 && (a = "" + t), Xe(r) && (a = "" + r.key), ze(r) && (O = r.ref, Ze(r, l)); for (f in r) H.call(r, f) && !Ke.hasOwnProperty(f) && (o[f] = r[f]); if (e && e.defaultProps) { var h = e.defaultProps; for (f in h) o[f] === void 0 && (o[f] = h[f]); } if (a || O) { var R = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; a && Qe(o, R), O && er(o, R); } return rr(e, a, O, l, n, $.current, o); } } var K = k.ReactCurrentOwner, Oe = k.ReactDebugCurrentFrame; function A(e) { if (e) { var r = e._owner, t = F(e.type, e._source, r ? r.type : null); Oe.setExtraStackFrame(t); } else Oe.setExtraStackFrame(null); } var z; z = !1; function X(e) { return typeof e == "object" && e !== null && e.$$typeof === _; } function we() { { if (K.current) { var e = x(K.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function nr(e) { { if (e !== void 0) { var r = e.fileName.replace(/^.*[\\\/]/, ""), t = e.lineNumber; return ` Check your code at ` + r + ":" + t + "."; } return ""; } } var Ce = {}; function ar(e) { { var r = we(); if (!r) { var t = typeof e == "string" ? e : e.displayName || e.name; t && (r = ` Check the top-level render call using <` + t + ">."); } return r; } } function xe(e, r) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var t = ar(r); if (Ce[t]) return; Ce[t] = !0; var n = ""; e && e._owner && e._owner !== K.current && (n = " It was passed a child from " + x(e._owner.type) + "."), A(e), C('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', t, n), A(null); } } function Pe(e, r) { { if (typeof e != "object") return; if (Array.isArray(e)) for (var t = 0; t < e.length; t++) { var n = e[t]; X(n) && xe(n, r); } else if (X(e)) e._store && (e._store.validated = !0); else if (e) { var l = Ne(e); if (typeof l == "function" && l !== e.entries) for (var f = l.call(e), o; !(o = f.next()).done; ) X(o.value) && xe(o.value, r); } } } function ir(e) { { var r = e.type; if (r == null || typeof r == "string") return; var t; if (typeof r == "function") t = r.propTypes; else if (typeof r == "object" && (r.$$typeof === p || r.$$typeof === g)) t = r.propTypes; else return; if (t) { var n = x(r); Je(t, e.props, "prop", n, e); } else if (r.PropTypes !== void 0 && !z) { z = !0; var l = x(r); C("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", l || "Unknown"); } typeof r.getDefaultProps == "function" && !r.getDefaultProps.isReactClassApproved && C("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function or(e) { { for (var r = Object.keys(e.props), t = 0; t < r.length; t++) { var n = r[t]; if (n !== "children" && n !== "key") { A(e), C("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", n), A(null); break; } } e.ref !== null && (A(e), C("Invalid attribute `ref` supplied to `React.Fragment`."), A(null)); } } function je(e, r, t, n, l, f) { { var o = Ue(e); if (!o) { var a = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (a += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."); var O = nr(l); O ? a += O : a += we(); var h; e === null ? h = "null" : Array.isArray(e) ? h = "array" : e !== void 0 && e.$$typeof === _ ? (h = "<" + (x(e.type) || "Unknown") + " />", a = " Did you accidentally export a JSX literal instead of a component?") : h = typeof e, C("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", h, a); } var R = tr(e, r, t, l, f); if (R == null) return R; if (o) { var j = r.children; if (j !== void 0) if (n) if (Array.isArray(j)) { for (var S = 0; S < j.length; S++) Pe(j[S], e); Object.freeze && Object.freeze(j); } else C("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 Pe(j, e); } return e === i.Fragment ? or(R) : ir(R), R; } } function ur(e, r, t) { return je(e, r, t, !0); } function sr(e, r, t) { return je(e, r, t, !1); } var cr = sr, lr = ur; i.jsx = cr, i.jsxs = lr; }(); }(Q)), Q; } (function(i) { process.env.NODE_ENV === "production" ? i.exports = br() : i.exports = yr(); })(Ie); const L = Ie.exports.jsx; function te({ children: i, ...E }) { const [u, _] = ke(!1), [w, v] = ke(""), b = vr(null); return pr(() => { const s = (p) => mr(p, b, () => _(!1)), c = (p) => Rr(p, () => _(!1)); return u && (document.body.addEventListener("click", s), document.body.addEventListener("keydown", c)), () => { u && (document.body.removeEventListener("click", s), document.body.removeEventListener("keydown", c)); }; }, [u]), /* @__PURE__ */ L(Le.Provider, { value: { open: u, value: w, setValue: v, setOpen: _ }, children: /* @__PURE__ */ L("div", { className: Y.autocomplete__container, ref: b, ...E, children: i }) }); } function Or({ onFocus: i, onBlur: E, onChange: u, ..._ }) { const { value: w, setOpen: v, setValue: b } = re(); function s(c) { b(c.target.value); } return /* @__PURE__ */ L("input", { onFocus: M(() => v(!0), i), onChange: M(s, u), value: w, className: Y.input, ..._ }); } function Ye({ children: i, value: E, onClick: u, onKeyDown: _, ...w }) { const { value: v, setOpen: b, setValue: s } = re(), c = new RegExp(`(${v.toLowerCase()})`, "i"); function p() { s(E), b(!1); } function d(g) { (g.key === "Enter" || g.code === "Enter") && p(); } const y = i.split(c).map((g, P) => g.toLowerCase() === v.toLowerCase() ? /* @__PURE__ */ L("span", { className: Y["list__item__text--highlighted"], children: g }, `value-${g}-${P}`) : g); return /* @__PURE__ */ L("li", { onClick: M(p, () => u == null ? void 0 : u(E)), onKeyDown: M(d, _), className: Y.list__item, tabIndex: 0, ...w, children: y }); } function wr({ children: i, ...E }) { const { open: u, value: _ } = re(), w = ee.Children.map(i, (v) => { if (v.type !== Ye) throw new Error("<Autocomplete.List> children must be an <Autocomplete.ListItem>"); return v.props.children.toLowerCase().includes(String(_ || "").toLowerCase()) ? v : null; }); return u ? /* @__PURE__ */ L("ul", { className: Y.list, ...E, children: w }) : null; } te.Input = Or; te.List = wr; te.ListItem = Ye; export { te as Autocomplete, Y as classes };