Skip to content

Commit cc2da18

Browse files
committed
Add preview ref api for Node
Add preview ref api for Node Revert "Add preview ref api for Node" This reverts commit ca8ccfc6da2292d46ab3134d0f497a6ea7aed97e. Add preview ref api for Node
1 parent 4c3368f commit cc2da18

File tree

6 files changed

+18
-13
lines changed

6 files changed

+18
-13
lines changed

packages/react-arborist/src/components/drag-preview-container.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useDragLayer } from "react-dnd";
2-
import { useDndContext, useTreeApi } from "../context";
2+
import { useTreeApi } from "../context";
33
import { DefaultDragPreview } from "./default-drag-preview";
44

55
export function DragPreviewContainer() {

packages/react-arborist/src/components/row-container.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const RowContainer = React.memo(function RowContainer<T>({
3535
const node = useFreshNode<T>(index);
3636

3737
const el = useRef<HTMLDivElement | null>(null);
38-
const dragRef = useDragHook<T>(node);
38+
const { dragRef, previewRef } = useDragHook<T>(node);
3939
const dropRef = useDropHook(el, node);
4040
const innerRef = useCallback(
4141
(n: any) => {
@@ -76,7 +76,7 @@ export const RowContainer = React.memo(function RowContainer<T>({
7676

7777
return (
7878
<Row node={node} innerRef={innerRef} attrs={rowAttrs}>
79-
<Node node={node} tree={tree} style={nodeStyle} dragHandle={dragRef} />
79+
<Node node={node} tree={tree} style={nodeStyle} dragHandle={dragRef} previewHandle={previewRef} />
8080
</Row>
8181
);
8282
});

packages/react-arborist/src/components/tree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function TreeComponent<T>(
1818
<OuterDrop>
1919
<TreeContainer />
2020
</OuterDrop>
21-
<DragPreviewContainer />
21+
{ props.renderDragPreview && <DragPreviewContainer /> }
2222
</TreeProvider>
2323
);
2424
}

packages/react-arborist/src/dnd/drag-hook.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from "react";
1+
import { useEffect, useMemo } from "react";
22
import { ConnectDragSource, useDrag } from "react-dnd";
33
import { getEmptyImage } from "react-dnd-html5-backend";
44
import { useTreeApi } from "../context";
@@ -9,7 +9,7 @@ import { actions as dnd } from "../state/dnd-slice";
99
import { safeRun } from "../utils";
1010
import { ROOT_ID } from "../data/create-root";
1111

12-
export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
12+
export function useDragHook<T>(node: NodeApi<T>) {
1313
const tree = useTreeApi();
1414
const ids = tree.selectedIds;
1515
const [_, ref, preview] = useDrag<DragItem, DropResult, void>(
@@ -42,10 +42,15 @@ export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
4242
}),
4343
[ids, node]
4444
);
45+
46+
useEffect(() => {
47+
if (tree.renderDragPreview) {
48+
preview(getEmptyImage());
49+
}
50+
}, []);
4551

46-
useEffect(() => {
47-
preview(getEmptyImage());
48-
}, [preview]);
49-
50-
return ref;
52+
return {
53+
dragRef: ref,
54+
previewRef: preview,
55+
};
5156
}

packages/react-arborist/src/interfaces/tree-api.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { createRoot, ROOT_ID } from "../data/create-root";
1515
import { actions as visibility } from "../state/open-slice";
1616
import { actions as selection } from "../state/selection-slice";
1717
import { actions as dnd } from "../state/dnd-slice";
18-
import { DefaultDragPreview } from "../components/default-drag-preview";
1918
import { DefaultContainer } from "../components/default-container";
2019
import { Cursor } from "../dnd/compute-drop";
2120
import { Store } from "redux";
@@ -628,7 +627,7 @@ export class TreeApi<T> {
628627
}
629628

630629
get renderDragPreview() {
631-
return this.props.renderDragPreview || DefaultDragPreview;
630+
return this.props.renderDragPreview;
632631
}
633632

634633
get renderCursor() {

packages/react-arborist/src/types/renderers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type NodeRendererProps<T> = {
99
node: NodeApi<T>;
1010
tree: TreeApi<T>;
1111
dragHandle?: (el: HTMLDivElement | null) => void;
12+
previewHandle?: (el: HTMLDivElement | null) => void;
1213
preview?: boolean;
1314
};
1415

0 commit comments

Comments
 (0)