Skip to content

Commit 9048978

Browse files
committed
update scrolling behavior when press arrow keys
1 parent 842b4a4 commit 9048978

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/components/app/data-table.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ export const DataTable: FC<DataTableProps> = ({ data }) => {
339339
const previousTicker = table.getRowModel().rows[activeRowIndex - 1].original.ticker;
340340
setTicker(previousTicker);
341341
}
342+
event.preventDefault();
343+
const { y } = document.querySelector('.grid-row.active')?.getBoundingClientRect() ?? { y: 0 };
344+
if (Math.round(y) <= 0) {
345+
listRef.current?.scrollToIndex({
346+
index: activeRowIndex - 1,
347+
offset: -40
348+
});
349+
} else if (Math.round(y) < 120) {
350+
parentRef.current?.scrollBy(0, -30);
351+
}
342352
break;
343353
}
344354
case 'ArrowDown': {
@@ -347,6 +357,18 @@ export const DataTable: FC<DataTableProps> = ({ data }) => {
347357
const nextTicker = table.getRowModel().rows[activeRowIndex + 1].original.ticker;
348358
setTicker(nextTicker);
349359
}
360+
event.preventDefault();
361+
const { y } = document.querySelector('.grid-row.active')?.getBoundingClientRect() ?? { y: 0 };
362+
const { height } = parentRef.current?.getBoundingClientRect() ?? { height: 0 };
363+
if (Math.round(y) - Math.round(height) > -10) {
364+
parentRef.current?.scrollBy(0, 30 + (y - height));
365+
}
366+
if (Math.round(y) <= 0) {
367+
listRef.current?.scrollToIndex({
368+
index: activeRowIndex + 1,
369+
offset: -height + 50
370+
});
371+
}
350372
break;
351373
}
352374
}

0 commit comments

Comments
 (0)