-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Upload): add drop zone (#1647)
* chore(Upload): add new translation string "errorUnsupportedFile" * chore(Upload): add drop zone
- Loading branch information
1 parent
32aa0eb
commit b74cc49
Showing
17 changed files
with
569 additions
and
189 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
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,4 @@ | ||
import { createContext } from 'react' | ||
import { UploadContextProps } from './types' | ||
|
||
export const UploadContext = createContext<UploadContextProps>(null) |
87 changes: 87 additions & 0 deletions
87
packages/dnb-eufemia/src/components/upload/UploadDropzone.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,87 @@ | ||
import React from 'react' | ||
import classnames from 'classnames' | ||
|
||
import HeightAnimation from '../height-animation/HeightAnimation' | ||
import { UploadContext } from './UploadContext' | ||
import type { UploadAllProps, UploadFile, UploadProps } from './types' | ||
|
||
export default function UploadDropzone({ | ||
children, | ||
className, | ||
...rest | ||
}: Partial<UploadAllProps>) { | ||
const props = rest as Omit<UploadProps, 'title'> | ||
const context = React.useContext(UploadContext) | ||
const [hover, setHover] = React.useState(false) | ||
const hoverTimeout = React.useRef<NodeJS.Timer>() | ||
|
||
const { onInputUpload, multipleFiles = false } = context | ||
|
||
const dropHandler = (event: React.DragEvent) => { | ||
event.preventDefault() | ||
|
||
const fileData = event.dataTransfer | ||
|
||
const files: UploadFile[] = [] | ||
|
||
Array.from(fileData.files).forEach((file, i) => { | ||
if (multipleFiles ? i >= 0 : i === 0) { | ||
files.push({ file }) | ||
} | ||
}) | ||
|
||
onInputUpload(files) | ||
|
||
setHover(false) | ||
} | ||
|
||
const dragOverHandler = (event: React.SyntheticEvent) => { | ||
event.preventDefault() | ||
clearTimers() | ||
setHover(true) | ||
} | ||
|
||
const dragLeaveHandler = (event: React.SyntheticEvent) => { | ||
event.preventDefault() | ||
clearTimers() | ||
hoverTimeout.current = setTimeout(() => { | ||
setHover(false) | ||
}, 300) // prevent flickering | ||
} | ||
|
||
const clearTimers = () => { | ||
clearTimeout(hoverTimeout.current) | ||
} | ||
|
||
React.useEffect(() => { | ||
return clearTimers | ||
}, []) | ||
|
||
return ( | ||
<HeightAnimation | ||
className={classnames(className, hover && 'dnb-upload--active')} | ||
onDrop={dropHandler} | ||
onDragOver={dragOverHandler} | ||
onDragLeave={dragLeaveHandler} | ||
{...props} | ||
> | ||
{children} | ||
|
||
<svg | ||
className="dnb-upload__outline" | ||
aria-hidden | ||
xmlns="http://www.w3.org/2000/svg" | ||
fill="none" | ||
> | ||
<rect | ||
width="100%" | ||
height="100%" | ||
rx="0.25rem" | ||
ry="0.25rem" | ||
strokeWidth="2.5" | ||
strokeDasharray="7 7" | ||
/> | ||
</svg> | ||
</HeightAnimation> | ||
) | ||
} |
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.