UNPKG

@momentum-ui/react-collaboration

Version:

Cisco Momentum UI Framework for React Collaboration Applications

55 lines 2.01 kB
import React, { useContext } from 'react'; import { isNumber } from 'lodash'; export var ListContext = React.createContext(null); export var useListContext = function () { return useContext(ListContext); }; /** * Calculate the next (or previous) index of a list * * @param isBackward Increase or decrease the index * @param listSize List size * @param currentFocus Current index * @param noLoop loop back to the front or the back of the list * @param allItemIndexes available indexes in order */ export var setNextFocus = function (currentFocus, listSize, isBackward, noLoop, allItemIndexes) { var nextIndex; var currentIndex; if (!allItemIndexes) { if (!isNumber(currentFocus)) { console.warn('Cannot handle non-numeric index without allItemIndexes'); return; } currentIndex = currentFocus; } else { currentIndex = allItemIndexes.indexOf(currentFocus); } if (isBackward) { nextIndex = (listSize + currentIndex - 1) % listSize; if (noLoop && nextIndex > currentIndex) { return; } } else { nextIndex = (listSize + currentIndex + 1) % listSize; if (noLoop && nextIndex < currentIndex) { return; } } if (allItemIndexes) { nextIndex = allItemIndexes[nextIndex]; } return nextIndex; }; export var onCurrentFocusNotFound = function (currentFocus, allItemIndexes, previousAllItemIndexes) { var previousIndexOfCurrentFocus = previousAllItemIndexes.indexOf(currentFocus); var allItemIndexesPositions = allItemIndexes.map(function (itemIndex) { return previousAllItemIndexes.indexOf(itemIndex); }); var differences = allItemIndexesPositions.map(function (position) { return Math.abs(position - previousIndexOfCurrentFocus); }); var nextIndex = allItemIndexes[differences.lastIndexOf(Math.min.apply(Math, differences))]; return nextIndex; }; //# sourceMappingURL=List.utils.js.map