turnstone-recent-searches
Version:
A plugin for the Turnstone React component. Display Recent Searches in the default listbox.
2 lines (1 loc) • 1.6 kB
JavaScript
var j=Object.defineProperty,v=Object.defineProperties;var w=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,J=Object.prototype.propertyIsEnumerable;var y=(e,n,t)=>n in e?j(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,m=(e,n)=>{for(var t in n||(n={}))D.call(n,t)&&y(e,t,n[t]);if(p)for(var t of p(n))J.call(n,t)&&y(e,t,n[t]);return e},S=(e,n)=>v(e,w(n));(function(e,n){typeof exports=="object"&&typeof module!="undefined"?module.exports=n(require("react")):typeof define=="function"&&define.amd?define(["react"],n):(e=typeof globalThis!="undefined"?globalThis:e||self,e["turnstone-recent-searches"]=n(e.React))})(this,function(e){"use strict";function n(i){return i&&typeof i=="object"&&"default"in i?i:{default:i}}var t=n(e);const f=t.default.forwardRef((i,h)=>{const{Component:b,componentProps:l,pluginIndex:g,render:_,ratio:x=1,id:C,name:F="Recent Searches",storageKey:a="recentSearches",limit:d=10}=i,{defaultListbox:I=[],onSelect:s}=l,c=e.useCallback(()=>JSON.parse(localStorage.getItem(a))||[],[a]),u=e.useCallback(o=>{const r=[o,...c().filter(N=>N._displayField!==o._displayField)];localStorage.setItem(a,JSON.stringify(r.slice(0,d)))},[a,d,c]),L=()=>[{id:C,name:F,displayField:"_displayField",data:()=>Promise.resolve(c()),ratio:x},...I],P=e.useCallback((o,r)=>{o&&(typeof o=="string"?o={_displayField:o}:o._displayField=o[r],u(o)),typeof s=="function"&&s(o,r)},[u,s]),k=S(m({},l),{defaultListbox:L(),defaultListboxIsImmutable:!1,onSelect:P});return _(b,k,g+1,h)});return f.displayName="RecentSearchesPlugin",f});