Skip to content

Commit 53a3778

Browse files
committed
wip
1 parent d798073 commit 53a3778

File tree

3 files changed

+204
-4
lines changed

3 files changed

+204
-4
lines changed

packages/lib/src/spatial-navigation/components/virtualizedList/helpers/computeTranslation.test.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,138 @@ describe('computeTranslation for virtualized list with stick-to-end scroll behav
290290
});
291291
});
292292

293+
describe('computeTranslation for virtualized list with stick-to-center scroll behavior', () => {
294+
const scrollBehavior = 'stick-to-center';
295+
296+
it('should not scroll if currentlyFocusedItemIndex < numberOfItemsVisibleOnScreen', () => {
297+
const expectedResult = computeTranslation({
298+
itemSizeInPx,
299+
currentlyFocusedItemIndex: 3,
300+
numberOfItemsVisibleOnScreen: 4,
301+
nbMaxOfItems: 11,
302+
scrollBehavior: scrollBehavior,
303+
data,
304+
listSizeInPx: 40,
305+
maxPossibleLeftAlignedIndex: 7,
306+
maxPossibleRightAlignedIndex: 3,
307+
});
308+
309+
expect(expectedResult).toEqual(-0);
310+
});
311+
312+
it('should not scroll if currentlyFocusedItemIndex < numberOfItemsVisibleOnScreen with dynamic sizes', () => {
313+
const expectedResult = computeTranslation({
314+
itemSizeInPx: itemSizeInPxFunction,
315+
currentlyFocusedItemIndex: 3,
316+
numberOfItemsVisibleOnScreen: 4,
317+
nbMaxOfItems: 11,
318+
scrollBehavior: scrollBehavior,
319+
data,
320+
listSizeInPx: 40,
321+
maxPossibleLeftAlignedIndex: 7,
322+
maxPossibleRightAlignedIndex: 3,
323+
});
324+
325+
expect(expectedResult).toEqual(-0);
326+
});
327+
328+
it('should end-align focused item', () => {
329+
const expectedResult = computeTranslation({
330+
itemSizeInPx,
331+
currentlyFocusedItemIndex: 6,
332+
numberOfItemsVisibleOnScreen: 4,
333+
nbMaxOfItems: 11,
334+
scrollBehavior: scrollBehavior,
335+
data,
336+
listSizeInPx: 40,
337+
maxPossibleLeftAlignedIndex: 7,
338+
maxPossibleRightAlignedIndex: 3,
339+
});
340+
341+
expect(expectedResult).toEqual(-30);
342+
});
343+
344+
it('should end-align focused item with dynamic sizes', () => {
345+
const expectedResult = computeTranslation({
346+
itemSizeInPx: itemSizeInPxFunction,
347+
currentlyFocusedItemIndex: 6,
348+
numberOfItemsVisibleOnScreen: 4,
349+
nbMaxOfItems: 11,
350+
scrollBehavior: scrollBehavior,
351+
data,
352+
listSizeInPx: 40,
353+
maxPossibleLeftAlignedIndex: 7,
354+
maxPossibleRightAlignedIndex: 3,
355+
});
356+
357+
expect(expectedResult).toEqual(-60);
358+
});
359+
360+
it('should end-align last element if focused', () => {
361+
const expectedResult = computeTranslation({
362+
itemSizeInPx,
363+
currentlyFocusedItemIndex: 10,
364+
numberOfItemsVisibleOnScreen: 4,
365+
nbMaxOfItems: 11,
366+
scrollBehavior: scrollBehavior,
367+
data,
368+
listSizeInPx: 40,
369+
maxPossibleLeftAlignedIndex: 7,
370+
maxPossibleRightAlignedIndex: 3,
371+
});
372+
373+
expect(expectedResult).toEqual(-70);
374+
});
375+
376+
it('should end-align last element if focused with dynamic sizes', () => {
377+
const expectedResult = computeTranslation({
378+
itemSizeInPx: itemSizeInPxFunction,
379+
currentlyFocusedItemIndex: 10,
380+
numberOfItemsVisibleOnScreen: 4,
381+
nbMaxOfItems: 11,
382+
scrollBehavior: scrollBehavior,
383+
data,
384+
listSizeInPx: 40,
385+
maxPossibleLeftAlignedIndex: 7,
386+
maxPossibleRightAlignedIndex: 3,
387+
});
388+
389+
expect(expectedResult).toEqual(-120);
390+
});
391+
392+
it('should start-align first element if numberOfItems <= numberOfVisibleItemsOnScreen', () => {
393+
const expectedResult = computeTranslation({
394+
itemSizeInPx,
395+
currentlyFocusedItemIndex: 1,
396+
numberOfItemsVisibleOnScreen: 4,
397+
nbMaxOfItems: 3,
398+
scrollBehavior: scrollBehavior,
399+
data,
400+
listSizeInPx: 40,
401+
maxPossibleLeftAlignedIndex: 7,
402+
maxPossibleRightAlignedIndex: 3,
403+
});
404+
405+
expect(expectedResult).toEqual(-0);
406+
});
407+
408+
it('should start-align first element if numberOfItems <= numberOfVisibleItemsOnScreen with dynamic sizes', () => {
409+
const expectedResult = computeTranslation({
410+
itemSizeInPx: itemSizeInPxFunction,
411+
currentlyFocusedItemIndex: 1,
412+
numberOfItemsVisibleOnScreen: 4,
413+
nbMaxOfItems: 3,
414+
scrollBehavior: scrollBehavior,
415+
data: data.slice(0, 2),
416+
listSizeInPx: 40,
417+
maxPossibleLeftAlignedIndex: 0,
418+
maxPossibleRightAlignedIndex: 2,
419+
});
420+
421+
expect(expectedResult).toEqual(-0);
422+
});
423+
});
424+
293425
describe('computeTranslation for virtualized list with jumping scroll behavior', () => {
294426
const scrollBehavior = 'jump-on-scroll';
295427

packages/lib/src/spatial-navigation/components/virtualizedList/helpers/computeTranslation.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ const computeStickToCenterTranslation = <T>({
2424
itemSizeInPx,
2525
data,
2626
listSizeInPx,
27+
maxPossibleRightAlignedIndex,
28+
maxPossibleLeftAlignedIndex,
2729
}: {
2830
currentlyFocusedItemIndex: number;
2931
itemSizeInPx: number | ((item: T) => number);
3032
data: T[];
3133
listSizeInPx: number;
34+
maxPossibleRightAlignedIndex: number;
35+
maxPossibleLeftAlignedIndex: number;
3236
}) => {
3337
const currentlyFocusedItemSize =
3438
typeof itemSizeInPx === 'function'
@@ -51,13 +55,35 @@ const computeStickToCenterTranslation = <T>({
5155
if (sizeOfListFromStartToCurrentlyFocusedItem < listSizeInPx / 2) {
5256
return 0;
5357
}
54-
55-
if (sizeOfListFromEndToCurrentlyFocusedItem < listSizeInPx / 2) {
56-
return -sizeOfListFromStartToCurrentlyFocusedItem + listSizeInPx - sizeOfListFromEndToCurrentlyFocusedItem - currentlyFocusedItemSize;
58+
59+
if (currentlyFocusedItemIndex > maxPossibleLeftAlignedIndex) {
60+
console.log('currentlyFocusedItemIndex', currentlyFocusedItemIndex);
5761
}
5862

63+
// if (sizeOfListFromEndToCurrentlyFocusedItem < listSizeInPx / 2) {
64+
// console.log('-----------');
65+
// console.log('currentlyFocusedItemIndex', currentlyFocusedItemIndex);
66+
// const result =
67+
// sizeOfListFromStartToCurrentlyFocusedItem -
68+
// listSizeInPx +
69+
// sizeOfListFromEndToCurrentlyFocusedItem +
70+
// currentlyFocusedItemSize;
71+
// console.log('list END inferior to half list size in px', result);
72+
// console.log(
73+
// 'sizeOfListFromStartToCurrentlyFocusedItem',
74+
// sizeOfListFromStartToCurrentlyFocusedItem,
75+
// );
76+
// console.log('listSizeInPx', listSizeInPx);
77+
// console.log('sizeOfListFromEndToCurrentlyFocusedItem', sizeOfListFromEndToCurrentlyFocusedItem);
78+
// console.log('currentlyFocusedItemSize', currentlyFocusedItemSize);
79+
// console.log('-----------');
80+
81+
// return -result;
82+
// }
83+
5984
const scrollOffset =
60-
sizeOfListFromStartToCurrentlyFocusedItem - (listSizeInPx / 2) + (currentlyFocusedItemSize / 2);
85+
sizeOfListFromStartToCurrentlyFocusedItem - listSizeInPx / 2 + currentlyFocusedItemSize / 2;
86+
6187
return -scrollOffset;
6288
};
6389

@@ -150,6 +176,8 @@ export const computeTranslation = <T>({
150176
itemSizeInPx,
151177
data,
152178
listSizeInPx,
179+
maxPossibleLeftAlignedIndex,
180+
maxPossibleRightAlignedIndex,
153181
});
154182
case 'stick-to-end':
155183
return computeStickToEndTranslation({
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Import the function to test
2+
3+
import { computeAllScrollOffsets } from './createScrollOffsetArray';
4+
5+
// Mock data and inputs
6+
const itemSize = (item) => item.size;
7+
// doesn't matter here
8+
const numberOfItemsVisibleOnScreen = 0;
9+
const data = [
10+
{ name: 'data 0', size: 1 },
11+
{ name: 'data 1', size: 1 },
12+
{ name: 'data 2', size: 2 },
13+
{ name: 'data 3', size: 1 },
14+
{ name: 'data 4', size: 1 },
15+
{ name: 'data 5', size: 2 },
16+
{ name: 'data 6', size: 1 },
17+
{ name: 'data 7', size: 1 },
18+
{ name: 'data 8', size: 2 },
19+
{ name: 'data 9', size: 1 },
20+
{ name: 'data 10', size: 1 },
21+
];
22+
const nbMaxOfItems = data.length;
23+
const scrollBehavior = 'stick-to-center';
24+
const listSizeInPx = 5;
25+
26+
// Expected output
27+
const expectedOutput = [0, 0, -0.5, -2, -3, -4.5, -6, -7, -8.5, -9, -9];
28+
29+
test('computeAllScrollOffsets returns an array of zeros with the same length as data', () => {
30+
const result = computeAllScrollOffsets({
31+
itemSize,
32+
nbMaxOfItems,
33+
numberOfItemsVisibleOnScreen,
34+
scrollBehavior,
35+
data,
36+
listSizeInPx,
37+
});
38+
39+
expect(result).toEqual(expectedOutput);
40+
});

0 commit comments

Comments
 (0)