@ornery/ui-grid-react 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +24 -2
- package/dist/index.d.ts +24 -2
- package/dist/index.js +1166 -607
- package/dist/index.mjs +1101 -549
- package/package.json +1 -1
- package/src/UiGrid.tsx +174 -47
- package/src/gridStateMath.test.ts +49 -0
- package/src/gridStateMath.ts +32 -0
- package/src/index.ts +2 -0
- package/src/rustWasmGridEngine.test.ts +56 -0
- package/src/rustWasmGridEngine.ts +21 -0
- package/src/useGridState.ts +637 -328
- package/src/useVirtualScroll.ts +11 -10
- package/src/virtualScrollMath.test.ts +44 -0
- package/src/virtualScrollMath.ts +36 -0
package/src/useVirtualScroll.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
import { calculateVirtualWindow } from './virtualScrollMath';
|
|
2
3
|
|
|
3
4
|
export interface UseVirtualScrollOptions {
|
|
4
5
|
itemCount: number;
|
|
@@ -21,22 +22,22 @@ export function useVirtualScroll(options: UseVirtualScrollOptions): UseVirtualSc
|
|
|
21
22
|
const [scrollTop, setScrollTop] = useState(0);
|
|
22
23
|
const viewportRef = useRef<HTMLDivElement | null>(null);
|
|
23
24
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
const virtualWindow = calculateVirtualWindow({
|
|
26
|
+
itemCount,
|
|
27
|
+
itemSize,
|
|
28
|
+
viewportHeight,
|
|
29
|
+
overscan,
|
|
30
|
+
scrollTop,
|
|
31
|
+
});
|
|
31
32
|
|
|
32
33
|
const onScroll = useCallback((event: React.UIEvent<HTMLDivElement>) => {
|
|
33
34
|
setScrollTop(event.currentTarget.scrollTop);
|
|
34
35
|
}, []);
|
|
35
36
|
|
|
36
37
|
return {
|
|
37
|
-
visibleRange:
|
|
38
|
-
totalHeight,
|
|
39
|
-
offsetY,
|
|
38
|
+
visibleRange: virtualWindow.visibleRange,
|
|
39
|
+
totalHeight: virtualWindow.totalHeight,
|
|
40
|
+
offsetY: virtualWindow.offsetY,
|
|
40
41
|
onScroll,
|
|
41
42
|
viewportRef,
|
|
42
43
|
scrollTop,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { calculateVirtualWindow } from './virtualScrollMath';
|
|
3
|
+
|
|
4
|
+
describe('virtualScrollMath', () => {
|
|
5
|
+
it('calculates the default overscanned window deterministically', () => {
|
|
6
|
+
expect(calculateVirtualWindow({
|
|
7
|
+
itemCount: 100,
|
|
8
|
+
itemSize: 44,
|
|
9
|
+
viewportHeight: 220,
|
|
10
|
+
scrollTop: 0,
|
|
11
|
+
})).toEqual({
|
|
12
|
+
visibleRange: { start: 0, end: 8 },
|
|
13
|
+
totalHeight: 4400,
|
|
14
|
+
offsetY: 0,
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('calculates a scrolled window deterministically', () => {
|
|
19
|
+
expect(calculateVirtualWindow({
|
|
20
|
+
itemCount: 100,
|
|
21
|
+
itemSize: 44,
|
|
22
|
+
viewportHeight: 220,
|
|
23
|
+
overscan: 3,
|
|
24
|
+
scrollTop: 440,
|
|
25
|
+
})).toEqual({
|
|
26
|
+
visibleRange: { start: 7, end: 18 },
|
|
27
|
+
totalHeight: 4400,
|
|
28
|
+
offsetY: 308,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('handles zero item size safely', () => {
|
|
33
|
+
expect(calculateVirtualWindow({
|
|
34
|
+
itemCount: 10,
|
|
35
|
+
itemSize: 0,
|
|
36
|
+
viewportHeight: 220,
|
|
37
|
+
scrollTop: 88,
|
|
38
|
+
})).toEqual({
|
|
39
|
+
visibleRange: { start: 0, end: 0 },
|
|
40
|
+
totalHeight: 0,
|
|
41
|
+
offsetY: 0,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface VirtualWindowRequest {
|
|
2
|
+
itemCount: number;
|
|
3
|
+
itemSize: number;
|
|
4
|
+
viewportHeight: number;
|
|
5
|
+
overscan?: number;
|
|
6
|
+
scrollTop: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface VirtualWindowResult {
|
|
10
|
+
visibleRange: { start: number; end: number };
|
|
11
|
+
totalHeight: number;
|
|
12
|
+
offsetY: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function calculateVirtualWindow(request: VirtualWindowRequest): VirtualWindowResult {
|
|
16
|
+
const overscan = request.overscan ?? 3;
|
|
17
|
+
|
|
18
|
+
if (request.itemCount <= 0 || request.itemSize <= 0) {
|
|
19
|
+
return {
|
|
20
|
+
visibleRange: { start: 0, end: 0 },
|
|
21
|
+
totalHeight: Math.max(0, request.itemCount) * Math.max(0, request.itemSize),
|
|
22
|
+
offsetY: 0,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const rawStart = Math.floor(request.scrollTop / request.itemSize) - overscan;
|
|
27
|
+
const start = Math.max(0, rawStart);
|
|
28
|
+
const rawEnd = rawStart + Math.ceil(request.viewportHeight / request.itemSize) + 2 * overscan;
|
|
29
|
+
const end = Math.min(request.itemCount, rawEnd);
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
visibleRange: { start, end },
|
|
33
|
+
totalHeight: request.itemCount * request.itemSize,
|
|
34
|
+
offsetY: start * request.itemSize,
|
|
35
|
+
};
|
|
36
|
+
}
|