Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preview ref api for Node #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this previewHandle get used?

Copy link
Author

@spellforce spellforce May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be used in nodeRender, if user need. They can custom preview div ref.
such as :

<div ref={previewHandle}><dragImg ref={dragHandle} />im a test</div>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, I see.

preview?: boolean;
};

Expand Down