-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated Document Upload and details #3643
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a350e46
Added drag and drop component
FuriousLlama 3025986
Merge branch 'dev' into features/psp-7321
FuriousLlama 51a9341
Updated Documents UI
FuriousLlama b78bf10
lint fixes
FuriousLlama 15f7fb8
Merge branch 'dev' into features/psp-7321
FuriousLlama f75888d
Merge branch 'dev' into features/psp-7321
FuriousLlama 23ccd22
PR fixes
FuriousLlama 0abe174
Merge branch 'features/psp-7321' of https://github.com/FuriousLlama/P…
FuriousLlama 01553ed
Data test id fixes
FuriousLlama 5c4ee8c
Merge branch 'dev' into features/psp-7321
FuriousLlama 0fb7ad3
Updated ui
FuriousLlama 2e215f3
Merge branch 'dev' into features/psp-7321
FuriousLlama dabbb99
Merge branch 'dev' into features/psp-7321
FuriousLlama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe could use the OverflowTip component instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked with ana and this way worked better |
||
</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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test coverage on this isn't great. Unit testing this completely may not be realistic but is there anything we can do here?