-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathDropTarget.tsx
More file actions
51 lines (46 loc) · 1.48 KB
/
DropTarget.tsx
File metadata and controls
51 lines (46 loc) · 1.48 KB
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
"use client";
import React, {JSX, ReactNode} from 'react';
import type {TextDropItem} from '@react-aria/dnd';
import {useDrop} from '@react-aria/dnd';
interface DroppedItem {
message: string;
style?: 'bold' | 'italic';
}
export function DropTarget() {
let [dropped, setDropped] = React.useState<DroppedItem[] | null>(null);
let ref = React.useRef(null);
let {dropProps, isDropTarget} = useDrop({
ref,
async onDrop(e) {
let items = await Promise.all(
(e.items as TextDropItem[])
.filter(item => item.kind === 'text' && (item.types.has('text/plain') || item.types.has('my-app-custom-type')))
.map(async (item: TextDropItem) => {
if (item.types.has('my-app-custom-type')) {
return JSON.parse(await item.getText('my-app-custom-type'));
} else {
return {message: await item.getText('text/plain')};
}
})
);
setDropped(items);
}
});
let message: ReactNode = <div key="drop here">'Drop here'</div>;
if (dropped) {
message = dropped.map((d, index) => {
let m: ReactNode = d.message;
if (d.style === 'bold') {
m = <strong key={index}>{m}</strong>;
} else if (d.style === 'italic') {
m = <em key={index}>{m}</em>;
}
return <div key={index}>{m}</div>;
});
}
return (
<div {...dropProps} role="button" tabIndex={0} ref={ref} className={`droppable ${isDropTarget ? 'target' : ''}`}>
{message}
</div>
);
}