forked from adazzle/react-data-grid
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDraggableRowRenderer.tsx
64 lines (56 loc) · 1.34 KB
/
DraggableRowRenderer.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
import { useDrag, useDrop } from 'react-dnd';
import { css } from '@linaria/core';
import clsx from 'clsx';
import { Row, type RenderRowProps } from '../../src';
const rowDraggingClassname = css`
opacity: 0.5;
`;
const rowOverClassname = css`
background-color: #ececec;
`;
interface DraggableRowRenderProps<R, SR> extends RenderRowProps<R, SR> {
onRowReorder: (sourceIndex: number, targetIndex: number) => void;
}
export function DraggableRowRenderer<R, SR>({
rowIdx,
isRowSelected,
className,
onRowReorder,
...props
}: DraggableRowRenderProps<R, SR>) {
const [{ isDragging }, drag] = useDrag({
type: 'ROW_DRAG',
item: { index: rowIdx },
collect: (monitor) => ({
isDragging: monitor.isDragging()
})
});
const [{ isOver }, drop] = useDrop({
accept: 'ROW_DRAG',
drop({ index }: { index: number }) {
onRowReorder(index, rowIdx);
},
collect: (monitor) => ({
isOver: monitor.isOver(),
canDrop: monitor.canDrop()
})
});
className = clsx(className, {
[rowDraggingClassname]: isDragging,
[rowOverClassname]: isOver
});
return (
<Row
ref={(ref) => {
if (ref) {
drag(ref.firstElementChild);
}
drop(ref);
}}
rowIdx={rowIdx}
isRowSelected={isRowSelected}
className={className}
{...props}
/>
);
}