UNPKG

monday-ui-react-core

Version:

Official monday.com UI resources for application development in React.js

3 lines (2 loc) 2.15 kB
import{slicedToArray as e}from"../../../_virtual/_rollupPluginBabelHelpers.js";import{useState as n,useRef as o,useContext as t,useEffect as i,useCallback as r}from"react";import{GridKeyboardNavigationContext as u}from"../../components/GridKeyboardNavigationContext/GridKeyboardNavigationContext.js";import a from"../useFullKeyboardListeners.js";import c from"../useEventListener/index.js";import{getActiveIndexFromInboundNavigation as s,calcActiveIndexAfterArrowNavigation as f}from"./gridKeyboardNavigationHelper.js";import{useLastNavigationDirection as d}from"../../components/Menu/Menu/hooks/useLastNavigationDirection.js";var l=-1;function m(m){var v=m.ref,b=m.itemsCount,I=m.numberOfItemsInLine,p=m.onItemClicked,x=m.getItemByIndex,g=void 0===x?function(e){}:x,N=m.focusOnMount,O=void 0!==N&&N,y=m.focusItemIndexOnMount,j=void 0===y?l:y,C=m.disabledIndexes,L=void 0===C?[]:C,k=n(O&&j!==l),K=e(k,2),M=K[0],A=K[1],S=o(!1),h=n(M?j:l),w=e(h,2),B=w[0],D=w[1],E=n(!0),G=e(E,2),H=G[0],_=G[1],F=t(u);i((function(){S.current?A(!1):S.current=!0}),[B]);var P=r((function(){var e;return null===(e=v.current)||void 0===e?void 0:e.blur()}),[v]),R=d().lastNavigationDirectionRef,T=r((function(){var e=R.current;if(e){if(-1===B){var n=s({direction:e,numberOfItemsInLine:I,itemsCount:b});D(n)}_(!0)}else B===l&&D(0)}),[B,b,R,I]),q=r((function(){_(!1)}),[_]),z=r((function(){_(!0),D(l)}),[D]);c({eventName:"focus",callback:T,ref:v}),c({eventName:"mousedown",callback:q,ref:v}),c({eventName:"blur",callback:z,ref:v}),i((function(){var e;B>-1&&(null===(e=v.current)||void 0===e||e.focus())}),[B,v]);var J=r((function(e){_(arguments.length>1&&void 0!==arguments[1]&&arguments[1]),D(e),p(g(e),e)}),[D,p,g]),Q=r((function(){if(H)return J(B,!0)}),[H,J,B]);return a({ref:v,onSelectionKey:Q,onArrowNavigation:function(e){if(_(!0),B!==l){var n=f({activeIndex:B,itemsCount:b,numberOfItemsInLine:I,direction:e,disabledIndexes:L}),o=n.nextIndex;n.isOutbound?null==F||F.onOutboundNavigation(v,e):D(o)}else D(0)},onEscape:P,focusOnMount:O}),{activeIndex:H?B:l,onSelectionAction:J,isInitialActiveState:M}}export{m as default}; //# sourceMappingURL=useGridKeyboardNavigation.js.map