-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathDroppableContent.tsx
82 lines (72 loc) · 2.4 KB
/
DroppableContent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as React from 'react';
import ItemList from './ItemList';
import UploadState from './UploadState';
import makeDroppable from '../common/droppable';
import type { UploadFile, UploadFileWithAPIOptions, UploadItem } from '../../common/types/upload';
import type { DOMStringList, View } from '../../common/types/core';
import './DroppableContent.scss';
export interface DroppableContentProps {
addDataTransferItemsToUploadQueue: (droppedItems: DataTransfer) => void;
addFiles: (files?: Array<UploadFileWithAPIOptions | UploadFile>) => void;
allowedTypes: Array<string>;
canDrop: boolean;
isFolderUploadEnabled: boolean;
isOver: boolean;
isTouch: boolean;
items: UploadItem[];
onClick: (item: UploadItem) => void;
view: View;
}
/**
* Definition for drag and drop behavior.
*/
const dropDefinition = {
/**
* Validates whether a file can be dropped or not.
*/
dropValidator: (
{ allowedTypes }: { allowedTypes: Array<string> },
{ types }: { types: Array<string> | DOMStringList },
) => {
if (types instanceof Array) {
return Array.from(types).some(type => allowedTypes.indexOf(type) > -1);
}
const allowedList = allowedTypes.filter(allowed => types.contains(allowed));
return allowedList.length > 0;
},
/**
* Determines what happens after a file is dropped
*/
onDrop: (event, { addDataTransferItemsToUploadQueue }: DroppableContentProps) => {
const { dataTransfer } = event;
addDataTransferItemsToUploadQueue(dataTransfer);
},
} as const;
const DroppableContent = makeDroppable(dropDefinition)(({
addFiles,
canDrop,
isFolderUploadEnabled,
isOver,
isTouch,
items,
onClick,
view,
}: DroppableContentProps) => {
const handleSelectFiles = ({ target: { files } }) => addFiles(files);
const hasItems = items.length > 0;
return (
<div className="bcu-droppable-content" data-testid="bcu-droppable-content">
<ItemList items={items} onClick={onClick} />
<UploadState
canDrop={canDrop}
hasItems={hasItems}
isFolderUploadEnabled={isFolderUploadEnabled}
isOver={isOver}
isTouch={isTouch}
onSelect={handleSelectFiles}
view={view}
/>
</div>
);
});
export default DroppableContent;