UNPKG

use-item-list

Version:

Manage indexed collections in React using hooks.

3 lines (2 loc) 4.68 kB
"use strict";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 c=0;exports.useItemList=function(e){var o=void 0===e?{}:e,l=o.id,f=void 0===l?c++:l,s=o.initialHighlightedIndex,d=void 0===s?0:s,a=o.onHighlight,g=o.onSelect,h=o.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([]),p=t.useRef(!0),b=t.useRef(!1),L=t.useCallback((function(e){var t=e.ref,n=e.text,r=e.value,u=e.disabled,i=y.current.findIndex((function(e){return e.value===r}));return-1===i&&y.current.length>0&&!1===p.current?b.current=!0:-1===i&&y.current.push({id:H(i=y.current.length),ref:t,text:n,value:r,disabled:u}),i}),[]);y.current=[],p.current=!0,u((function(){_.current=y.current,p.current=!1})),t.useEffect((function(){function e(e){var t=_.current[e];g&&t&&g(t,e)}return E.on("SELECT_ITEM",e),function(){E.off("SELECT_ITEM",e)}}),[g]);var M=t.useRef(null);function G(e){return"function"==typeof M.current?M.current(e):M.current===e}function k(e){C.current=e,E.emit("HIGHLIGHT_ITEM",e),a&&a(_.current[e],e)}function R(e){var t,n;return null!=(t=null==(n=_.current[e])?void 0:n.id)?t:null}M.current=I;var x=t.useCallback((function(){var e=t.useState((function(){return R(C.current)})),n=e[0],r=e[1];return t.useEffect((function(){function e(e){r(R(e))}return E.on("HIGHLIGHT_ITEM",e),function(){E.off("HIGHLIGHT_ITEM",e)}}),[]),n}),[]),w=t.useRef(""),S=t.useRef(null),P=t.useCallback((function(e){var c=e.ref,o=e.text,l=e.value,f=e.disabled,s=void 0!==f&&f,d=n((function(){return r()})),a=i(),g=L({ref:c,text:o,value:l,disabled:s}),h=t.useRef(g);u((function(){b.current&&(m(),b.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==c?void 0:c.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&&k(h.current)},select:function(){!1===s&&E.emit("SELECT_ITEM",h.current)},selected:G(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])?e:null},setHighlightedItem:k,moveHighlightedItem:function(e,t){var n=(void 0===t?{}:t).contain,r=void 0===n||n,u=_.current.length,i=C.current;k(i=null===i?e>=0?0:u-1:function t(n){var i,c,o=n+e;return(o<0||o>=u)&&(o=r?(c=u,(i=C.current+e)>=0?i%c:(i%c+c)%c):null),_.current[o].disabled&&(o=t(o)),o}(i))},clearHighlightedItem:function(){k(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(),w.current+=e.key.toLowerCase(),function(e){clearTimeout(S.current),S.current=setTimeout((function(){w.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)){k(t);break}}}(w.current))},useItem:P}}; //# sourceMappingURL=use-item-list.cjs.production.min.js.map