mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-11 23:04:40 +02:00
split out item list table functionality
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
interface UseTableInitialScrollProps {
|
||||
initialTop?: {
|
||||
behavior?: 'auto' | 'smooth';
|
||||
to: number;
|
||||
type: 'index' | 'offset';
|
||||
};
|
||||
scrollToTableIndex: (index: number, options?: { align?: 'bottom' | 'center' | 'top' }) => void;
|
||||
scrollToTableOffset: (offset: number) => void;
|
||||
startRowIndex?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to handle initial scroll position and scrolling to top when startRowIndex changes.
|
||||
*/
|
||||
export const useTableInitialScroll = ({
|
||||
initialTop,
|
||||
scrollToTableIndex,
|
||||
scrollToTableOffset,
|
||||
startRowIndex,
|
||||
}: UseTableInitialScrollProps) => {
|
||||
const isInitialScrollPositionSet = useRef<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!initialTop || isInitialScrollPositionSet.current) return;
|
||||
isInitialScrollPositionSet.current = true;
|
||||
|
||||
if (initialTop.type === 'offset') {
|
||||
scrollToTableOffset(initialTop.to);
|
||||
} else {
|
||||
scrollToTableIndex(initialTop.to);
|
||||
}
|
||||
}, [initialTop, scrollToTableIndex, scrollToTableOffset]);
|
||||
|
||||
// Scroll to top when startRowIndex changes
|
||||
useEffect(() => {
|
||||
if (startRowIndex !== undefined) {
|
||||
scrollToTableOffset(0);
|
||||
}
|
||||
}, [startRowIndex, scrollToTableOffset]);
|
||||
};
|
||||
Reference in New Issue
Block a user