This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
Draggable.tsx
88 lines (74 loc) · 2.24 KB
/
Draggable.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
83
84
85
86
87
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
// Copyright (c) 2021. Sendanor <info@sendanor.fi>. All rights reserved.
import {
ReactNode,
RefObject,
useEffect,
useRef
} from 'react';
import { DraggableElementManager } from "./DragManager";
import { LogService } from "../../../core/LogService";
import { DRAGGABLE_CLASS_NAME } from "../../constants/hgClassName";
import './Draggable.scss';
const LOG = LogService.createLogger('Draggable');
export interface DraggableClickCallback {
(): void;
}
export interface DraggableProps {
readonly id: string;
readonly data?: any[];
}
export interface DraggableProps {
readonly className?: string;
readonly id: string;
readonly data?: any[];
readonly children?: ReactNode;
}
export function useDraggableElementManager (
ref : RefObject<HTMLDivElement>,
id : string,
data ?: any[]
) : void {
let manager : DraggableElementManager<HTMLDivElement> | undefined = undefined;
useEffect(
() => {
if (!manager) {
if ( ref.current && id ) {
manager = new DraggableElementManager<HTMLDivElement>(id, ref.current);
if ( data ) {
manager.setDropData(data);
}
} else {
LOG.warn('Warning! No reference to the DOM element or id: ', ref.current, id);
}
} else if ( data ) {
manager.setDropData(data);
} else {
manager.setDropData([]);
}
return () => {
if ( manager ) {
manager.destroy();
manager = undefined;
}
};
},
[
data
]
);
}
export function Draggable (props: DraggableProps) {
const className = props?.className;
const id = props?.id;
const data = props?.data;
const ref = useRef<HTMLDivElement>(null);
useDraggableElementManager(ref, id, data);
return (
<div
ref={ref}
className={DRAGGABLE_CLASS_NAME + (className ? ` ${className}` : '')}
draggable="true"
>{props?.children}</div>
);
}