-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Document Upload and details (#3643)
* Added drag and drop component * Updated Documents UI * lint fixes * PR fixes * Data test id fixes * Updated ui
- Loading branch information
1 parent
b6f33c1
commit 750c95c
Showing
22 changed files
with
833 additions
and
765 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
source/frontend/src/components/common/form/FileDragAndDrop.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { ChangeEvent, FunctionComponent, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { SectionField } from '../Section/SectionField'; | ||
|
||
interface IFileDragAndDropProps { | ||
onSelectFile: (file: File | null) => void; | ||
selectedFile: File | null; | ||
validExtensions: string[]; | ||
} | ||
|
||
const FileDragAndDrop: FunctionComponent<React.PropsWithChildren<IFileDragAndDropProps>> = ({ | ||
onSelectFile, | ||
selectedFile, | ||
validExtensions, | ||
}) => { | ||
const validDocumentExtensions: string = validExtensions.map(x => `.${x}`).join(','); | ||
|
||
const [isDragging, setIsDragging] = useState(false); | ||
|
||
const handleFileInput = (changeEvent: ChangeEvent<HTMLInputElement>) => { | ||
// handle validations | ||
if (changeEvent.target !== null) { | ||
var target = changeEvent.target; | ||
if (target.files !== null && target.files.length > 0) { | ||
onSelectFile(target.files[0]); | ||
} | ||
} | ||
}; | ||
|
||
const handleDragEnter = (event: React.DragEvent) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
setIsDragging(true); | ||
}; | ||
|
||
const handleDragLeave = (event: React.DragEvent) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
setIsDragging(false); | ||
}; | ||
|
||
const handleDragOver = (event: React.DragEvent) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
setIsDragging(true); | ||
}; | ||
|
||
const handleDrop = (event: React.DragEvent) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
let files = [...event.dataTransfer.files]; | ||
|
||
if (files && files.length > 0) { | ||
onSelectFile(files[0]); | ||
event.dataTransfer.clearData(); | ||
} | ||
|
||
setIsDragging(false); | ||
}; | ||
|
||
const shortenString = (text: string, maxLength: number, terminator: string = '...'): string => { | ||
if (text.length > maxLength) { | ||
return text.substring(0, maxLength - terminator.length) + terminator; | ||
} | ||
|
||
return text; | ||
}; | ||
|
||
return ( | ||
<div> | ||
<DragDropZone | ||
onDrop={handleDrop} | ||
onDragOver={handleDragOver} | ||
onDragEnter={handleDragEnter} | ||
onDragLeave={handleDragLeave} | ||
isDragging={isDragging} | ||
> | ||
<StyledContent> | ||
Drag files here to attach or{' '} | ||
<StyledUploadLabel> | ||
Browse | ||
<StyledFileInput | ||
data-testid="upload-input" | ||
id="uploadInput" | ||
type="file" | ||
name="documentFile" | ||
accept={validDocumentExtensions} | ||
onChange={handleFileInput} | ||
className="" | ||
/> | ||
</StyledUploadLabel> | ||
</StyledContent> | ||
</DragDropZone> | ||
{selectedFile !== null && ( | ||
<StyledSelectedFile> | ||
<SectionField label="File to add" labelWidth="3"> | ||
{shortenString(selectedFile.name || '', 20)} | ||
</SectionField> | ||
</StyledSelectedFile> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default FileDragAndDrop; | ||
|
||
const DragDropZone = styled.div<{ isDragging: boolean }>` | ||
border: 1px solid ${({ theme }) => theme.css.lightVariantColor}; | ||
border-style: ${({ isDragging }) => (isDragging ? 'solid' : 'dashed')}; | ||
border: ${props => (props.isDragging ? `1px solid ${props.theme.css.draftColor}` : '')}; | ||
background-color: ${props => (props.isDragging ? props.theme.css.filterBoxColor : '')}; | ||
height: 10rem; | ||
line-height: 10rem; | ||
text-align: center; | ||
`; | ||
|
||
const StyledContent = styled.div` | ||
width: 100%; | ||
display: inline-block; | ||
vertical-align: middle; | ||
line-height: normal; | ||
`; | ||
|
||
const StyledUploadLabel = styled.label` | ||
display: inline-block; | ||
color: ${({ theme }) => theme.css.linkColor}; | ||
cursor: pointer; | ||
&:hover { | ||
color: ${({ theme }) => theme.css.linkHoverColor}; | ||
text-decoration: underline; | ||
} | ||
`; | ||
|
||
const StyledFileInput = styled.input` | ||
display: none; | ||
`; | ||
|
||
const StyledSelectedFile = styled.div` | ||
padding-top: 2rem; | ||
overflow: hide; | ||
word-wrap: break-word; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.