1
1
import React , { forwardRef , memo , useCallback , useEffect , useMemo } from 'react' ;
2
2
import AutoSizer , { Size } from 'react-virtualized-auto-sizer' ;
3
3
import { FixedSizeGrid as Grid , GridChildComponentProps } from 'react-window' ;
4
- import { getItemIndex , ObjectWithId , THUMBNAIL_WIDTH } from '../../utils' ;
4
+ import { getItemIndex , ObjectWithId } from '../../utils' ;
5
5
6
6
interface VirtualizedProps {
7
7
files : ObjectWithId [ ] ;
8
8
padding : number ;
9
+ size : { width : number ; height : number } ;
9
10
onColumnCountChange : ( newColumnCount : number ) => void ;
10
11
renderItem : ( file : any , index : number , style : React . CSSProperties | undefined ) => JSX . Element ; // eslint-disable-line @typescript-eslint/no-explicit-any
11
12
}
@@ -19,15 +20,15 @@ const MemoGridItem = memo<GridChildComponentProps>(({ columnIndex, rowIndex, sty
19
20
20
21
const MemoGrid = memo (
21
22
forwardRef < Grid , Size & VirtualizedProps > (
22
- ( { width, height, files, padding, renderItem, onColumnCountChange } , ref ) => {
23
+ ( { width, height, files, padding, renderItem, onColumnCountChange, size } , ref ) => {
23
24
const modifiedHeight = height - 2 * padding ;
24
25
const modifiedWidth = width - 2 * padding ;
25
26
26
27
const data = useMemo ( ( ) => {
27
- let columnCount = Math . floor ( modifiedWidth / THUMBNAIL_WIDTH ) ;
28
+ let columnCount = Math . floor ( modifiedWidth / size . width ) ;
28
29
if ( columnCount > files . length ) columnCount = files . length ;
29
30
return { files, renderItem, columnCount } ;
30
- } , [ files , renderItem , modifiedWidth ] ) ;
31
+ } , [ modifiedWidth , size . width , files , renderItem ] ) ;
31
32
32
33
useEffect ( ( ) => {
33
34
onColumnCountChange ( data . columnCount ) ;
@@ -36,8 +37,8 @@ const MemoGrid = memo(
36
37
return (
37
38
< Grid
38
39
ref = { ref }
39
- columnWidth = { THUMBNAIL_WIDTH }
40
- rowHeight = { THUMBNAIL_WIDTH }
40
+ columnWidth = { size . width }
41
+ rowHeight = { size . height }
41
42
height = { modifiedHeight }
42
43
width = { modifiedWidth }
43
44
style = { { padding, height, width } }
@@ -57,7 +58,7 @@ const MemoGrid = memo(
57
58
) ;
58
59
59
60
export const MemoAutoSizer = memo (
60
- forwardRef < Grid , VirtualizedProps > ( ( { files, padding, renderItem, onColumnCountChange } , ref ) => {
61
+ forwardRef < Grid , VirtualizedProps > ( ( { files, padding, size , renderItem, onColumnCountChange } , ref ) => {
61
62
const onRenderGrid = useCallback (
62
63
( { width, height } : Size ) => (
63
64
< MemoGrid
@@ -66,11 +67,12 @@ export const MemoAutoSizer = memo(
66
67
width = { width }
67
68
height = { height }
68
69
files = { files }
70
+ size = { size }
69
71
renderItem = { renderItem }
70
72
onColumnCountChange = { onColumnCountChange }
71
73
/>
72
74
) ,
73
- [ files , padding , renderItem , ref , onColumnCountChange ] ,
75
+ [ padding , ref , files , size , renderItem , onColumnCountChange ] ,
74
76
) ;
75
77
76
78
return < AutoSizer > { onRenderGrid } </ AutoSizer > ;
0 commit comments