From 77c0c0efcf9eae723547a5995b5c998c37e21011 Mon Sep 17 00:00:00 2001 From: Liam Ross Date: Wed, 27 May 2020 12:16:25 -0700 Subject: [PATCH] feat(FileOrganizer): added gridRef prop to allow access to react-window grid --- src/components/ClickableDiv/ClickableDiv.tsx | 2 +- src/components/Draggable/Draggable.tsx | 2 +- src/components/EditableText/EditableText.tsx | 2 +- .../FileOrganizer/FileOrganizer.stories.tsx | 77 +++++++++++-------- .../FileOrganizer/FileOrganizer.tsx | 12 ++- .../FileOrganizer/MemoAutoSizer.tsx | 10 +-- 6 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/components/ClickableDiv/ClickableDiv.tsx b/src/components/ClickableDiv/ClickableDiv.tsx index 9e74f49..6107de1 100644 --- a/src/components/ClickableDiv/ClickableDiv.tsx +++ b/src/components/ClickableDiv/ClickableDiv.tsx @@ -22,7 +22,7 @@ export interface ClickableDivProps extends Remove export const ClickableDiv = forwardRef( ({ onClick, onKeyPress, disabled, noFocusStyle, usePointer, className, children, tabIndex, ...divProps }, ref) => { const clickableDivRef = useRef(null); - useImperativeHandle(ref, () => clickableDivRef.current!); + useImperativeHandle(ref, () => clickableDivRef.current as HTMLDivElement); const handleOnClick = useOnClick(onClick, { disabled, stopPropagation: true }); diff --git a/src/components/Draggable/Draggable.tsx b/src/components/Draggable/Draggable.tsx index 15e5fe8..9d0e9e9 100644 --- a/src/components/Draggable/Draggable.tsx +++ b/src/components/Draggable/Draggable.tsx @@ -81,7 +81,7 @@ export const Draggable = forwardRef( ref, ) => { const draggableRef = useRef(null); - useImperativeHandle(ref, () => draggableRef.current!); + useImperativeHandle(ref, () => draggableRef.current as HTMLDivElement); /* --- Drag and drop settings. --- */ diff --git a/src/components/EditableText/EditableText.tsx b/src/components/EditableText/EditableText.tsx index 402d80e..508ae65 100644 --- a/src/components/EditableText/EditableText.tsx +++ b/src/components/EditableText/EditableText.tsx @@ -81,7 +81,7 @@ export const EditableText = forwardRef( const inputRef = useRef(null); const buttonRef = useRef(null); - useImperativeHandle(ref, () => buttonRef.current!); + useImperativeHandle(ref, () => buttonRef.current as HTMLDivElement); // Use state if controlled edit mode not provided. const [stateEditMode, setStateEditMode] = useState(false); diff --git a/src/components/FileOrganizer/FileOrganizer.stories.tsx b/src/components/FileOrganizer/FileOrganizer.stories.tsx index 149a011..1c7e1e7 100644 --- a/src/components/FileOrganizer/FileOrganizer.stories.tsx +++ b/src/components/FileOrganizer/FileOrganizer.stories.tsx @@ -1,5 +1,5 @@ import { boolean, number } from '@storybook/addon-knobs'; -import React, { FC, useCallback, useEffect, useState } from 'react'; +import React, { FC, useCallback, useEffect, useState, useRef } from 'react'; import { useManagedFiles } from '../../hooks'; import { action } from '../../storybook-helpers/action/action'; import { createFile, FakeFile } from '../../storybook-helpers/data/files'; @@ -10,6 +10,8 @@ import { Icon } from '../Icon'; import { Thumbnail } from '../Thumbnail'; import { ThumbnailDragLayer } from '../ThumbnailDragLayer'; import readme from './README.md'; +import { FixedSizeGrid } from 'react-window'; +import { Button } from '../Button'; export default { title: 'Components/FileOrganizer', @@ -21,9 +23,10 @@ interface TemplateProps { onRenderDragLayer?: boolean; numFiles?: number; editable?: boolean; + scrollToTop?: boolean; } -const Template: FC = ({ onRenderDragLayer, numFiles = 2, editable }) => { +const Template: FC = ({ onRenderDragLayer, numFiles = 2, editable, scrollToTop }) => { // This is the index organizing function. const [files, setFiles] = useState([]); const handleOnMove = useCallback['onMove']>>((fromIndex, toIndex) => { @@ -37,6 +40,11 @@ const Template: FC = ({ onRenderDragLayer, numFiles = 2, editable return true; }, []); + const gridRef = useRef(null); + const onScrollToTop = () => { + gridRef.current?.scrollTo({ scrollTop: 0, scrollLeft: 0 }); + }; + // This is just a helper for adding or removing files. useEffect(() => { setFiles((prev) => { @@ -55,35 +63,39 @@ const Template: FC = ({ onRenderDragLayer, numFiles = 2, editable }, [numFiles]); return ( - : undefined} - onRenderThumbnail={({ onRenderThumbnailProps }) => ( - {} : undefined} - buttonProps={ - editable - ? [ - { - children: , - onClick: () => {}, - key: 0, - }, - ] - : undefined - } - /> - )} - /> + <> + : undefined} + onRenderThumbnail={({ onRenderThumbnailProps }) => ( + {} : undefined} + buttonProps={ + editable + ? [ + { + children: , + onClick: () => {}, + key: 0, + }, + ] + : undefined + } + /> + )} + /> + {scrollToTop && } + ); }; @@ -99,6 +111,9 @@ export const WithCustomDragLayer = () => { const numFiles = number('number of files', 2, { min: 0, max: 16, step: 1, range: true }); return