feat(node-toolbar): make nodeId optional and get id from context
This commit is contained in:
@@ -4,7 +4,7 @@ import { Handle, Position, NodeProps, NodeToolbar } from 'reactflow';
|
|||||||
const CustomNode: FC<NodeProps> = ({ id, data }) => {
|
const CustomNode: FC<NodeProps> = ({ id, data }) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<NodeToolbar nodeId={id} isVisible={data.toolbarVisible} position={data.toolbarPosition}>
|
<NodeToolbar isVisible={data.toolbarVisible} position={data.toolbarPosition}>
|
||||||
<button>delete</button>
|
<button>delete</button>
|
||||||
<button>copy</button>
|
<button>copy</button>
|
||||||
<button>expand</button>
|
<button>expand</button>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
Rect,
|
Rect,
|
||||||
Position,
|
Position,
|
||||||
internalsSymbol,
|
internalsSymbol,
|
||||||
|
useNodeId,
|
||||||
} from '@reactflow/core';
|
} from '@reactflow/core';
|
||||||
import cc from 'classcat';
|
import cc from 'classcat';
|
||||||
import shallow from 'zustand/shallow';
|
import shallow from 'zustand/shallow';
|
||||||
@@ -72,9 +73,11 @@ function NodeToolbar({
|
|||||||
offset = 10,
|
offset = 10,
|
||||||
...rest
|
...rest
|
||||||
}: NodeToolbarProps) {
|
}: NodeToolbarProps) {
|
||||||
|
const contextNodeId = useNodeId();
|
||||||
|
|
||||||
const nodesSelector = useCallback(
|
const nodesSelector = useCallback(
|
||||||
(state: ReactFlowState): Node[] => {
|
(state: ReactFlowState): Node[] => {
|
||||||
const nodeIds: string[] = typeof nodeId === 'string' ? [nodeId] : nodeId;
|
const nodeIds = Array.isArray(nodeId) ? nodeId : [nodeId || contextNodeId || ''];
|
||||||
|
|
||||||
return nodeIds.reduce<Node[]>((acc, id) => {
|
return nodeIds.reduce<Node[]>((acc, id) => {
|
||||||
const node = state.nodeInternals.get(id);
|
const node = state.nodeInternals.get(id);
|
||||||
@@ -84,7 +87,7 @@ function NodeToolbar({
|
|||||||
return acc;
|
return acc;
|
||||||
}, [] as Node[]);
|
}, [] as Node[]);
|
||||||
},
|
},
|
||||||
[nodeId]
|
[nodeId, contextNodeId]
|
||||||
);
|
);
|
||||||
const nodes = useStore(nodesSelector, nodesEqualityFn);
|
const nodes = useStore(nodesSelector, nodesEqualityFn);
|
||||||
const { transform, nodeOrigin, selectedNodesCount } = useStore(storeSelector, shallow);
|
const { transform, nodeOrigin, selectedNodesCount } = useStore(storeSelector, shallow);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Position } from '@reactflow/core';
|
|||||||
import type { HTMLAttributes } from 'react';
|
import type { HTMLAttributes } from 'react';
|
||||||
|
|
||||||
export type NodeToolbarProps = HTMLAttributes<HTMLDivElement> & {
|
export type NodeToolbarProps = HTMLAttributes<HTMLDivElement> & {
|
||||||
nodeId: string | string[];
|
nodeId?: string | string[];
|
||||||
isVisible?: boolean;
|
isVisible?: boolean;
|
||||||
position?: Position;
|
position?: Position;
|
||||||
offset?: number;
|
offset?: number;
|
||||||
|
|||||||
Reference in New Issue
Block a user