-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
36 lines (32 loc) · 1.12 KB
/
index.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
import React from 'react';
import { DragDropContext, DragDropContextConsumer, ContextComponent } from 'react-dnd';
import { BackendFactory } from 'dnd-core';
import HTML5Backend from 'react-dnd-html5-backend';
let defaultDragDropContext: <TargetClass extends React.ComponentType<any>>(
DecoratedComponent: TargetClass
) => TargetClass & ContextComponent<any>;
function getDefaultDragDropContext(backendFactory: BackendFactory) {
if (!defaultDragDropContext) {
defaultDragDropContext = DragDropContext(backendFactory);
}
return defaultDragDropContext;
}
const withDndContext = (
WrappedComponent: React.ComponentType<any>,
backendFactory: BackendFactory = HTML5Backend
): React.ComponentType<any> => {
return (props: any) => {
return (
<DragDropContextConsumer>
{value => {
if (value && value.dragDropManager != null) {
return <WrappedComponent {...props} />;
}
const Comp = getDefaultDragDropContext(backendFactory)(WrappedComponent);
return <Comp {...props} />;
}}
</DragDropContextConsumer>
);
};
};
export default withDndContext;