feat(node-resizer): add onBeforeResize handler

This commit is contained in:
moklick
2023-01-12 14:34:39 +01:00
parent 893ee87838
commit 4d7a5e785c
8 changed files with 61 additions and 7 deletions
@@ -1,7 +1,7 @@
import { memo, FC, CSSProperties } from 'react'; import { memo, FC } from 'react';
import { Handle, Position, NodeProps } from 'reactflow'; import { Handle, Position, NodeProps } from 'reactflow';
import { NodeResizer, NodeResizeControl } from '@reactflow/node-resizer'; import { NodeResizeControl } from '@reactflow/node-resizer';
import '@reactflow/node-resizer/dist/style.css'; import '@reactflow/node-resizer/dist/style.css';
import ResizeIcon from './ResizeIcon'; import ResizeIcon from './ResizeIcon';
@@ -1,14 +1,14 @@
import { memo, FC } from 'react'; import { memo, FC } from 'react';
import { Handle, Position, NodeProps } from 'reactflow'; import { Handle, Position, NodeProps } from 'reactflow';
import { NodeResizer, NodeResizeControl } from '@reactflow/node-resizer'; import { NodeResizeControl } from '@reactflow/node-resizer';
import '@reactflow/node-resizer/dist/style.css'; import '@reactflow/node-resizer/dist/style.css';
const CustomNode: FC<NodeProps> = ({ id, data }) => { const CustomNode: FC<NodeProps> = ({ id, data }) => {
return ( return (
<> <>
<NodeResizeControl color="red" position="top" /> <NodeResizeControl color="red" position={Position.Top} />
<NodeResizeControl color="red" position="bottom" /> <NodeResizeControl color="red" position={Position.Bottom} />
<Handle type="target" position={Position.Left} /> <Handle type="target" position={Position.Left} />
<div style={{ padding: 10 }}>{data.label}</div> <div style={{ padding: 10 }}>{data.label}</div>
<Handle type="source" position={Position.Right} /> <Handle type="source" position={Position.Right} />
@@ -0,0 +1,31 @@
import { memo, FC } from 'react';
import { Handle, Position, NodeProps } from 'reactflow';
import { NodeResizeControl, ResizeDragEvent, ResizeEventParams } from '@reactflow/node-resizer';
import '@reactflow/node-resizer/dist/style.css';
const onBeforeResize = (event: ResizeDragEvent, params: ResizeEventParams) => {
console.log('before resize', params);
if (params.width > 100) {
return false;
}
};
const onResize = (event: ResizeDragEvent, params: ResizeEventParams) => {
console.log('resize', params);
};
const CustomNode: FC<NodeProps> = ({ id, data }) => {
return (
<>
<NodeResizeControl color="red" position={Position.Left} />
<NodeResizeControl color="red" position={Position.Right} onBeforeResize={onBeforeResize} onResize={onResize} />
<Handle type="target" position={Position.Top} />
<div style={{ padding: 10 }}>{data.label}</div>
<Handle type="source" position={Position.Bottom} />
</>
);
};
export default memo(CustomNode);
@@ -8,6 +8,10 @@ const onResizeStart = (_: ResizeDragEvent, params: ResizeEventParams) => {
console.log('resize start', params); console.log('resize start', params);
}; };
const onBeforeResize = (_: ResizeDragEvent, params: ResizeEventParams) => {
console.log('before resize', params);
};
const onResize = (_: ResizeDragEvent, params: ResizeEventParams) => { const onResize = (_: ResizeDragEvent, params: ResizeEventParams) => {
console.log('resize', params); console.log('resize', params);
}; };
@@ -24,6 +28,7 @@ const CustomNode: FC<NodeProps> = ({ data, selected }) => {
minHeight={100} minHeight={100}
isVisible={selected} isVisible={selected}
onResizeStart={onResizeStart} onResizeStart={onResizeStart}
onBeforeResize={onBeforeResize}
onResize={onResize} onResize={onResize}
onResizeEnd={onResizeEnd} onResizeEnd={onResizeEnd}
/> />
@@ -4,11 +4,13 @@ import ReactFlow, { Controls, addEdge, Position, Connection, useNodesState, useE
import NodeResizerNode from './NodeResizerNode'; import NodeResizerNode from './NodeResizerNode';
import CustomResizer from './CustomResizer'; import CustomResizer from './CustomResizer';
import CustomResizer2 from './CustomResizer2'; import CustomResizer2 from './CustomResizer2';
import CustomResizer3 from './CustomResizer3';
const nodeTypes = { const nodeTypes = {
resizer: NodeResizerNode, resizer: NodeResizerNode,
customResizer: CustomResizer, customResizer: CustomResizer,
customResizer2: CustomResizer2, customResizer2: CustomResizer2,
customResizer3: CustomResizer3,
}; };
const initialEdges = [ const initialEdges = [
@@ -60,6 +62,13 @@ const initialNodes = [
position: { x: 250, y: 250 }, position: { x: 250, y: 250 },
style: { border: '1px solid #222', fontSize: 10 }, style: { border: '1px solid #222', fontSize: 10 },
}, },
{
id: '6',
type: 'customResizer3',
data: { label: 'resize controls' },
position: { x: 400, y: 200 },
style: { border: '1px solid #222', fontSize: 10 },
},
]; ];
const CustomNodeFlow = () => { const CustomNodeFlow = () => {
@@ -87,7 +96,7 @@ const CustomNodeFlow = () => {
> >
<Controls /> <Controls />
<Panel position="bottom-right"> <Panel position="bottom-right">
<button onClick={() => setSnapToGrid(!snapToGrid)}>snapToGrid: {snapToGrid ? 'on' : 'off'}</button> <button onClick={() => setSnapToGrid((s) => !s)}>snapToGrid: {snapToGrid ? 'on' : 'off'}</button>
</Panel> </Panel>
</ReactFlow> </ReactFlow>
); );
@@ -15,6 +15,7 @@ export default function NodeResizer({
minWidth = 10, minWidth = 10,
minHeight = 10, minHeight = 10,
onResizeStart, onResizeStart,
onBeforeResize,
onResize, onResize,
onResizeEnd, onResizeEnd,
}: NodeResizerProps) { }: NodeResizerProps) {
@@ -36,6 +37,7 @@ export default function NodeResizer({
minWidth={minWidth} minWidth={minWidth}
minHeight={minHeight} minHeight={minHeight}
onResizeStart={onResizeStart} onResizeStart={onResizeStart}
onBeforeResize={onBeforeResize}
onResize={onResize} onResize={onResize}
onResizeEnd={onResizeEnd} onResizeEnd={onResizeEnd}
/> />
@@ -51,6 +53,7 @@ export default function NodeResizer({
minWidth={minWidth} minWidth={minWidth}
minHeight={minHeight} minHeight={minHeight}
onResizeStart={onResizeStart} onResizeStart={onResizeStart}
onBeforeResize={onBeforeResize}
onResize={onResize} onResize={onResize}
onResizeEnd={onResizeEnd} onResizeEnd={onResizeEnd}
/> />
@@ -32,6 +32,7 @@ function ResizeControl({
minWidth = 10, minWidth = 10,
minHeight = 10, minHeight = 10,
onResizeStart, onResizeStart,
onBeforeResize,
onResize, onResize,
onResizeEnd, onResizeEnd,
}: ResizeControlProps) { }: ResizeControlProps) {
@@ -98,6 +99,7 @@ function ResizeControl({
const height = Math.max(startHeight + (invertY ? -distY : distY), minHeight); const height = Math.max(startHeight + (invertY ? -distY : distY), minHeight);
const isWidthChange = width !== prevWidth; const isWidthChange = width !== prevWidth;
const isHeightChange = height !== prevHeight; const isHeightChange = height !== prevHeight;
const onBeforeResizeResult = onBeforeResize?.(event, { ...prevValues.current });
if (invertX || invertY) { if (invertX || invertY) {
const x = invertX ? startNodeX - (width - startWidth) : startNodeX; const x = invertX ? startNodeX - (width - startWidth) : startNodeX;
@@ -139,6 +141,9 @@ function ResizeControl({
prevValues.current.width = width; prevValues.current.width = width;
prevValues.current.height = height; prevValues.current.height = height;
} }
if (onBeforeResizeResult === false) {
return;
}
onResize?.(event, { ...prevValues.current }); onResize?.(event, { ...prevValues.current });
triggerNodeChanges(changes); triggerNodeChanges(changes);
+2 -1
View File
@@ -19,6 +19,7 @@ export type NodeResizerProps = {
minWidth?: number; minWidth?: number;
minHeight?: number; minHeight?: number;
onResizeStart?: (event: ResizeDragEvent, params: ResizeEventParams) => void; onResizeStart?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
onBeforeResize?: (event: ResizeDragEvent, params: ResizeEventParams) => unknown;
onResize?: (event: ResizeDragEvent, params: ResizeEventParams) => void; onResize?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
onResizeEnd?: (event: ResizeDragEvent, params: ResizeEventParams) => void; onResizeEnd?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
}; };
@@ -34,7 +35,7 @@ export enum ResizeControlVariant {
export type ResizeControlProps = Pick< export type ResizeControlProps = Pick<
NodeResizerProps, NodeResizerProps,
'nodeId' | 'color' | 'minWidth' | 'minHeight' | 'onResizeStart' | 'onResize' | 'onResizeEnd' 'nodeId' | 'color' | 'minWidth' | 'minHeight' | 'onResizeStart' | 'onBeforeResize' | 'onResize' | 'onResizeEnd'
> & { > & {
position?: ControlPosition; position?: ControlPosition;
variant?: ResizeControlVariant; variant?: ResizeControlVariant;