turnstone
Version:
React customisable autocomplete component with typeahead and grouped results from multiple APIs.
26 lines (23 loc) • 29.1 kB
JavaScript
var mn=Object.defineProperty,pn=Object.defineProperties;var gn=Object.getOwnPropertyDescriptors;var Me=Object.getOwnPropertySymbols;var Ut=Object.prototype.hasOwnProperty,Gt=Object.prototype.propertyIsEnumerable;var $t=(u,S,E)=>S in u?mn(u,S,{enumerable:!0,configurable:!0,writable:!0,value:E}):u[S]=E,Y=(u,S)=>{for(var E in S||(S={}))Ut.call(S,E)&&$t(u,E,S[E]);if(Me)for(var E of Me(S))Gt.call(S,E)&&$t(u,E,S[E]);return u},we=(u,S)=>pn(u,gn(S));var qt=(u,S)=>{var E={};for(var $ in u)Ut.call(u,$)&&S.indexOf($)<0&&(E[$]=u[$]);if(u!=null&&Me)for(var $ of Me(u))S.indexOf($)<0&&Gt.call(u,$)&&(E[$]=u[$]);return E};(function(u,S){typeof exports=="object"&&typeof module!="undefined"?module.exports=S(require("react")):typeof define=="function"&&define.amd?define(["react"],S):(u=typeof globalThis!="undefined"?globalThis:u||self,u.turnstone=S(u.React))})(this,function(u){"use strict";function S(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var E=S(u),$={exports:{}},Qt="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED",zt=Qt,Kt=zt;function Ye(){}function Xe(){}Xe.resetWarningCache=Ye;var Jt=function(){function e(a,n,o,s,c,v){if(v!==Kt){var i=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw i.name="Invariant Violation",i}}e.isRequired=e;function r(){return e}var t={array:e,bigint:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:r,element:e,elementType:e,instanceOf:r,node:e,objectOf:r,oneOf:r,oneOfType:r,shape:r,exact:r,checkPropTypes:Xe,resetWarningCache:Ye};return t.PropTypes=t,t};$.exports=Jt();var f=$.exports;const Ze="SET_QUERY",et="SET_ITEMS",tt="SET_ITEMS_ERROR",rt="CLEAR",nt="SET_HIGHLIGHTED",Yt="CLEAR_HIGHLIGHTED",at="NEXT_HIGHLIGHTED",ot="PREV_HIGHLIGHTED",st="SET_SELECTED",F=void 0;function le(e){return e===F}const Ie=(e,r)=>r[e]?{index:e,text:r[e].text}:F,Xt=(e,r)=>{const t=(()=>{let a,n;switch(r.type){case Ze:return a={itemsError:!1,query:r.query,selected:F},r.query.length<e.props.minQueryLength&&(a.canShowListbox=!1),r.query.length===0&&e.props.defaultListbox&&(a.canShowListbox=!0),a;case et:return a={items:r.items,itemsError:!1,highlighted:r.items.length&&e.query.length?Ie(0,r.items):F},(e.query.length||r.items.length)&&(a.canShowListbox=!0),a;case rt:return{query:"",items:[],itemsError:!1,canShowListbox:!1,highlighted:F,selected:F};case tt:return{items:[],itemsError:!0,canShowListbox:!1};case nt:return{highlighted:Ie(r.index,e.items)};case Yt:return{highlighted:F};case ot:return e.highlighted&&e.highlighted.index>0?{highlighted:Ie(e.highlighted.index-1,e.items)}:{};case at:return e.highlighted&&e.highlighted.index<e.items.length-1?{highlighted:Ie(e.highlighted.index+1,e.items)}:{};case st:return n=le(r.index)?r.item:e.items[r.index],{selected:n,query:n?n.text:F};default:throw new Error("Invalid action type passed to reducer")}})();return Y(Y({},e),t)},it=e=>({type:Ze,query:e}),Zt=e=>({type:et,items:e}),er=()=>({type:tt}),tr=()=>({type:rt}),ut=e=>({type:nt,index:e}),rr=()=>({type:ot}),nr=()=>({type:at}),Oe=e=>{const r=st;return typeof e=="object"?{type:r,item:e}:{type:r,index:e}};var Le={exports:{}},ge={};/*
object-assign
(c) Sindre Sorhus
@license MIT
*/var ct=Object.getOwnPropertySymbols,ar=Object.prototype.hasOwnProperty,or=Object.prototype.propertyIsEnumerable;function sr(e){if(e==null)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(e)}function ir(){try{if(!Object.assign)return!1;var e=new String("abc");if(e[5]="de",Object.getOwnPropertyNames(e)[0]==="5")return!1;for(var r={},t=0;t<10;t++)r["_"+String.fromCharCode(t)]=t;var a=Object.getOwnPropertyNames(r).map(function(o){return r[o]});if(a.join("")!=="0123456789")return!1;var n={};return"abcdefghijklmnopqrst".split("").forEach(function(o){n[o]=o}),Object.keys(Object.assign({},n)).join("")==="abcdefghijklmnopqrst"}catch{return!1}}ir();/** @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 ur=E.default,lt=60103;if(ge.Fragment=60107,typeof Symbol=="function"&&Symbol.for){var ft=Symbol.for;lt=ft("react.element"),ge.Fragment=ft("react.fragment")}var cr=ur.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,lr=Object.prototype.hasOwnProperty,fr={key:!0,ref:!0,__self:!0,__source:!0};function dt(e,r,t){var a,n={},o=null,s=null;t!==void 0&&(o=""+t),r.key!==void 0&&(o=""+r.key),r.ref!==void 0&&(s=r.ref);for(a in r)lr.call(r,a)&&!fr.hasOwnProperty(a)&&(n[a]=r[a]);if(e&&e.defaultProps)for(a in r=e.defaultProps,r)n[a]===void 0&&(n[a]=r[a]);return{$$typeof:lt,type:e,key:o,ref:s,props:n,_owner:cr.current}}ge.jsx=dt,ge.jsxs=dt,Le.exports=ge;const p=Le.exports.jsx,vt=Le.exports.jsxs,ht=Le.exports.Fragment,re=u.createContext(),dr=e=>{const{text:r="",items:t=[]}=e,c=e,{children:a}=c,n=qt(c,["children"]),[o,s]=u.useReducer(Xt,{query:r,items:t,itemsError:!1,canShowListbox:!1,highlighted:F,selected:F,props:n});return u.useEffect(()=>s(it(r)),[r]),p(re.Provider,{value:{state:o,dispatch:s},children:a})},He={listbox:{position:"absolute",zIndex:4}};function vr(e){if(typeof e!="string")throw new TypeError("Expected a string");return e.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d")}function hr(e){const{text:r,match:t,global:a,styles:n}=e,s=`${a?"":"^"}(${vr(t)})`,c=new RegExp(s,"i"),v=t?r.split(c).filter(l=>l.length):[r],i=v.map((l,h)=>l.toLowerCase()===t.toLowerCase()?p("strong",{className:n.match,children:v[h]},`part${h}`):p(E.default.Fragment,{children:v[h]},`part${h}`));return p(ht,{children:i})}function mt(e){const{index:r,item:t,styles:a}=e,{state:n,dispatch:o}=u.useContext(re),{highlighted:s,query:c}=n,v=n.props.Item,i=t.searchType==="contains",l=s&&r===s.index,h=a[l?"highlightedItem":"item"],m=()=>{o(ut(r))},w=()=>{o(Oe(r))},k=(y,d)=>{o(Oe({value:y,displayField:d,text:y[d]}))},x=v?p(v,{appearsInDefaultListbox:t.defaultListbox,groupId:t.groupId,groupIndex:t.groupIndex,groupName:t.groupName,index:r,isHighlighted:l,item:t.value,query:c,searchType:t.searchType,setSelected:k,totalItems:n.items.length}):n.props.matchText&&!t.defaultListbox?p(hr,{text:t.text,match:c,global:i,styles:a}):p(ht,{children:t.text});return p("div",{className:h,onMouseEnter:m,onMouseDown:w,role:"option","aria-selected":l,"aria-label":t.text,children:x})}function mr(e){const{groupName:r,index:t,item:a,styles:n}=e,{state:o}=u.useContext(re),s=o.props.GroupName,c=!!r&&(s?p(s,{id:a.groupId,index:a.groupIndex,children:r}):r);return vt(E.default.Fragment,{children:[!!c&&p("div",{className:n.groupHeading,children:c}),p(mt,{index:t,item:a,styles:n},`item${t}`)]})}function pr(e){const{id:r,items:t,noItemsMessage:a,styles:n}=e,{state:o}=u.useContext(re),s=()=>p("div",{id:r,className:n.listbox,style:He.listbox,role:"listbox",children:t.map((i,l)=>l===0||i.groupIndex!==t[l-1].groupIndex?p(mr,{groupName:i.groupName,index:l,item:i,styles:n},`item${l}`):p(mt,{index:l,item:i,styles:n},`item${l}`))}),c=()=>p("div",{id:r,className:n.listbox,style:He.listbox,children:p("div",{className:n.noItems,children:a})});return(()=>t&&t.length?s():a&&o.query?c():p(E.default.Fragment,{}))()}function gr(e){const{id:r,errorMessage:t,styles:a}=e;return p("div",{id:r,className:a.errorbox,style:He.listbox,children:p("div",{className:a.errorMessage,children:t})})}function yr(e,r,t){var a=this,n=u.useRef(null),o=u.useRef(0),s=u.useRef(null),c=u.useRef([]),v=u.useRef(),i=u.useRef(),l=u.useRef(e),h=u.useRef(!0);l.current=e;var m=!r&&r!==0&&typeof window!="undefined";if(typeof e!="function")throw new TypeError("Expected a function");r=+r||0,t=t||{};var w=!!t.leading,k="trailing"in t?!!t.trailing:!0,x="maxWait"in t,y=x?Math.max(+t.maxWait||0,r):null;u.useEffect(function(){return h.current=!0,function(){h.current=!1}},[]);var d=u.useMemo(function(){var z=function(T){var g=c.current,P=v.current;return c.current=v.current=null,o.current=T,i.current=l.current.apply(P,g)},A=function(T,g){m&&cancelAnimationFrame(s.current),s.current=m?requestAnimationFrame(T):setTimeout(T,g)},M=function(T){if(!h.current)return!1;var g=T-n.current,P=T-o.current;return!n.current||g>=r||g<0||x&&P>=y},H=function(T){return s.current=null,k&&c.current?z(T):(c.current=v.current=null,i.current)},_=function(){var T=Date.now();if(M(T))return H(T);if(!!h.current){var g=T-n.current,P=T-o.current,C=r-g,de=x?Math.min(C,y-P):C;A(_,de)}},N=function(){for(var T=[],g=0;g<arguments.length;g++)T[g]=arguments[g];var P=Date.now(),C=M(P);if(c.current=T,v.current=a,n.current=P,C){if(!s.current&&h.current)return o.current=n.current,A(_,r),w?z(n.current):i.current;if(x)return A(_,r),z(n.current)}return s.current||A(_,r),i.current};return N.cancel=function(){s.current&&(m?cancelAnimationFrame(s.current):clearTimeout(s.current)),o.current=0,c.current=n.current=v.current=s.current=null},N.isPending=function(){return!!s.current},N.flush=function(){return s.current?H(Date.now()):i.current},N},[w,x,r,y,k,m]);return d}function br(e,r){return e===r}function pt(e){return typeof e=="function"?function(){return e}:e}function xr(e){var r=u.useState(pt(e)),t=r[0],a=r[1],n=u.useCallback(function(o){return a(pt(o))},[]);return[t,n]}function Er(e,r,t){var a=t&&t.equalityFn||br,n=xr(e),o=n[0],s=n[1],c=yr(u.useCallback(function(i){return s(i)},[s]),r,t),v=u.useRef(e);return a(v.current,e)||(c(e),v.current=e),[o,c]}/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function gt(e,r,t,a){function n(o){return o instanceof t?o:new t(function(s){s(o)})}return new(t||(t=Promise))(function(o,s){function c(l){try{i(a.next(l))}catch(h){s(h)}}function v(l){try{i(a.throw(l))}catch(h){s(h)}}function i(l){l.done?o(l.value):n(l.value).then(c,v)}i((a=a.apply(e,r||[])).next())})}function yt(e,r){var t={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},a,n,o,s;return s={next:c(0),throw:c(1),return:c(2)},typeof Symbol=="function"&&(s[Symbol.iterator]=function(){return this}),s;function c(i){return function(l){return v([i,l])}}function v(i){if(a)throw new TypeError("Generator is already executing.");for(;t;)try{if(a=1,n&&(o=i[0]&2?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[i[0]&2,o.value]),i[0]){case 0:case 1:o=i;break;case 4:return t.label++,{value:i[1],done:!1};case 5:t.label++,n=i[1],i=[0];continue;case 7:i=t.ops.pop(),t.trys.pop();continue;default:if(o=t.trys,!(o=o.length>0&&o[o.length-1])&&(i[0]===6||i[0]===2)){t=0;continue}if(i[0]===3&&(!o||i[1]>o[0]&&i[1]<o[3])){t.label=i[1];break}if(i[0]===6&&t.label<o[1]){t.label=o[1],o=i;break}if(o&&t.label<o[2]){t.label=o[2],t.ops.push(i);break}o[2]&&t.ops.pop(),t.trys.pop();continue}i=r.call(e,t)}catch(l){i=[6,l],n=0}finally{a=o=0}if(i[0]&5)throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}}var K=function(){},L=K(),ye=Object,R=function(e){return e===L},ne=function(e){return typeof e=="function"},X=function(e,r){return ye.assign({},e,r)},Pe="undefined",je=function(){return typeof window!=Pe},Tr=function(){return typeof document!=Pe},Sr=function(){return je()&&typeof window.requestAnimationFrame!=Pe},_e=new WeakMap,Cr=0,be=function(e){var r=typeof e,t=e&&e.constructor,a=t==Date,n,o;if(ye(e)===e&&!a&&t!=RegExp){if(n=_e.get(e),n)return n;if(n=++Cr+"~",_e.set(e,n),t==Array){for(n="@",o=0;o<e.length;o++)n+=be(e[o])+",";_e.set(e,n)}if(t==ye){n="#";for(var s=ye.keys(e).sort();!R(o=s.pop());)R(e[o])||(n+=o+":"+be(e[o])+",");_e.set(e,n)}}else n=a?e.toJSON():r=="symbol"?e.toString():r=="string"?JSON.stringify(e):""+e;return n},ke=!0,wr=function(){return ke},bt=je(),We=Tr(),Be=bt&&window.addEventListener?window.addEventListener.bind(window):K,Ir=We?document.addEventListener.bind(document):K,Ve=bt&&window.removeEventListener?window.removeEventListener.bind(window):K,Or=We?document.removeEventListener.bind(document):K,Lr=function(){var e=We&&document.visibilityState;return R(e)||e!=="hidden"},_r=function(e){return Ir("visibilitychange",e),Be("focus",e),function(){Or("visibilitychange",e),Ve("focus",e)}},Dr=function(e){var r=function(){ke=!0,e()},t=function(){ke=!1};return Be("online",r),Be("offline",t),function(){Ve("online",r),Ve("offline",t)}},Rr={isOnline:wr,isVisible:Lr},Nr={initFocus:_r,initReconnect:Dr},De=!je()||"Deno"in window,Fr=function(e){return Sr()?window.requestAnimationFrame(e):setTimeout(e,1)},xe=De?u.useEffect:u.useLayoutEffect,$e=typeof navigator!="undefined"&&navigator.connection,xt=!De&&$e&&(["slow-2g","2g"].includes($e.effectiveType)||$e.saveData),Et=function(e){if(ne(e))try{e=e()}catch{e=""}var r=[].concat(e);e=typeof e=="string"?e:(Array.isArray(e)?e.length:e)?be(e):"";var t=e?"$swr$"+e:"";return[e,r,t]},ae=new WeakMap,Tt=0,St=1,Ct=2,Ee=function(e,r,t,a,n,o,s){s===void 0&&(s=!0);var c=ae.get(e),v=c[0],i=c[1],l=c[3],h=v[r],m=i[r];if(s&&m)for(var w=0;w<m.length;++w)m[w](t,a,n);return o&&(delete l[r],h&&h[0])?h[0](Ct).then(function(){return e.get(r)}):e.get(r)},Ar=0,Ue=function(){return++Ar},wt=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return gt(void 0,void 0,void 0,function(){var t,a,n,o,s,c,v,i,l,h,m,w,k,x,y,d,z,A,M,H;return yt(this,function(_){switch(_.label){case 0:if(t=e[0],a=e[1],n=e[2],o=e[3],s=typeof o=="boolean"?{revalidate:o}:o||{},c=R(s.populateCache)?!0:s.populateCache,v=s.revalidate!==!1,i=s.rollbackOnError!==!1,l=s.optimisticData,h=Et(a),m=h[0],w=h[2],!m)return[2];if(k=ae.get(t),x=k[2],e.length<3)return[2,Ee(t,m,t.get(m),L,L,v,!0)];if(y=n,z=Ue(),x[m]=[z,0],A=!R(l),M=t.get(m),A&&(t.set(m,l),Ee(t,m,l)),ne(y))try{y=y(t.get(m))}catch(N){d=N}return y&&ne(y.then)?[4,y.catch(function(N){d=N})]:[3,2];case 1:if(y=_.sent(),z!==x[m][0]){if(d)throw d;return[2,y]}else d&&A&&i&&(c=!0,y=M,t.set(m,M));_.label=2;case 2:return c&&(d||(ne(c)&&(y=c(y,M)),t.set(m,y)),t.set(w,X(t.get(w),{error:d}))),x[m][1]=Ue(),[4,Ee(t,m,y,d,L,v,!!c)];case 3:if(H=_.sent(),d)throw d;return[2,c?H:y]}})})},It=function(e,r){for(var t in e)e[t][0]&&e[t][0](r)},Ot=function(e,r){if(!ae.has(e)){var t=X(Nr,r),a={},n=wt.bind(L,e),o=K;if(ae.set(e,[a,{},{},{},n]),!De){var s=t.initFocus(setTimeout.bind(L,It.bind(L,a,Tt))),c=t.initReconnect(setTimeout.bind(L,It.bind(L,a,St)));o=function(){s&&s(),c&&c(),ae.delete(e)}}return[e,n,o]}return[e,ae.get(e)[4]]},Mr=function(e,r,t,a,n){var o=t.errorRetryCount,s=n.retryCount,c=~~((Math.random()+.5)*(1<<(s<8?s:8)))*t.errorRetryInterval;!R(o)&&s>o||setTimeout(a,c,n)},Lt=Ot(new Map),_t=Lt[0],Hr=Lt[1],Dt=X({onLoadingSlow:K,onSuccess:K,onError:K,onErrorRetry:Mr,onDiscarded:K,revalidateOnFocus:!0,revalidateOnReconnect:!0,revalidateIfStale:!0,shouldRetryOnError:!0,errorRetryInterval:xt?1e4:5e3,focusThrottleInterval:5*1e3,dedupingInterval:2*1e3,loadingTimeout:xt?5e3:3e3,compare:function(e,r){return be(e)==be(r)},isPaused:function(){return!1},cache:_t,mutate:Hr,fallback:{}},Rr),Rt=function(e,r){var t=X(e,r);if(r){var a=e.use,n=e.fallback,o=r.use,s=r.fallback;a&&o&&(t.use=a.concat(o)),n&&s&&(t.fallback=X(n,s))}return t},Ge=u.createContext({}),Pr=function(e){var r=e.value,t=Rt(u.useContext(Ge),r),a=r&&r.provider,n=u.useState(function(){return a?Ot(a(t.cache||_t),r):L})[0];return n&&(t.cache=n[0],t.mutate=n[1]),xe(function(){return n?n[2]:L},[]),u.createElement(Ge.Provider,X(e,{value:t}))},jr=function(e,r){var t=u.useState({})[1],a=u.useRef(e),n=u.useRef({data:!1,error:!1,isValidating:!1}),o=u.useCallback(function(s){var c=!1,v=a.current;for(var i in s){var l=i;v[l]!==s[l]&&(v[l]=s[l],n.current[l]&&(c=!0))}c&&!r.current&&t({})},[]);return xe(function(){a.current=e}),[a,n.current,o]},kr=function(e){return ne(e[1])?[e[0],e[1],e[2]||{}]:[e[0],null,(e[1]===null?e[2]:e[1])||{}]},Wr=function(){return X(Dt,u.useContext(Ge))},Br=function(e){return function(){for(var t=[],a=0;a<arguments.length;a++)t[a]=arguments[a];var n=Wr(),o=kr(t),s=o[0],c=o[1],v=o[2],i=Rt(n,v),l=e,h=i.use;if(h)for(var m=h.length;m-- >0;)l=h[m](l);return l(s,c||i.fetcher,i)}},Nt=function(e,r,t){var a=r[e]||(r[e]=[]);return a.push(t),function(){var n=a.indexOf(t);n>=0&&(a[n]=a[a.length-1],a.pop())}},qe={dedupe:!0},Vr=function(e,r,t){var a=t.cache,n=t.compare,o=t.fallbackData,s=t.suspense,c=t.revalidateOnMount,v=t.refreshInterval,i=t.refreshWhenHidden,l=t.refreshWhenOffline,h=ae.get(a),m=h[0],w=h[1],k=h[2],x=h[3],y=Et(e),d=y[0],z=y[1],A=y[2],M=u.useRef(!1),H=u.useRef(!1),_=u.useRef(d),N=u.useRef(r),T=u.useRef(t),g=function(){return T.current},P=function(){return g().isVisible()&&g().isOnline()},C=function(B){return a.set(A,X(a.get(A),B))},de=a.get(d),Te=R(o)?t.fallback[d]:o,U=R(de)?Te:de,Re=a.get(A)||{},oe=Re.error,ve=!M.current,Ne=function(){return ve&&!R(c)?c:g().isPaused()?!1:s?!R(U):R(U)||t.revalidateIfStale},D=function(){return!d||!r?!1:Re.isValidating?!0:ve&&Ne()},G=D(),se=jr({data:U,error:oe,isValidating:G},H),W=se[0],he=se[1],me=se[2],J=u.useCallback(function(B){return gt(void 0,void 0,void 0,function(){var b,I,j,ie,ue,V,O,q,Q,Se,pe,Z,Ce;return yt(this,function(ce){switch(ce.label){case 0:if(b=N.current,!d||!b||H.current||g().isPaused())return[2,!1];ie=!0,ue=B||{},V=!x[d]||!ue.dedupe,O=function(){return!H.current&&d===_.current&&M.current},q=function(){var ee=x[d];ee&&ee[1]===j&&delete x[d]},Q={isValidating:!1},Se=function(){C({isValidating:!1}),O()&&me(Q)},C({isValidating:!0}),me({isValidating:!0}),ce.label=1;case 1:return ce.trys.push([1,3,,4]),V&&(Ee(a,d,W.current.data,W.current.error,!0),t.loadingTimeout&&!a.get(d)&&setTimeout(function(){ie&&O()&&g().onLoadingSlow(d,t)},t.loadingTimeout),x[d]=[b.apply(void 0,z),Ue()]),Ce=x[d],I=Ce[0],j=Ce[1],[4,I];case 2:return I=ce.sent(),V&&setTimeout(q,t.dedupingInterval),!x[d]||x[d][1]!==j?(V&&O()&&g().onDiscarded(d),[2,!1]):(C({error:L}),Q.error=L,pe=k[d],!R(pe)&&(j<=pe[0]||j<=pe[1]||pe[1]===0)?(Se(),V&&O()&&g().onDiscarded(d),[2,!1]):(n(W.current.data,I)?Q.data=W.current.data:Q.data=I,n(a.get(d),I)||a.set(d,I),V&&O()&&g().onSuccess(I,d,t),[3,4]));case 3:return Z=ce.sent(),q(),g().isPaused()||(C({error:Z}),Q.error=Z,V&&O()&&(g().onError(Z,d,t),(typeof t.shouldRetryOnError=="boolean"&&t.shouldRetryOnError||ne(t.shouldRetryOnError)&&t.shouldRetryOnError(Z))&&P()&&g().onErrorRetry(Z,d,t,J,{retryCount:(ue.retryCount||0)+1,dedupe:!0}))),[3,4];case 4:return ie=!1,Se(),O()&&V&&Ee(a,d,Q.data,Q.error,!1),[2,!0]}})})},[d]),Fe=u.useCallback(wt.bind(L,a,function(){return _.current}),[]);if(xe(function(){N.current=r,T.current=t}),xe(function(){if(!!d){var B=d!==_.current,b=J.bind(L,qe),I=function(O,q,Q){me(X({error:q,isValidating:Q},n(W.current.data,O)?L:{data:O}))},j=0,ie=function(O){if(O==Tt){var q=Date.now();g().revalidateOnFocus&&q>j&&P()&&(j=q+g().focusThrottleInterval,b())}else if(O==St)g().revalidateOnReconnect&&P()&&b();else if(O==Ct)return J()},ue=Nt(d,w,I),V=Nt(d,m,ie);return H.current=!1,_.current=d,M.current=!0,B&&me({data:U,error:oe,isValidating:G}),Ne()&&(R(U)||De?b():Fr(b)),function(){H.current=!0,ue(),V()}}},[d,J]),xe(function(){var B;function b(){var j=ne(v)?v(U):v;j&&B!==-1&&(B=setTimeout(I,j))}function I(){!W.current.error&&(i||g().isVisible())&&(l||g().isOnline())?J(qe).then(b):b()}return b(),function(){B&&(clearTimeout(B),B=-1)}},[v,i,l,J]),u.useDebugValue(U),s&&R(U)&&d)throw N.current=r,T.current=t,H.current=!1,R(oe)?J(qe):oe;return{mutate:Fe,get data(){return he.data=!0,U},get error(){return he.error=!0,oe},get isValidating(){return he.isValidating=!0,G}}};ye.defineProperty(Pr,"default",{value:Dt});var $r=Br(Vr),Ft={};Object.defineProperty(Ft,"__esModule",{value:!0});var Ur=Ft.default=Gr;function Gr(e,r){for(const t in e)if(typeof e[t]===r)return e[t]}function qr(e){return(r,t,a)=>{const n=u.useRef(),o=e(r,t,a);u.useEffect(()=>{o.data!==void 0&&(n.current=o.data)},[o.data]);const s=u.useCallback(()=>{n.current=void 0},[]),c=o.data===void 0?n.current:o.data,v=o.data===void 0&&n.current!==void 0;return Object.assign({},o,{data:c,isLagging:v,resetLaggy:s})}}const Qr=(e,r)=>typeof e=="function"?e:()=>Promise.resolve(Array.isArray(e)?e:[we(Y({},e),{name:"",ratio:r})]),zr=(e,r)=>{const{data:t,displayField:a,searchType:n}=e;switch(n&&n.toLowerCase()){case"startswith":return t.filter(s=>Qe(s,a).toLowerCase().startsWith(r.toLowerCase()));case"contains":return t.filter(s=>Qe(s,a).toLowerCase().includes(r.toLowerCase()));default:return t}},Kr=(e,r,t)=>{const a=r.map(i=>i.ratio||1),n=a.reduce((i,l)=>i+l,0),o=t/n,s=e.flat().length,c=[];let v=s<t?s:t;for(;v>0;)e=e.map((i,l)=>(c[l]?c[l]<i.length&&(v-=++c[l]):(c[l]=Math.round(a[l]*o),c[l]>i.length&&(c[l]=i.length),v=v-c[l]),i));return e.map((i,l)=>i.slice(0,c[l]))},Qe=(e,r)=>{const a=typeof e==="string"&&le(r)?e:e[r];return le(a)?Ur(e,"string")||"":a},Jr=e=>{const r={use:[qr]};return e?we(Y({},r),{revalidateIfStale:!1,revalidateOnFocus:!1,revalidateOnReconnect:!1}):r},Yr=(e,r,t,a,n)=>{if(t&&e.length>0&&e.length<a)return[];if(!t&&e.length<a)return[];const o=t&&!e.length;return Qr(o?t:r,n)(e).then(c=>{const v=c.map(i=>typeof i.data=="function"?i.data(e):Promise.resolve(zr(i,e)));return Promise.all(v).then(i=>(i=i.reduce((l,h,m)=>{const{id:w,name:k,displayField:x,searchType:y}=c[m];return[...l,h.map(d=>({value:d,text:Qe(d,x),groupIndex:m,groupId:w,groupName:k,searchType:y,displayField:x,defaultListbox:o}))]},[]),i.length&&(i=Kr(i,c,n)),i.flat()))})},Xr=(e,r,t,a,n,o)=>{const s="X";return $r([e.toLowerCase(),s],v=>Yr(v,t,a,n,o),Jr(r))},fe={container:{position:"relative",textAlign:"left"},containerFocus:{position:"relative",textAlign:"left"},query:{position:"relative",zIndex:1,backgroundColor:"transparent"},queryNoTypeahead:{position:"relative"},typeahead:{position:"absolute",zIndex:0,top:0,left:0},clearButton:{zIndex:2},cancelButton:{zIndex:3}};var Zr=["textarea","text","search","tel","url","password"],en=function(e){return!!(e.setSelectionRange&&~Zr.indexOf(e.type))},tn=en,At=function(e,r){var t=tn(e)&&e===document.activeElement;if(t){var a=e.selectionStart,n=e.selectionEnd;e.value=r,e.setSelectionRange(a,n)}else e.value=r};const rn=(e,r)=>typeof e!="string"||typeof r!="string"?!1:e.toLowerCase().startsWith(r.toLowerCase()),nn=e=>{const{dispatch:r}=u.useContext(re);u.useEffect(()=>{r(Zt(e||[]))},[e,r])},an=e=>{const{dispatch:r}=u.useContext(re);u.useEffect(()=>{e&&r(er())},[e,r])},on=(e,r,t,a)=>{const n=u.useRef(!0);u.useEffect(()=>{const o=!!t.current,s=(()=>{const c=o?t.current.value:"";return!e||!c.startsWith(e)?"":c})();o&&(t.current.value=s),At(r.current,e),n.current&&!e||typeof a=="function"&&a(e),n.current=!1},[e,a,r,t])},sn=(e,r,t,a)=>{u.useEffect(()=>{const n=e&&r&&t.current.value.length>0&&rn(e.text,t.current.value)?e.text:"",o=cn(t.current.value,n);a.current&&(a.current.value=n),At(t.current,o)},[e,r,t,a])},un=(e,r,t,a)=>{const n=u.useRef(!0);u.useEffect(()=>{let o,s;le(e)?(o=F,s=F):(t.current&&(t.current.value=""),r.current.blur(),o=e.value,s=e.displayField),n.current&&le(e)||typeof a=="function"&&a(o,s),n.current=!1},[e,a,r,t])},cn=(e,r)=>{const t=r.substring(0,e.length);return t.length>0&&e.toLowerCase()===t.toLowerCase()&&e!==t?t:e},Mt=E.default.forwardRef((e,r)=>{const{autoFocus:t,cancelButton:a,cancelButtonAriaLabel:n,clearButton:o,clearButtonAriaLabel:s,debounceWait:c,defaultListbox:v,defaultListboxIsImmutable:i,disabled:l,enterKeyHint:h,errorMessage:m,id:w,listbox:k,listboxIsImmutable:x,maxItems:y,minQueryLength:d,name:z,noItemsMessage:A,onBlur:M,onChange:H,onEnter:_,onFocus:N,onSelect:T,onTab:g,placeholder:P,styles:C,tabIndex:de,text:Te,typeahead:U,Cancel:Re,Clear:oe}=e,ve=`${w}-listbox`,Ne=`${w}-errorbox`,{state:D,dispatch:G}=u.useContext(re),[se]=Er(D.query,c),[W,he]=u.useState(!1),[me,J]=u.useState(!1),[Fe,B]=u.useState(!!Te),b=u.useRef(null),I=u.useRef(null),j=U&&D.items.length>1,ie=o&&!!D.query,ue=a&&W,V=W&&D.canShowListbox,O=!!e.errorMessage&&D.itemsError,q=W?"containerFocus":"container",Q=C[q]||C.container,Se=C[q]?F:fe[q],Z=C[W?"inputFocus":"input"]||C.input,Ce=j?fe.query:fe.queryNoTypeahead,ce=(()=>x&&!(v&&!i&&se.length===0))(),ee=Xr(se?se.toLowerCase():"",ce,k,v,d,y);nn(ee.data),u.useEffect(()=>{var te;Fe&&ee.data&&((te=ee.data[0])==null?void 0:te.text)===Te&&(G(Oe(ee.data[0])),B(!1))},[Fe,ee.data,Te,G]),an(ee.error),on(D.query,b,I,H),sn(D.highlighted,W,b,I),un(D.selected,b,I,T);const kt=te=>{const Je=D.highlighted&&D.highlighted.index,Bt=le(Je)?F:D.items[Je],Vt=te.toLowerCase()==="enter"?_:g;Bt&&G(Oe(Je)),typeof Vt=="function"&&Vt(b.current.value,Bt)},ln=te=>{switch(te.keyCode){case 40:G(nr());break;case 38:G(rr());break;case 13:kt("enter");break;case 9:kt("tab");break;case 27:Ae();break}},Wt=()=>{G(it(b.current.value))},fn=()=>{J(!0),Ae()},dn=()=>{Ae()},Ae=()=>{b.current.value="",U&&I.current&&(I.current.value=""),G(tr()),b.current.focus()},vn=()=>{W||(he(!0),D.items&&D.items.length>0&&G(ut(0)),typeof N=="function"&&N())},hn=()=>{me?b.current.focus():(he(!1),typeof M=="function"&&M()),J(!1)};return u.useImperativeHandle(r,()=>({focus:()=>{b.current.focus()},blur:()=>{b.current.blur()},select:()=>{b.current.select()},clear:()=>{Ae()},query:te=>{typeof te=="string"&&(b.current.value=te,b.current.focus(),Wt())}})),p(E.default.Fragment,{children:vt("div",{className:Q,style:Se,role:"combobox","aria-expanded":V,"aria-owns":ve,"aria-haspopup":"listbox",children:[p("input",{id:w,name:z,className:`${Z||""} ${C.query||""}`.trim(),style:Ce,disabled:l,placeholder:P,type:"text",autoFocus:t,autoComplete:"off",autoCorrect:"off",autoCapitalize:"off",spellCheck:"false",tabIndex:de,enterKeyHint:h,ref:b,onKeyDown:ln,onInput:Wt,onFocus:vn,onBlur:hn,"aria-autocomplete":"both","aria-controls":ve}),j&&p("input",{className:`${Z||""} ${C.typeahead||""}`.trim(),style:fe.typeahead,disabled:l,type:"text",autoComplete:"off",autoCorrect:"off",autoCapitalize:"off",spellCheck:"false",tabIndex:"-1",readOnly:"readonly","aria-hidden":"true",ref:I}),ie&&p("button",{className:C.clearButton,style:fe.clearButton,onMouseDown:fn,tabIndex:-1,"aria-label":s,children:p(oe,{})}),ue&&p("button",{className:C.cancelButton,style:fe.cancelButton,onMouseDown:dn,tabIndex:-1,"aria-label":n,children:p(Re,{})}),V&&p(pr,{id:ve,items:D.items,noItemsMessage:A,styles:C}),O&&p(gr,{id:Ne,errorMessage:m,styles:C})]})})});Mt.displayName="Container";const ze={autoFocus:!1,cancelButton:!1,cancelButtonAriaLabel:"Cancel",clearButton:!1,clearButtonAriaLabel:"Clear contents",debounceWait:250,defaultListboxIsImmutable:!0,disabled:!1,id:(()=>`turnstone-${(0|Math.random()*604e5).toString(36)}`)(),listboxIsImmutable:!0,matchText:!1,maxItems:10,minQueryLength:1,placeholder:"",styles:{},typeahead:!0,Cancel:()=>"Cancel",Clear:()=>"\xD7"},Ht=(e,r,t,a)=>{const n=Array.isArray(r.plugins)&&r.plugins[t];if(n){const[o,s]=Array.isArray(n)?n:[n];return p(o,we(Y({ref:a},s),{Component:e,componentProps:r,pluginIndex:t,render:Ht}))}return p(e,Y({ref:a},r))},Ke=E.default.forwardRef((e,r)=>{const t=Y(Y({},ze),e);return p(E.default.StrictMode,{children:p(dr,we(Y({},t),{children:Ht(Mt,t,0,r)}))})});Ke.displayName="Turnstone";const Pt=["startswith","contains"],jt=f.oneOfType([f.arrayOf(f.exact({data:f.oneOfType([f.func,f.array]).isRequired,searchType:f.oneOf(Pt),displayField:f.oneOfType([f.number,f.string]),id:f.string,name:f.string.isRequired,ratio:f.number})),f.exact({data:f.oneOfType([f.func,f.array]).isRequired,searchType:f.oneOf(Pt),displayField:f.oneOfType([f.number,f.string])}),f.func]);return Ke.propTypes={autoFocus:f.bool,cancelButton:f.bool,cancelButtonAriaLabel:f.string,cancelButtonText:f.string,clearButton:f.bool,clearButtonAriaLabel:f.string,clearButtonText:f.string,debounceWait:f.number,defaultListbox:jt,defaultListboxIsImmutable:f.bool,disabled:f.bool,enterKeyHint:f.oneOf(["enter","done","go","next","previous","search","send"]),errorMessage:f.string,id:f.string,listbox:jt.isRequired,listboxIsImmutable:f.bool,matchText:f.bool,maxItems:f.number,minQueryLength:e=>{if(f.checkPropTypes({minQueryLength:f.number},{minQueryLength:e.minQueryLength},"prop","Turnstone"),e.minQueryLength<ze.minQueryLength)return new Error(`Prop "minQueryLength" must be a number greater than ${ze.minQueryLength-1}`)},name:f.string,noItemsMessage:f.string,onBlur:f.func,onChange:f.func,onEnter:f.func,onFocus:f.func,onSelect:f.func,onTab:f.func,placeholder:f.string,plugins:f.array,styles:f.object,tabIndex:f.number,text:f.string,typeahead:f.bool,Cancel:f.elementType,Clear:f.elementType,Item:f.elementType,GroupName:f.elementType},Ke});