@jgamaraalv/react-auto-complete
Version:
An amazing auto complete for your React application
30 lines (26 loc) • 17.1 kB
JavaScript
(function(A,j){typeof exports=="object"&&typeof module<"u"?j(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],j):(A=typeof globalThis<"u"?globalThis:A||self,j(A.ReactAutoComplete={},A.React))})(this,function(A,j){"use strict";const H=(a=>a&&typeof a=="object"&&"default"in a?a:{default:a})(j),k={autocomplete__container:"_autocomplete__container_umggu_1",input:"_input_umggu_11",list:"_list_umggu_19",list__item:"_list__item_umggu_32","list__item__text--highlighted":"_list__item__text--highlighted_umggu_45"},F=(...a)=>(...m)=>a.forEach(u=>u==null?void 0:u(...m));function Ve(a,m,u){let _=a.target;for(;_!=null;){if(_===m.current)return;_=_.parentElement}u()}function Fe(a,m){(a.code==="Escape"||a.key==="Escape")&&m()}const ie=j.createContext(null);function J(){const a=j.useContext(ie);if(!a)throw new Error("This component must be used within <Autocomplete/>");return a}var oe={exports:{}},D={};/*
object-assign
(c) Sindre Sorhus
@license MIT
*/var K,ue;function se(){if(ue)return K;ue=1;var a=Object.getOwnPropertySymbols,m=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 K=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)m.call(s,y)&&(c[y]=s[y]);if(a){p=a(s);for(var g=0;g<p.length;g++)u.call(s,p[g])&&(c[p[g]]=s[p[g]])}}return c},K}/** @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 ce;function Ue(){if(ce)return D;ce=1,se();var a=H.default,m=60103;if(D.Fragment=60107,typeof Symbol=="function"&&Symbol.for){var u=Symbol.for;m=u("react.element"),D.Fragment=u("react.fragment")}var _=a.__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,T=null;p!==void 0&&(g=""+p),c.key!==void 0&&(g=""+c.key),c.ref!==void 0&&(T=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:m,type:s,key:g,ref:T,props:y,_owner:_.current}}return D.jsx=b,D.jsxs=b,D}var z={};/** @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 le;function $e(){return le||(le=1,function(a){process.env.NODE_ENV!=="production"&&function(){var m=H.default,u=se(),_=60103,w=60106;a.Fragment=60107;var v=60108,b=60114,s=60109,c=60110,p=60112,d=60113,y=60120,g=60115,T=60116,$=60121,de=60122,ve=60117,pe=60129,_e=60131;if(typeof Symbol=="function"&&Symbol.for){var E=Symbol.for;_=E("react.element"),w=E("react.portal"),a.Fragment=E("react.fragment"),v=E("react.strict_mode"),b=E("react.profiler"),s=E("react.provider"),c=E("react.context"),p=E("react.forward_ref"),d=E("react.suspense"),y=E("react.suspense_list"),g=E("react.memo"),T=E("react.lazy"),$=E("react.block"),de=E("react.server.block"),ve=E("react.fundamental"),E("react.scope"),E("react.opaque.id"),pe=E("react.debug_trace_mode"),E("react.offscreen"),_e=E("react.legacy_hidden")}var ge=typeof Symbol=="function"&&Symbol.iterator,qe="@@iterator";function Ge(e){if(e===null||typeof e!="object")return null;var r=ge&&e[ge]||e[qe];return typeof r=="function"?r:null}var I=m.__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];He("error",e,t)}}function He(e,r,t){{var n=I.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 Je=!1;function Ke(e){return!!(typeof e=="string"||typeof e=="function"||e===a.Fragment||e===b||e===pe||e===v||e===d||e===y||e===_e||Je||typeof e=="object"&&e!==null&&(e.$$typeof===T||e.$$typeof===g||e.$$typeof===s||e.$$typeof===c||e.$$typeof===p||e.$$typeof===ve||e.$$typeof===$||e[0]===de))}function ze(e,r,t){var n=r.displayName||r.name||"";return e.displayName||(n!==""?t+"("+n+")":t)}function he(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 a.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 he(r)+".Consumer";case s:var t=e;return he(t._context)+".Provider";case p:return ze(e,e.render,"ForwardRef");case g:return x(e.type);case $:return x(e._render);case T:{var n=e,l=n._payload,f=n._init;try{return x(f(l))}catch{return null}}}return null}var N=0,me,Ee,Re,be,ye,Oe,we;function Ce(){}Ce.__reactDisabledLog=!0;function Xe(){{if(N===0){me=console.log,Ee=console.info,Re=console.warn,be=console.error,ye=console.group,Oe=console.groupCollapsed,we=console.groupEnd;var e={configurable:!0,enumerable:!0,value:Ce,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}N++}}function Ze(){{if(N--,N===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:u({},e,{value:me}),info:u({},e,{value:Ee}),warn:u({},e,{value:Re}),error:u({},e,{value:be}),group:u({},e,{value:ye}),groupCollapsed:u({},e,{value:Oe}),groupEnd:u({},e,{value:we})})}N<0&&C("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var X=I.ReactCurrentDispatcher,Z;function M(e,r,t){{if(Z===void 0)try{throw Error()}catch(l){var n=l.stack.trim().match(/\n( *(at )?)/);Z=n&&n[1]||""}return`
`+Z+e}}var Q=!1,B;{var Qe=typeof WeakMap=="function"?WeakMap:Map;B=new Qe}function je(e,r){if(!e||Q)return"";{var t=B.get(e);if(t!==void 0)return t}var n;Q=!0;var l=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var f;f=X.current,X.current=null,Xe();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(S){n=S}Reflect.construct(e,[],o)}else{try{o.call()}catch(S){n=S}e.call(o.prototype)}}else{try{throw Error()}catch(S){n=S}e()}}catch(S){if(S&&n&&typeof S.stack=="string"){for(var i=S.stack.split(`
`),O=n.stack.split(`
`),h=i.length-1,R=O.length-1;h>=1&&R>=0&&i[h]!==O[R];)R--;for(;h>=1&&R>=0;h--,R--)if(i[h]!==O[R]){if(h!==1||R!==1)do if(h--,R--,R<0||i[h]!==O[R]){var P=`
`+i[h].replace(" at new "," at ");return typeof e=="function"&&B.set(e,P),P}while(h>=1&&R>=0);break}}}finally{Q=!1,X.current=f,Ze(),Error.prepareStackTrace=l}var W=e?e.displayName||e.name:"",Ne=W?M(W):"";return typeof e=="function"&&B.set(e,Ne),Ne}function xe(e,r,t){return je(e,!1)}function er(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function q(e,r,t){if(e==null)return"";if(typeof e=="function")return je(e,er(e));if(typeof e=="string")return M(e);switch(e){case d:return M("Suspense");case y:return M("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case p:return xe(e.render);case g:return q(e.type,r,t);case $:return xe(e._render);case T:{var n=e,l=n._payload,f=n._init;try{return q(f(l),r,t)}catch{}}}return""}var Te={},Pe=I.ReactDebugCurrentFrame;function G(e){if(e){var r=e._owner,t=q(e.type,e._source,r?r.type:null);Pe.setExtraStackFrame(t)}else Pe.setExtraStackFrame(null)}function rr(e,r,t,n,l){{var f=Function.call.bind(Object.prototype.hasOwnProperty);for(var o in e)if(f(e,o)){var i=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}i=e[o](r,o,n,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(h){i=h}i&&!(i instanceof Error)&&(G(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 i),G(null)),i instanceof Error&&!(i.message in Te)&&(Te[i.message]=!0,G(l),C("Failed %s type: %s",t,i.message),G(null))}}}var V=I.ReactCurrentOwner,ee=Object.prototype.hasOwnProperty,tr={key:!0,ref:!0,__self:!0,__source:!0},Se,Ae,re;re={};function nr(e){if(ee.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function ar(e){if(ee.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function ir(e,r){if(typeof e.ref=="string"&&V.current&&r&&V.current.stateNode!==r){var t=x(V.current.type);re[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(V.current.type),e.ref),re[t]=!0)}}function or(e,r){{var t=function(){Se||(Se=!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 ur(e,r){{var t=function(){Ae||(Ae=!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 sr=function(e,r,t,n,l,f,o){var i={$$typeof:_,type:e,key:r,ref:t,props:o,_owner:f};return i._store={},Object.defineProperty(i._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(i,"_self",{configurable:!1,enumerable:!1,writable:!1,value:n}),Object.defineProperty(i,"_source",{configurable:!1,enumerable:!1,writable:!1,value:l}),Object.freeze&&(Object.freeze(i.props),Object.freeze(i)),i};function cr(e,r,t,n,l){{var f,o={},i=null,O=null;t!==void 0&&(i=""+t),ar(r)&&(i=""+r.key),nr(r)&&(O=r.ref,ir(r,l));for(f in r)ee.call(r,f)&&!tr.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(i||O){var R=typeof e=="function"?e.displayName||e.name||"Unknown":e;i&&or(o,R),O&&ur(o,R)}return sr(e,i,O,l,n,V.current,o)}}var te=I.ReactCurrentOwner,ke=I.ReactDebugCurrentFrame;function Y(e){if(e){var r=e._owner,t=q(e.type,e._source,r?r.type:null);ke.setExtraStackFrame(t)}else ke.setExtraStackFrame(null)}var ne;ne=!1;function ae(e){return typeof e=="object"&&e!==null&&e.$$typeof===_}function De(){{if(te.current){var e=x(te.current.type);if(e)return`
Check the render method of \``+e+"`."}return""}}function lr(e){{if(e!==void 0){var r=e.fileName.replace(/^.*[\\\/]/,""),t=e.lineNumber;return`
Check your code at `+r+":"+t+"."}return""}}var Le={};function fr(e){{var r=De();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 Ie(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=fr(r);if(Le[t])return;Le[t]=!0;var n="";e&&e._owner&&e._owner!==te.current&&(n=" It was passed a child from "+x(e._owner.type)+"."),Y(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),Y(null)}}function Ye(e,r){{if(typeof e!="object")return;if(Array.isArray(e))for(var t=0;t<e.length;t++){var n=e[t];ae(n)&&Ie(n,r)}else if(ae(e))e._store&&(e._store.validated=!0);else if(e){var l=Ge(e);if(typeof l=="function"&&l!==e.entries)for(var f=l.call(e),o;!(o=f.next()).done;)ae(o.value)&&Ie(o.value,r)}}}function dr(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);rr(t,e.props,"prop",n,e)}else if(r.PropTypes!==void 0&&!ne){ne=!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 vr(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var n=r[t];if(n!=="children"&&n!=="key"){Y(e),C("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",n),Y(null);break}}e.ref!==null&&(Y(e),C("Invalid attribute `ref` supplied to `React.Fragment`."),Y(null))}}function We(e,r,t,n,l,f){{var o=Ke(e);if(!o){var i="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(i+=" 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=lr(l);O?i+=O:i+=De();var h;e===null?h="null":Array.isArray(e)?h="array":e!==void 0&&e.$$typeof===_?(h="<"+(x(e.type)||"Unknown")+" />",i=" 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,i)}var R=cr(e,r,t,l,f);if(R==null)return R;if(o){var P=r.children;if(P!==void 0)if(n)if(Array.isArray(P)){for(var W=0;W<P.length;W++)Ye(P[W],e);Object.freeze&&Object.freeze(P)}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 Ye(P,e)}return e===a.Fragment?vr(R):dr(R),R}}function pr(e,r,t){return We(e,r,t,!0)}function _r(e,r,t){return We(e,r,t,!1)}var gr=_r,hr=pr;a.jsx=gr,a.jsxs=hr}()}(z)),z}(function(a){process.env.NODE_ENV==="production"?a.exports=Ue():a.exports=$e()})(oe);const L=oe.exports.jsx;function U({children:a,...m}){const[u,_]=j.useState(!1),[w,v]=j.useState(""),b=j.useRef(null);return j.useEffect(()=>{const s=p=>Ve(p,b,()=>_(!1)),c=p=>Fe(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]),L(ie.Provider,{value:{open:u,value:w,setValue:v,setOpen:_},children:L("div",{className:k.autocomplete__container,ref:b,...m,children:a})})}function Me({onFocus:a,onBlur:m,onChange:u,..._}){const{value:w,setOpen:v,setValue:b}=J();function s(c){b(c.target.value)}return L("input",{onFocus:F(()=>v(!0),a),onChange:F(s,u),value:w,className:k.input,..._})}function fe({children:a,value:m,onClick:u,onKeyDown:_,...w}){const{value:v,setOpen:b,setValue:s}=J(),c=new RegExp(`(${v.toLowerCase()})`,"i");function p(){s(m),b(!1)}function d(g){(g.key==="Enter"||g.code==="Enter")&&p()}const y=a.split(c).map((g,T)=>g.toLowerCase()===v.toLowerCase()?L("span",{className:k["list__item__text--highlighted"],children:g},`value-${g}-${T}`):g);return L("li",{onClick:F(p,()=>u==null?void 0:u(m)),onKeyDown:F(d,_),className:k.list__item,tabIndex:0,...w,children:y})}function Be({children:a,...m}){const{open:u,value:_}=J(),w=H.default.Children.map(a,v=>{if(v.type!==fe)throw new Error("<Autocomplete.List> children must be an <Autocomplete.ListItem>");return v.props.children.toLowerCase().includes(String(_||"").toLowerCase())?v:null});return u?L("ul",{className:k.list,...m,children:w}):null}U.Input=Me,U.List=Be,U.ListItem=fe,A.Autocomplete=U,A.classes=k,Object.defineProperties(A,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});