UNPKG

@hitachivantara/uikit-react-core

Version:

Core React components for the NEXT Design System.

55 lines (54 loc) 1.5 kB
const cycleThroughSelection = (selectedState, index, allValues, selectionCheck) => { const newValue = []; selectedState.forEach((isSelected, i) => { if (i === index) { if (selectionCheck || !isSelected) { newValue.push(allValues[i]); } } else if (isSelected) { newValue.push(allValues[i]); } }); return newValue; }; const multiSelectionEventHandler = (evt, index, selectionAnchor, allValues, selectedState, selectionCheck) => { let newValue = []; if (evt.nativeEvent.shiftKey) { if (selectionAnchor.current === void 0) { selectionAnchor.current = index; newValue.push(allValues[index]); } else { let selectionStart; let selectionEnd; if (selectionAnchor.current > index) { selectionStart = index; selectionEnd = selectionAnchor.current + 1; } else { selectionStart = selectionAnchor.current; selectionEnd = index + 1; } const selectedValues = allValues.slice(selectionStart, selectionEnd); newValue.push(...selectedValues); } } else if (evt.nativeEvent.metaKey) { selectionAnchor.current = index; newValue = cycleThroughSelection( selectedState, index, allValues, selectionCheck ); } else { selectionAnchor.current = index; newValue = cycleThroughSelection( selectedState, index, allValues, selectionCheck ); } return newValue; }; export { multiSelectionEventHandler };