UNPKG

@itwin/itwinui-react

Version:

A react component library for iTwinUI

56 lines (55 loc) 1.59 kB
import { actions } from 'react-table'; actions.setScrolledLeft = 'setScrolledLeft'; actions.setScrolledRight = 'setScrolledRight'; export const useStickyColumns = (hooks) => { hooks.stateReducers.push(reducer); hooks.useInstance.push(useInstance); }; let reducer = (newState, action) => { if (action.type === actions.init) return { ...newState, sticky: {}, }; if ( action.type === actions.setScrolledLeft && newState.sticky?.isScrolledToLeft !== action.value ) return { ...newState, sticky: { ...newState.sticky, isScrolledToLeft: action.value, }, }; if ( action.type === actions.setScrolledRight && newState.sticky?.isScrolledToRight !== action.value ) return { ...newState, sticky: { ...newState.sticky, isScrolledToRight: action.value, }, }; return newState; }; let useInstance = (instance) => { let { flatHeaders } = instance; flatHeaders.forEach((header) => { if (!header.originalSticky) header.originalSticky = header.sticky ?? 'none'; header.sticky = 'none' === header.originalSticky ? void 0 : header.originalSticky; }); let hasLeftStickyColumn = false; [...flatHeaders].reverse().forEach((header) => { if ('left' === header.sticky) hasLeftStickyColumn = true; if (hasLeftStickyColumn) header.sticky = 'left'; }); let hasRightStickyColumn = false; flatHeaders.forEach((header) => { if ('right' === header.sticky) hasRightStickyColumn = true; if (hasRightStickyColumn) header.sticky = 'right'; }); };