Commit 23787bf1 authored by tom's avatar tom

disabled state for drag-and-drop area

parent 39b7f189
...@@ -91,7 +91,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, title, hint }: ...@@ -91,7 +91,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, title, hint }:
rowGap={ 2 } rowGap={ 2 }
w="100%" w="100%"
> >
<DragAndDropArea onDrop={ onChange } p={{ base: 3, lg: 6 }}> <DragAndDropArea onDrop={ onChange } p={{ base: 3, lg: 6 }} isDisabled={ formState.isSubmitting }>
{ hasValue ? renderFiles(field.value) : renderUploadButton() } { hasValue ? renderFiles(field.value) : renderUploadButton() }
</DragAndDropArea> </DragAndDropArea>
</Flex> </Flex>
...@@ -100,7 +100,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, title, hint }: ...@@ -100,7 +100,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, title, hint }:
{ commonError?.message && <FieldError message={ commonError.type === 'required' ? 'Field is required' : commonError.message }/> } { commonError?.message && <FieldError message={ commonError.type === 'required' ? 'Field is required' : commonError.message }/> }
</> </>
); );
}, [ fileTypes, multiple, renderFiles, commonError, renderUploadButton ]); }, [ fileTypes, multiple, commonError, formState.isSubmitting, renderFiles, renderUploadButton ]);
const validateFileType = React.useCallback(async(value: FieldPathValue<FormFields, 'sources'>): Promise<ValidateResult> => { const validateFileType = React.useCallback(async(value: FieldPathValue<FormFields, 'sources'>): Promise<ValidateResult> => {
if (Array.isArray(value)) { if (Array.isArray(value)) {
......
import { chakra, Center } from '@chakra-ui/react'; import { chakra, Center, useColorModeValue } from '@chakra-ui/react';
import type { DragEvent } from 'react'; import type { DragEvent } from 'react';
import React from 'react'; import React from 'react';
...@@ -8,20 +8,25 @@ interface Props { ...@@ -8,20 +8,25 @@ interface Props {
children: React.ReactNode; children: React.ReactNode;
onDrop: (files: Array<File>) => void; onDrop: (files: Array<File>) => void;
className?: string; className?: string;
isDisabled?: boolean;
} }
const DragAndDropArea = ({ onDrop, children, className }: Props) => { const DragAndDropArea = ({ onDrop, children, className, isDisabled }: Props) => {
const [ isDragOver, setIsDragOver ] = React.useState(false); const [ isDragOver, setIsDragOver ] = React.useState(false);
const handleDrop = React.useCallback(async(event: DragEvent<HTMLDivElement>) => { const handleDrop = React.useCallback(async(event: DragEvent<HTMLDivElement>) => {
event.preventDefault(); event.preventDefault();
if (isDisabled) {
return;
}
const fileEntries = await getAllFileEntries(event.dataTransfer.items); const fileEntries = await getAllFileEntries(event.dataTransfer.items);
const files = await Promise.all(fileEntries.map(convertFileEntryToFile)); const files = await Promise.all(fileEntries.map(convertFileEntryToFile));
onDrop(files); onDrop(files);
setIsDragOver(false); setIsDragOver(false);
}, [ onDrop ]); }, [ isDisabled, onDrop ]);
const handleDragOver = React.useCallback((event: DragEvent<HTMLDivElement>) => { const handleDragOver = React.useCallback((event: DragEvent<HTMLDivElement>) => {
event.preventDefault(); event.preventDefault();
...@@ -37,20 +42,31 @@ const DragAndDropArea = ({ onDrop, children, className }: Props) => { ...@@ -37,20 +42,31 @@ const DragAndDropArea = ({ onDrop, children, className }: Props) => {
setIsDragOver(false); setIsDragOver(false);
}, []); }, []);
const handleClick = React.useCallback((event: React.MouseEvent) => {
if (isDisabled) {
event.preventDefault();
event.stopPropagation();
}
}, [ isDisabled ]);
const disabledBorderColor = useColorModeValue('blackAlpha.200', 'whiteAlpha.200');
const borderColor = isDragOver ? 'link_hovered' : 'link';
return ( return (
<Center <Center
className={ className } className={ className }
w="100%" w="100%"
minH="120px" minH="120px"
borderWidth="2px" borderWidth="2px"
borderColor={ isDragOver ? 'link_hovered' : 'link' } borderColor={ isDisabled ? disabledBorderColor : borderColor }
_hover={{ _hover={{
borderColor: 'link_hovered', borderColor: isDisabled ? disabledBorderColor : 'link_hovered',
}} }}
borderRadius="base" borderRadius="base"
borderStyle="dashed" borderStyle="dashed"
cursor="pointer" cursor="pointer"
textAlign="center" textAlign="center"
onClick={ handleClick }
onDrop={ handleDrop } onDrop={ handleDrop }
onDragOver={ handleDragOver } onDragOver={ handleDragOver }
onDragEnter={ handleDragEnter } onDragEnter={ handleDragEnter }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment