Merge branch 'main' into feat/packages

This commit is contained in:
moklick
2023-03-28 16:27:18 +02:00
61 changed files with 2513 additions and 1150 deletions
@@ -26,7 +26,7 @@ describe('useNodesInitialized.cy.tsx', () => {
</ReactFlow>
);
cy.get('@initSpy').should('to.be.calledOnce');
// cy.get('@initSpy').should('to.be.calledOnce');
cy.get('@initSpy').should('have.be.calledWith', false);
});
+2 -1
View File
@@ -21,7 +21,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"reactflow": "workspace:*"
"reactflow": "workspace:*",
"zustand": "^4.3.1"
},
"devDependencies": {
"@cypress/skip-test": "^2.6.1",
+6
View File
@@ -45,6 +45,7 @@ import CancelConnection from '../examples/CancelConnection';
import InteractiveMinimap from '../examples/InteractiveMinimap';
import UseOnSelectionChange from '../examples/UseOnSelectionChange';
import NodeToolbar from '../examples/NodeToolbar';
import useNodesInitialized from '../examples/UseNodesInit';
interface IRoute {
name: string;
@@ -248,6 +249,11 @@ const routes: IRoute[] = [
path: '/update-node',
component: UpdateNode,
},
{
name: 'useNodesInitialized',
path: '/use-nodes-initialized',
component: useNodesInitialized,
},
{
name: 'useOnSelectionChange',
path: '/use-on-selection-change',
@@ -19,13 +19,15 @@ const initialNodes: Node[] = [
},
];
const Flow: FC<{ id: string; bgProps: BackgroundProps }> = ({ id, bgProps }) => {
const Flow: FC<{ id: string; bgProps: BackgroundProps[] }> = ({ id, bgProps }) => {
const [nodes, , onNodesChange] = useNodesState(initialNodes);
return (
<ReactFlowProvider>
<ReactFlow nodes={nodes} onNodesChange={onNodesChange} id={id}>
<Background {...bgProps} />
{bgProps.map((props, idx) => (
<Background key={idx} id={idx.toString()} {...props} />
))}
</ReactFlow>
</ReactFlowProvider>
);
@@ -33,9 +35,16 @@ const Flow: FC<{ id: string; bgProps: BackgroundProps }> = ({ id, bgProps }) =>
const Backgrounds: FC = () => (
<div className={styles.wrapper}>
<Flow id="flow-a" bgProps={{ variant: BackgroundVariant.Dots }} />
<Flow id="flow-b" bgProps={{ variant: BackgroundVariant.Lines, gap: [50, 50] }} />
<Flow id="flow-c" bgProps={{ variant: BackgroundVariant.Cross, gap: [100, 50] }} />
<Flow id="flow-a" bgProps={[{ variant: BackgroundVariant.Dots }]} />
<Flow id="flow-b" bgProps={[{ variant: BackgroundVariant.Lines, gap: [50, 50] }]} />
<Flow id="flow-c" bgProps={[{ variant: BackgroundVariant.Cross, gap: [100, 50] }]} />
<Flow
id="flow-d"
bgProps={[
{ variant: BackgroundVariant.Lines, gap: 10 },
{ variant: BackgroundVariant.Lines, gap: 100, offset: 2, color: '#ccc' },
]}
/>
</div>
);
@@ -1,4 +1,4 @@
import { MouseEvent, useCallback } from 'react';
import { MouseEvent, useCallback, useState } from 'react';
import ReactFlow, {
MiniMap,
Background,
@@ -87,6 +87,7 @@ const defaultEdgeOptions = { zIndex: 0 };
const BasicFlow = () => {
const instance = useReactFlow();
const [inverse, setInverse] = useState(false);
const updatePos = () => {
instance.setNodes((nodes) =>
@@ -103,6 +104,7 @@ const BasicFlow = () => {
const logToObject = () => console.log(instance.toObject());
const resetTransform = () => instance.setViewport({ x: 0, y: 0, zoom: 1 });
const toggleInverse = () => setInverse(!inverse);
const toggleClassnames = () => {
instance.setNodes((nodes) =>
@@ -137,7 +139,8 @@ const BasicFlow = () => {
fitView
>
<Background variant={BackgroundVariant.Dots} />
<MiniMap onClick={onMiniMapClick} onNodeClick={onMiniMapNodeClick} pannable zoomable />
<MiniMap onClick={onMiniMapClick} onNodeClick={onMiniMapNodeClick} pannable zoomable
inversePan={inverse}/>
<Controls />
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}>
@@ -150,7 +153,12 @@ const BasicFlow = () => {
<button onClick={toggleClassnames} style={{ marginRight: 5 }}>
toggle classnames
</button>
<button onClick={logToObject}>toObject</button>
<button onClick={logToObject} style={{ marginRight: 5 }}>
toObject
</button>
<button onClick={toggleInverse} style={{ marginRight: 5 }}>
{inverse ? 'un-inverse pan' : 'inverse pan'}
</button>
</div>
</ReactFlow>
);
@@ -1,8 +1,6 @@
import { memo, FC } from 'react';
import { Handle, Position, NodeProps } from 'reactflow';
import { Handle, Position, NodeProps, NodeResizeControl } from 'reactflow';
import { NodeResizeControl } from '@reactflow/node-resizer';
import '@reactflow/node-resizer/dist/style.css';
import ResizeIcon from './ResizeIcon';
const controlStyle = {
@@ -1,8 +1,5 @@
import { memo, FC } from 'react';
import { Handle, Position, NodeProps } from 'reactflow';
import { NodeResizer } from '@reactflow/node-resizer';
import '@reactflow/node-resizer/dist/style.css';
import { Handle, Position, NodeProps, NodeResizer } from 'reactflow';
const DefaultResizerNode: FC<NodeProps> = ({ data, selected }) => {
return (
@@ -1,8 +1,5 @@
import { memo, FC } from 'react';
import { Handle, Position, NodeProps } from 'reactflow';
import { NodeResizeControl } from '@reactflow/node-resizer';
import '@reactflow/node-resizer/dist/style.css';
import { Handle, Position, NodeProps, NodeResizeControl } from 'reactflow';
const HorizontalResizerNode: FC<NodeProps> = ({ data }) => {
return (
@@ -1,8 +1,5 @@
import { memo, FC } from 'react';
import { Handle, Position, NodeProps } from 'reactflow';
import { NodeResizeControl } from '@reactflow/node-resizer';
import '@reactflow/node-resizer/dist/style.css';
import { Handle, Position, NodeProps, NodeResizeControl } from 'reactflow';
const CustomNode: FC<NodeProps> = ({ id, data }) => {
return (
@@ -6,6 +6,8 @@ import CustomResizer from './CustomResizer';
import VerticalResizer from './VerticalResizer';
import HorizontalResizer from './HorizontalResizer';
import 'reactflow/dist/style.css';
const nodeTypes = {
defaultResizer: DefaultResizer,
customResizer: CustomResizer,
@@ -36,7 +36,7 @@ const initialNodes: Node[] = [
</>
),
},
position: { x: 100, y: 100 },
position: { x: 75, y: 0 },
},
{
id: '3',
@@ -55,9 +55,46 @@ const initialNodes: Node[] = [
width: 180,
},
},
{
id: '4',
data: {
label: (
<>
Node <strong>D</strong>
</>
),
},
position: { x: -75, y: 100 },
},
{
id: '5',
data: {
label: (
<>
Node <strong>E</strong>
</>
),
},
position: { x: 150, y: 100 },
},
{
id: '6',
data: {
label: (
<>
Node <strong>F</strong>
</>
),
},
position: { x: 150, y: 250 },
},
];
const initialEdges = [{ id: 'e1-2', source: '1', target: '2', label: 'This is a draggable edge' }];
const initialEdges: Edge[] = [
{ id: 'e1-3', source: '1', target: '3', label: 'This edge can only be updated from source', updatable: 'source' },
{ id: 'e2-4', source: '2', target: '4', label: 'This edge can only be updated from target', updatable: 'target' },
{ id: 'e5-6', source: '5', target: '6', label: 'This edge can be updated from both sides' },
];
const onInit = (reactFlowInstance: ReactFlowInstance) => reactFlowInstance.fitView();
const onEdgeUpdateStart = (_: ReactMouseEvent, edge: Edge, handleType: HandleType) =>
@@ -0,0 +1,83 @@
import { useCallback, useEffect } from 'react';
import ReactFlow, {
Background,
MiniMap,
Node,
addEdge,
ReactFlowProvider,
Edge,
useNodesState,
useEdgesState,
OnConnect,
useNodesInitialized,
} from 'reactflow';
const initialNodes: Node[] = [
{
id: '1',
type: 'input',
data: { label: 'Node 1' },
position: { x: 250, y: 5 },
className: 'light',
},
{
id: '2',
data: { label: 'Node 2' },
position: { x: 100, y: 100 },
className: 'light',
},
{
id: '3',
data: { label: 'Node 3' },
position: { x: 400, y: 100 },
className: 'light',
},
{
id: '4',
data: { label: 'Node 4' },
position: { x: 400, y: 200 },
className: 'light',
hidden: true,
},
];
const initialEdges: Edge[] = [
{ id: 'e1-2', source: '1', target: '2', animated: true },
{ id: 'e1-3', source: '1', target: '3' },
// { id: 'e3-4', source: '3', target: '4' }
];
const UseZoomPanHelperFlow = () => {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect: OnConnect = useCallback((params) => setEdges((eds) => addEdge(params, eds)), []);
const initialized = useNodesInitialized();
useEffect(() => {
console.log('initialized', initialized);
}, [initialized]);
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
fitView
maxZoom={Infinity}
>
<Background />
<MiniMap />
</ReactFlow>
);
};
const WrappedFlow = () => (
<ReactFlowProvider>
<UseZoomPanHelperFlow />
</ReactFlowProvider>
);
export default WrappedFlow;
@@ -0,0 +1,50 @@
import { ReactFlowState, useStore } from 'reactflow';
import { shallow } from 'zustand/shallow';
import styles from './validation.module.css';
const selector = (state: ReactFlowState) => ({
connectionPosition: state.connectionPosition,
connectionStatus: state.connectionStatus,
connectionStartNodeId: state.connectionStartHandle?.nodeId,
connectionStartHandleType: state.connectionStartHandle?.type,
connectionEndNodeId: state.connectionEndHandle?.nodeId,
connectionEndHandleType: state.connectionEndHandle?.type,
});
function ConnectionStatus() {
const {
connectionPosition,
connectionStatus,
connectionStartNodeId,
connectionStartHandleType,
connectionEndNodeId,
connectionEndHandleType,
} = useStore(selector, shallow);
if (!connectionPosition) {
return null;
}
return (
<div className={styles.connectionstatus}>
{connectionStartNodeId ? (
<>
<div>
<strong>connection info</strong>
</div>
<div>position: {JSON.stringify(connectionPosition)}</div>
<div>status: {connectionStatus}</div>
<div>from node id: {connectionStartNodeId}</div>
<div>from handle type: {connectionStartHandleType}</div>
<div>to node id: {connectionEndNodeId}</div>
<div>to handle type: {connectionEndHandleType}</div>
</>
) : (
'no connection data'
)}
</div>
);
}
export default ConnectionStatus;
@@ -16,6 +16,8 @@ import ReactFlow, {
Edge,
} from 'reactflow';
import ConnectionStatus from './ConnectionStatus';
import styles from './validation.module.css';
const initialNodes: Node[] = [
@@ -36,7 +38,7 @@ const CustomInput: FC<NodeProps> = () => (
const CustomNode: FC<NodeProps> = ({ id }) => (
<>
<Handle type="target" position={Position.Left} />
<Handle type="target" position={Position.Left} isConnectableStart={false} />
<div>{id}</div>
<Handle type="source" position={Position.Right} />
</>
@@ -96,7 +98,9 @@ const ValidationFlow = () => {
onEdgeUpdate={onEdgeUpdate}
isValidConnection={isValidConnection}
fitView
/>
>
<ConnectionStatus />
</ReactFlow>
);
};
@@ -39,3 +39,10 @@
.validationflow :global .invalid .react-flow__connection-path {
stroke: #ff6060;
}
.connectionstatus {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}