5app-use-item-list
Version:
Manage indexed collections in React using hooks.
3 lines (2 loc) • 4.68 kB
JavaScript
;function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),n=e(require("use-constant")),r=e(require("mitt")),u="undefined"!=typeof window?t.useEffect:t.useLayoutEffect;function i(){var e=t.useState()[1];return t.useCallback((function(){return e(Object.create(null))}),[])}var o=0;exports.useItemList=function(e){var c=void 0===e?{}:e,l=c.id,f=void 0===l?o++:l,s=c.initialHighlightedIndex,d=void 0===s?0:s,a=c.onHighlight,g=c.onSelect,h=c.selected,I=void 0===h?null:h,v=t.useRef("controller-"+f),T=t.useRef("list-"+f),H=function(e){return T.current+"-item-"+e},E=n((function(){return r()})),m=i(),C=t.useRef(d),_=t.useRef([]),y=t.useRef(!0),p=t.useRef(!1),b=t.useCallback((function(e){var t=e.ref,n=e.text,r=e.value,u=e.disabled,i=_.current.findIndex((function(e){return e.value===r}));return-1===i&&_.current.length>0&&!1===y.current?p.current=!0:-1===i&&_.current.push({id:H(i=_.current.length),ref:t,text:n,value:r,disabled:u}),i}),[]);_.current=[],y.current=!0,u((function(){y.current=!1})),t.useEffect((function(){function e(e){g(_.current[e]||{},e)}return E.on("SELECT_ITEM",e),function(){E.off("SELECT_ITEM",e)}}),[g]);var L=t.useRef(null);function M(e){return"function"==typeof L.current?L.current(e):L.current===e}function G(e){C.current=e,E.emit("HIGHLIGHT_ITEM",e),a&&a(_.current[e],e)}function k(e){var t,n;return null!==(t=null===(n=_.current[e])||void 0===n?void 0:n.id)&&void 0!==t?t:null}L.current=I;var x=t.useCallback((function(){var e=t.useState((function(){return k(C.current)})),n=e[0],r=e[1];return t.useEffect((function(){function e(e){r(k(e))}return E.on("HIGHLIGHT_ITEM",e),function(){E.off("HIGHLIGHT_ITEM",e)}}),[]),n}),[]),R=t.useRef(""),w=t.useRef(null),S=t.useCallback((function(e){var o=e.ref,c=e.text,l=e.value,f=e.disabled,s=void 0!==f&&f,d=n((function(){return r()})),a=i(),g=b({ref:o,text:c,value:l,disabled:s}),h=t.useRef(g);u((function(){p.current&&(m(),p.current=!1)})),u((function(){h.current!==g&&(h.current=g,a()),d.emit("UPDATE_ITEM_INDEX",g)}),[g]),t.useEffect((function(){function e(e){if(h.current===e){var t=null==o?void 0:o.current;t&&function(e){var t=function e(t){return null!==t?t===document.body||t.scrollHeight>t.clientHeight?function(e){return e===document.body?{offsetTop:0,scrollY:window.pageYOffset,height:window.innerHeight,setPosition:function(e){return window.scrollTo(0,e)}}:{offsetTop:e.getBoundingClientRect().top,scrollY:e.scrollTop,height:e.offsetHeight,setPosition:function(t){return e.scrollTop=t}}}(t):e(t.parentNode):null}(e);if(null!==t){var n=e.getBoundingClientRect(),r=t.scrollY+(n.top-t.offsetTop);r<t.scrollY?t.setPosition(r):r+n.height>t.scrollY+t.height&&t.setPosition(r+n.height-t.height)}}(t)}}return E.on("HIGHLIGHT_ITEM",e),function(){E.off("HIGHLIGHT_ITEM",e)}}),[]);var I=t.useCallback((function(){var e=t.useState(null),n=e[0],r=e[1];return u((function(){function e(e){r(h.current===e)}function t(e){var t=C.current===e;n!==t&&r(t)}return E.on("HIGHLIGHT_ITEM",e),d.on("UPDATE_ITEM_INDEX",t),function(){E.off("HIGHLIGHT_ITEM",e),d.off("UPDATE_ITEM_INDEX",t)}}),[]),u((function(){var e=h.current===C.current;n!==e&&r(e)})),n}),[]);return{id:H(g),index:h.current,highlight:function(){!1===s&&G(h.current)},select:function(){!1===s&&E.emit("SELECT_ITEM",h.current)},selected:M(l),useHighlighted:I}}),[]);return{controllerId:v.current,listId:T.current,items:_,getHighlightedIndex:function(){return C.current},getHighlightedItem:function(){var e;return null!==(e=_.current[C.current])&&void 0!==e?e:null},setHighlightedItem:G,moveHighlightedItem:function(e,t){var n=(void 0===t?{}:t).contain,r=void 0===n||n,u=_.current.length,i=C.current;G(i=null===i?e>=0?0:u-1:function t(n){var i,o,c=n+e;return(c<0||c>=u)&&(c=r?(o=u,(i=C.current+e)>=0?i%o:(i%o+o)%o):null),_.current[c].disabled&&(c=t(c)),c}(i))},clearHighlightedItem:function(){G(null)},selectHighlightedItem:function(){null!==C.current&&E.emit("SELECT_ITEM",C.current)},useHighlightedItemId:x,highlightItemByString:function(e,t){void 0===t&&(t=300),(function(e){return e.keyCode>=48&&e.keyCode<=57}(e)||function(e){return e.keyCode>=65&&e.keyCode<=90}(e)||function(e){return e.keyCode>=188&&e.keyCode<=190}(e))&&!function(e){return e.ctrlKey||e.metaKey||e.altKey}(e)&&(e.preventDefault(),R.current+=e.key.toLowerCase(),function(e){clearTimeout(w.current),w.current=setTimeout((function(){R.current=""}),e)}(t),function(e){for(var t=0;t<_.current.length;t++){var n=_.current[t];if(0===(n.text||String(n.value)).toLowerCase().indexOf(e)){G(t);break}}}(R.current))},useItem:S}};
//# sourceMappingURL=app-use-item-list.cjs.production.min.js.map