Skip to content

Commit

Permalink
refactor: simplify getRawStartAndEndIndexes
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienIzz committed Aug 29, 2024
1 parent ed0b15d commit 75dc0f7
Showing 1 changed file with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,33 @@ const getRawStartAndEndIndexes = ({
}) => {
const halfNumberOfItemsNotVisible = numberOfItemsNotVisible / 2;

let rawStartIndex: number;
let rawEndIndex: number;

switch (scrollBehavior) {
case 'stick-to-start':
rawStartIndex = currentlyFocusedItemIndex - halfNumberOfItemsNotVisible;
rawEndIndex =
currentlyFocusedItemIndex + numberOfItemsVisibleOnScreen - 1 + halfNumberOfItemsNotVisible;
break;
return {
rawStartIndex: currentlyFocusedItemIndex - halfNumberOfItemsNotVisible,
rawEndIndex:
currentlyFocusedItemIndex +
numberOfItemsVisibleOnScreen -
1 +
halfNumberOfItemsNotVisible,
};
case 'stick-to-end':
rawStartIndex =
currentlyFocusedItemIndex - numberOfItemsVisibleOnScreen + 1 - halfNumberOfItemsNotVisible;
rawEndIndex = currentlyFocusedItemIndex + halfNumberOfItemsNotVisible;
break;
return {
rawStartIndex:
currentlyFocusedItemIndex -
numberOfItemsVisibleOnScreen +
1 -
halfNumberOfItemsNotVisible,
rawEndIndex: currentlyFocusedItemIndex + halfNumberOfItemsNotVisible,
};
case 'jump-on-scroll':
rawStartIndex = currentlyFocusedItemIndex - numberOfItemsVisibleOnScreen;
rawEndIndex = currentlyFocusedItemIndex + numberOfItemsVisibleOnScreen;
break;
return {
rawStartIndex: currentlyFocusedItemIndex - numberOfItemsVisibleOnScreen,
rawEndIndex: currentlyFocusedItemIndex + numberOfItemsVisibleOnScreen,
};
default:
throw new Error(`Unknown scroll behavior: ${scrollBehavior}`);
}

return { rawStartIndex, rawEndIndex };
};

/**
Expand Down

0 comments on commit 75dc0f7

Please sign in to comment.