Skip to content

Commit

Permalink
Add preview ref api for Node
Browse files Browse the repository at this point in the history
  • Loading branch information
spellforce committed Mar 21, 2023
1 parent 4c3368f commit a342e61
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useDragLayer } from "react-dnd";
import { useDndContext, useTreeApi } from "../context";
import { useTreeApi } from "../context";
import { DefaultDragPreview } from "./default-drag-preview";

export function DragPreviewContainer() {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-arborist/src/components/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function TreeProvider<T>({
useMemo(() => {
updateCount.current += 1;
api.update(treeProps);
}, [...Object.values(treeProps), state.nodes.open]);
}, [...Object.values(treeProps), state.nodes.open, state.dnd.parentId]);

/* Expose the tree api */
useImperativeHandle(imperativeHandle, () => api);
Expand Down
4 changes: 2 additions & 2 deletions packages/react-arborist/src/components/row-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const RowContainer = React.memo(function RowContainer<T>({
const node = useFreshNode<T>(index);

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

return (
<Row node={node} innerRef={innerRef} attrs={rowAttrs}>
<Node node={node} tree={tree} style={nodeStyle} dragHandle={dragRef} />
<Node node={node} tree={tree} style={nodeStyle} dragHandle={dragRef} previewHandle={previewRef} />
</Row>
);
});
2 changes: 1 addition & 1 deletion packages/react-arborist/src/components/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function TreeComponent<T>(
<OuterDrop>
<TreeContainer />
</OuterDrop>
<DragPreviewContainer />
{ props.renderDragPreview && <DragPreviewContainer /> }
</TreeProvider>
);
}
Expand Down
19 changes: 12 additions & 7 deletions packages/react-arborist/src/dnd/drag-hook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useMemo } from "react";
import { ConnectDragSource, useDrag } from "react-dnd";
import { getEmptyImage } from "react-dnd-html5-backend";
import { useTreeApi } from "../context";
Expand All @@ -9,7 +9,7 @@ import { actions as dnd } from "../state/dnd-slice";
import { safeRun } from "../utils";
import { ROOT_ID } from "../data/create-root";

export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
export function useDragHook<T>(node: NodeApi<T>) {
const tree = useTreeApi();
const ids = tree.selectedIds;
const [_, ref, preview] = useDrag<DragItem, DropResult, void>(
Expand Down Expand Up @@ -42,10 +42,15 @@ export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
}),
[ids, node]
);

useEffect(() => {
if (tree.renderDragPreview) {
preview(getEmptyImage());
}
}, []);

useEffect(() => {
preview(getEmptyImage());
}, [preview]);

return ref;
return {
dragRef: ref,
previewRef: preview,
};
}
3 changes: 1 addition & 2 deletions packages/react-arborist/src/interfaces/tree-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { createRoot, ROOT_ID } from "../data/create-root";
import { actions as visibility } from "../state/open-slice";
import { actions as selection } from "../state/selection-slice";
import { actions as dnd } from "../state/dnd-slice";
import { DefaultDragPreview } from "../components/default-drag-preview";
import { DefaultContainer } from "../components/default-container";
import { Cursor } from "../dnd/compute-drop";
import { Store } from "redux";
Expand Down Expand Up @@ -628,7 +627,7 @@ export class TreeApi<T> {
}

get renderDragPreview() {
return this.props.renderDragPreview || DefaultDragPreview;
return this.props.renderDragPreview;
}

get renderCursor() {
Expand Down
1 change: 1 addition & 0 deletions packages/react-arborist/src/types/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type NodeRendererProps<T> = {
node: NodeApi<T>;
tree: TreeApi<T>;
dragHandle?: (el: HTMLDivElement | null) => void;
previewHandle?: (el: HTMLDivElement | null) => void;
preview?: boolean;
};

Expand Down

0 comments on commit a342e61

Please sign in to comment.