Skip to content

Commit a342e61

Browse files
committed
Add preview ref api for Node
1 parent 4c3368f commit a342e61

File tree

7 files changed

+19
-14
lines changed

7 files changed

+19
-14
lines changed

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

+1-1
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/provider.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function TreeProvider<T>({
5757
useMemo(() => {
5858
updateCount.current += 1;
5959
api.update(treeProps);
60-
}, [...Object.values(treeProps), state.nodes.open]);
60+
}, [...Object.values(treeProps), state.nodes.open, state.dnd.parentId]);
6161

6262
/* Expose the tree api */
6363
useImperativeHandle(imperativeHandle, () => api);

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

+2-2
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

+1-1
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

+12-7
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

+1-2
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

+1
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)