Merge pull request #5013 from xyflow/fix-connection-component-type

Fix connection component type
This commit is contained in:
Moritz Klack
2025-02-12 14:22:12 +01:00
committed by GitHub
4 changed files with 27 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@xyflow/react': patch
---
Pass `NodeType` type argument from `ReactFlowProps` to `connectionLineComponent` property.
@@ -11,12 +11,12 @@ import {
import { useStore } from '../../hooks/useStore'; import { useStore } from '../../hooks/useStore';
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge'; import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge';
import type { ConnectionLineComponent, ReactFlowState } from '../../types'; import type { ConnectionLineComponent, Node, ReactFlowState } from '../../types';
import { useConnection } from '../../hooks/useConnection'; import { useConnection } from '../../hooks/useConnection';
type ConnectionLineWrapperProps = { type ConnectionLineWrapperProps<NodeType extends Node = Node> = {
type: ConnectionLineType; type: ConnectionLineType;
component?: ConnectionLineComponent; component?: ConnectionLineComponent<NodeType>;
containerStyle?: CSSProperties; containerStyle?: CSSProperties;
style?: CSSProperties; style?: CSSProperties;
}; };
@@ -29,7 +29,12 @@ const selector = (s: ReactFlowState) => ({
height: s.height, height: s.height,
}); });
export function ConnectionLineWrapper({ containerStyle, style, type, component }: ConnectionLineWrapperProps) { export function ConnectionLineWrapper<NodeType extends Node = Node>({
containerStyle,
style,
type,
component,
}: ConnectionLineWrapperProps<NodeType>) {
const { nodesConnectable, width, height, isValid, inProgress } = useStore(selector, shallow); const { nodesConnectable, width, height, isValid, inProgress } = useStore(selector, shallow);
const renderConnection = !!(width && nodesConnectable && inProgress); const renderConnection = !!(width && nodesConnectable && inProgress);
@@ -45,21 +50,27 @@ export function ConnectionLineWrapper({ containerStyle, style, type, component }
className="react-flow__connectionline react-flow__container" className="react-flow__connectionline react-flow__container"
> >
<g className={cc(['react-flow__connection', getConnectionStatus(isValid)])}> <g className={cc(['react-flow__connection', getConnectionStatus(isValid)])}>
<ConnectionLine style={style} type={type} CustomComponent={component} isValid={isValid} /> <ConnectionLine<NodeType> style={style} type={type} CustomComponent={component} isValid={isValid} />
</g> </g>
</svg> </svg>
); );
} }
type ConnectionLineProps = { type ConnectionLineProps<NodeType extends Node = Node> = {
type: ConnectionLineType; type: ConnectionLineType;
style?: CSSProperties; style?: CSSProperties;
CustomComponent?: ConnectionLineComponent; CustomComponent?: ConnectionLineComponent<NodeType>;
isValid: boolean | null; isValid: boolean | null;
}; };
const ConnectionLine = ({ style, type = ConnectionLineType.Bezier, CustomComponent, isValid }: ConnectionLineProps) => { const ConnectionLine = <NodeType extends Node = Node>({
const { inProgress, from, fromNode, fromHandle, fromPosition, to, toNode, toHandle, toPosition } = useConnection(); style,
type = ConnectionLineType.Bezier,
CustomComponent,
isValid,
}: ConnectionLineProps<NodeType>) => {
const { inProgress, from, fromNode, fromHandle, fromPosition, to, toNode, toHandle, toPosition } =
useConnection<NodeType>();
if (!inProgress) { if (!inProgress) {
return; return;
@@ -170,7 +170,7 @@ function GraphViewComponent<NodeType extends Node = Node, EdgeType extends Edge
disableKeyboardA11y={disableKeyboardA11y} disableKeyboardA11y={disableKeyboardA11y}
rfId={rfId} rfId={rfId}
/> />
<ConnectionLineWrapper <ConnectionLineWrapper<NodeType>
style={connectionLineStyle} style={connectionLineStyle}
type={connectionLineType} type={connectionLineType}
component={connectionLineComponent} component={connectionLineComponent}
+1 -1
View File
@@ -259,7 +259,7 @@ export interface ReactFlowProps<NodeType extends Node = Node, EdgeType extends E
/** Styles to be applied to the connection line */ /** Styles to be applied to the connection line */
connectionLineStyle?: CSSProperties; connectionLineStyle?: CSSProperties;
/** React Component to be used as a connection line */ /** React Component to be used as a connection line */
connectionLineComponent?: ConnectionLineComponent; connectionLineComponent?: ConnectionLineComponent<NodeType>;
/** Styles to be applied to the container of the connection line */ /** Styles to be applied to the container of the connection line */
connectionLineContainerStyle?: CSSProperties; connectionLineContainerStyle?: CSSProperties;
/** 'strict' connection mode will only allow you to connect source handles to target handles. /** 'strict' connection mode will only allow you to connect source handles to target handles.