feat(interactive-minimap): add node click handler
This commit is contained in:
@@ -118,6 +118,10 @@ const BasicFlow = () => {
|
|||||||
console.log(pos);
|
console.log(pos);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const onMiniMapNodeClick = useCallback((event: MouseEvent, node: Node) => {
|
||||||
|
console.log(node);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
defaultNodes={initialNodes}
|
defaultNodes={initialNodes}
|
||||||
@@ -133,7 +137,7 @@ const BasicFlow = () => {
|
|||||||
fitView
|
fitView
|
||||||
>
|
>
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
<MiniMap onClick={onMiniMapClick} />
|
<MiniMap onClick={onMiniMapClick} onNodeClick={onMiniMapNodeClick} />
|
||||||
<Controls />
|
<Controls />
|
||||||
|
|
||||||
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}>
|
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}>
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ function MiniMap({
|
|||||||
maskColor = 'rgb(200, 200, 200, 0.9)',
|
maskColor = 'rgb(200, 200, 200, 0.9)',
|
||||||
position = 'bottom-right',
|
position = 'bottom-right',
|
||||||
onClick,
|
onClick,
|
||||||
|
onNodeClick,
|
||||||
}: MiniMapProps) {
|
}: MiniMapProps) {
|
||||||
const store = useStoreApi();
|
const store = useStoreApi();
|
||||||
const svg = useRef<SVGSVGElement>(null);
|
const svg = useRef<SVGSVGElement>(null);
|
||||||
@@ -114,9 +115,20 @@ function MiniMap({
|
|||||||
d3Zoom.transform(d3Selection, nextTransform);
|
d3Zoom.transform(d3Selection, nextTransform);
|
||||||
});
|
});
|
||||||
selection.call(zoomHandler);
|
selection.call(zoomHandler);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
selection.on('.zoom', null);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const onSvgNodeClick = onNodeClick
|
||||||
|
? (event: MouseEvent, nodeId: string) => {
|
||||||
|
const node = store.getState().nodeInternals.get(nodeId)!;
|
||||||
|
onNodeClick(event, node);
|
||||||
|
}
|
||||||
|
: undefined;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Panel position={position} style={style} className={cc(['react-flow__minimap', className])}>
|
<Panel position={position} style={style} className={cc(['react-flow__minimap', className])}>
|
||||||
<svg
|
<svg
|
||||||
@@ -144,6 +156,8 @@ function MiniMap({
|
|||||||
strokeColor={nodeStrokeColorFunc(node)}
|
strokeColor={nodeStrokeColorFunc(node)}
|
||||||
strokeWidth={nodeStrokeWidth}
|
strokeWidth={nodeStrokeWidth}
|
||||||
shapeRendering={shapeRendering}
|
shapeRendering={shapeRendering}
|
||||||
|
onClick={onSvgNodeClick}
|
||||||
|
id={node.id}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { memo, CSSProperties } from 'react';
|
import { memo, CSSProperties, MouseEvent } from 'react';
|
||||||
import cc from 'classcat';
|
import cc from 'classcat';
|
||||||
|
|
||||||
interface MiniMapNodeProps {
|
interface MiniMapNodeProps {
|
||||||
|
id: string;
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
width: number;
|
width: number;
|
||||||
@@ -13,9 +14,11 @@ interface MiniMapNodeProps {
|
|||||||
strokeColor: string;
|
strokeColor: string;
|
||||||
strokeWidth: number;
|
strokeWidth: number;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
|
onClick?: (event: MouseEvent, id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MiniMapNode = ({
|
const MiniMapNode = ({
|
||||||
|
id,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
width,
|
width,
|
||||||
@@ -27,6 +30,7 @@ const MiniMapNode = ({
|
|||||||
className,
|
className,
|
||||||
borderRadius,
|
borderRadius,
|
||||||
shapeRendering,
|
shapeRendering,
|
||||||
|
onClick,
|
||||||
}: MiniMapNodeProps) => {
|
}: MiniMapNodeProps) => {
|
||||||
const { background, backgroundColor } = style || {};
|
const { background, backgroundColor } = style || {};
|
||||||
const fill = (color || background || backgroundColor) as string;
|
const fill = (color || background || backgroundColor) as string;
|
||||||
@@ -44,6 +48,7 @@ const MiniMapNode = ({
|
|||||||
stroke={strokeColor}
|
stroke={strokeColor}
|
||||||
strokeWidth={strokeWidth}
|
strokeWidth={strokeWidth}
|
||||||
shapeRendering={shapeRendering}
|
shapeRendering={shapeRendering}
|
||||||
|
onClick={onClick ? (event) => onClick(event, id) : undefined}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,4 +13,5 @@ export type MiniMapProps<NodeData = any> = Omit<HTMLAttributes<SVGSVGElement>, '
|
|||||||
maskColor?: string;
|
maskColor?: string;
|
||||||
position?: PanelPosition;
|
position?: PanelPosition;
|
||||||
onClick?: (event: MouseEvent, position: XYPosition) => void;
|
onClick?: (event: MouseEvent, position: XYPosition) => void;
|
||||||
|
onNodeClick?: (event: MouseEvent, node: Node<NodeData>) => void;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user