1
1
import { boolean , number } from '@storybook/addon-knobs' ;
2
- import React , { FC , useCallback , useEffect , useState } from 'react' ;
2
+ import React , { FC , useCallback , useEffect , useState , useRef } from 'react' ;
3
3
import { useManagedFiles } from '../../hooks' ;
4
4
import { action } from '../../storybook-helpers/action/action' ;
5
5
import { createFile , FakeFile } from '../../storybook-helpers/data/files' ;
@@ -10,6 +10,8 @@ import { Icon } from '../Icon';
10
10
import { Thumbnail } from '../Thumbnail' ;
11
11
import { ThumbnailDragLayer } from '../ThumbnailDragLayer' ;
12
12
import readme from './README.md' ;
13
+ import { FixedSizeGrid } from 'react-window' ;
14
+ import { Button } from '../Button' ;
13
15
14
16
export default {
15
17
title : 'Components/FileOrganizer' ,
@@ -21,9 +23,10 @@ interface TemplateProps {
21
23
onRenderDragLayer ?: boolean ;
22
24
numFiles ?: number ;
23
25
editable ?: boolean ;
26
+ scrollToTop ?: boolean ;
24
27
}
25
28
26
- const Template : FC < TemplateProps > = ( { onRenderDragLayer, numFiles = 2 , editable } ) => {
29
+ const Template : FC < TemplateProps > = ( { onRenderDragLayer, numFiles = 2 , editable, scrollToTop } ) => {
27
30
// This is the index organizing function.
28
31
const [ files , setFiles ] = useState < FakeFile [ ] > ( [ ] ) ;
29
32
const handleOnMove = useCallback < NonNullable < FileOrganizerProps < FakeFile > [ 'onMove' ] > > ( ( fromIndex , toIndex ) => {
@@ -37,6 +40,11 @@ const Template: FC<TemplateProps> = ({ onRenderDragLayer, numFiles = 2, editable
37
40
return true ;
38
41
} , [ ] ) ;
39
42
43
+ const gridRef = useRef < FixedSizeGrid > ( null ) ;
44
+ const onScrollToTop = ( ) => {
45
+ gridRef . current ?. scrollTo ( { scrollTop : 0 , scrollLeft : 0 } ) ;
46
+ } ;
47
+
40
48
// This is just a helper for adding or removing files.
41
49
useEffect ( ( ) => {
42
50
setFiles ( ( prev ) => {
@@ -55,35 +63,39 @@ const Template: FC<TemplateProps> = ({ onRenderDragLayer, numFiles = 2, editable
55
63
} , [ numFiles ] ) ;
56
64
57
65
return (
58
- < FileOrganizer
59
- files = { files }
60
- preventArrowsToMove = { boolean ( 'preventArrowsToMove' , false ) }
61
- preventClickAwayDeselect = { boolean ( 'preventClickAwayDeselect' , false ) }
62
- disableMove = { boolean ( 'disableMove' , false ) }
63
- padding = { integer ( 'padding' , 0 ) }
64
- onMove = { forwardAction ( 'onMove' , handleOnMove ) }
65
- onDragChange = { action ( 'onDragChange' ) }
66
- onDeselectAll = { action ( 'onDeselectAll' ) }
67
- onSelectAll = { action ( 'onSelectAll' ) }
68
- onRenderDragLayer = { onRenderDragLayer ? ( ) => < ThumbnailDragLayer /> : undefined }
69
- onRenderThumbnail = { ( { onRenderThumbnailProps } ) => (
70
- < Thumbnail
71
- { ...onRenderThumbnailProps }
72
- onRename = { editable ? ( ) => { } : undefined }
73
- buttonProps = {
74
- editable
75
- ? [
76
- {
77
- children : < Icon icon = "Close" /> ,
78
- onClick : ( ) => { } ,
79
- key : 0 ,
80
- } ,
81
- ]
82
- : undefined
83
- }
84
- />
85
- ) }
86
- />
66
+ < >
67
+ < FileOrganizer
68
+ files = { files }
69
+ gridRef = { gridRef }
70
+ preventArrowsToMove = { boolean ( 'preventArrowsToMove' , false ) }
71
+ preventClickAwayDeselect = { boolean ( 'preventClickAwayDeselect' , false ) }
72
+ disableMove = { boolean ( 'disableMove' , false ) }
73
+ padding = { integer ( 'padding' , 0 ) }
74
+ onMove = { forwardAction ( 'onMove' , handleOnMove ) }
75
+ onDragChange = { action ( 'onDragChange' ) }
76
+ onDeselectAll = { action ( 'onDeselectAll' ) }
77
+ onSelectAll = { action ( 'onSelectAll' ) }
78
+ onRenderDragLayer = { onRenderDragLayer ? ( ) => < ThumbnailDragLayer /> : undefined }
79
+ onRenderThumbnail = { ( { onRenderThumbnailProps } ) => (
80
+ < Thumbnail
81
+ { ...onRenderThumbnailProps }
82
+ onRename = { editable ? ( ) => { } : undefined }
83
+ buttonProps = {
84
+ editable
85
+ ? [
86
+ {
87
+ children : < Icon icon = "Close" /> ,
88
+ onClick : ( ) => { } ,
89
+ key : 0 ,
90
+ } ,
91
+ ]
92
+ : undefined
93
+ }
94
+ />
95
+ ) }
96
+ />
97
+ { scrollToTop && < Button onClick = { onScrollToTop } > Scroll to top</ Button > }
98
+ </ >
87
99
) ;
88
100
} ;
89
101
@@ -99,6 +111,9 @@ export const WithCustomDragLayer = () => {
99
111
const numFiles = number ( 'number of files' , 2 , { min : 0 , max : 16 , step : 1 , range : true } ) ;
100
112
return < Template numFiles = { numFiles } onRenderDragLayer /> ;
101
113
} ;
114
+ export const WithGridRef = ( ) => {
115
+ return < Template numFiles = { 100 } scrollToTop /> ;
116
+ } ;
102
117
103
118
export const UseManagedFilesHook = ( ) => {
104
119
const { fileOrganizerProps, getThumbnailSelectionProps, draggingIds } = useManagedFiles ( {
0 commit comments