fix: fixed an issue where Handle component onMouseDown event could not be bind

This commit is contained in:
hiyangguo
2022-05-13 19:54:26 +08:00
parent 415fc77a3c
commit a07f3dc528
4 changed files with 18 additions and 7 deletions
+10 -1
View File
@@ -16,7 +16,16 @@ const ColorSelectorNode: FC<NodeProps> = ({ data, isConnectable }) => {
Custom Color Picker Node: <strong>{data.color}</strong> Custom Color Picker Node: <strong>{data.color}</strong>
</div> </div>
<input className="nodrag" type="color" onChange={data.onChange} defaultValue={data.color} /> <input className="nodrag" type="color" onChange={data.onChange} defaultValue={data.color} />
<Handle type="source" position={Position.Right} id="a" style={sourceHandleStyleA} isConnectable={isConnectable} /> <Handle
type="source"
position={Position.Right}
id="a"
style={sourceHandleStyleA}
isConnectable={isConnectable}
onMouseDown={(e) => {
console.log('You trigger mousedown event', e);
}}
/>
<Handle type="source" position={Position.Right} id="b" style={sourceHandleStyleB} isConnectable={isConnectable} /> <Handle type="source" position={Position.Right} id="b" style={sourceHandleStyleB} isConnectable={isConnectable} />
</> </>
); );
+2 -2
View File
@@ -4,7 +4,7 @@ import shallow from 'zustand/shallow';
import { useStore, useStoreApi } from '../../store'; import { useStore, useStoreApi } from '../../store';
import { Edge, EdgeProps, WrapEdgeProps, ReactFlowState, Connection } from '../../types'; import { Edge, EdgeProps, WrapEdgeProps, ReactFlowState, Connection } from '../../types';
import { onMouseDown } from '../../components/Handle/handler'; import { handleMouseDown } from '../../components/Handle/handler';
import { EdgeAnchor } from './EdgeAnchor'; import { EdgeAnchor } from './EdgeAnchor';
import { getMarkerId } from '../../utils/graph'; import { getMarkerId } from '../../utils/graph';
@@ -160,7 +160,7 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
} }
}; };
onMouseDown( handleMouseDown(
event, event,
handleId, handleId,
nodeId, nodeId,
+1 -1
View File
@@ -82,7 +82,7 @@ function resetRecentHandle(hoveredHandle: Element): void {
hoveredHandle?.classList.remove('react-flow__handle-connecting'); hoveredHandle?.classList.remove('react-flow__handle-connecting');
} }
export function onMouseDown( export function handleMouseDown(
event: ReactMouseEvent, event: ReactMouseEvent,
handleId: string | null, handleId: string | null,
nodeId: string, nodeId: string,
+5 -3
View File
@@ -5,7 +5,7 @@ import shallow from 'zustand/shallow';
import { useStore, useStoreApi } from '../../store'; import { useStore, useStoreApi } from '../../store';
import NodeIdContext from '../../contexts/NodeIdContext'; import NodeIdContext from '../../contexts/NodeIdContext';
import { HandleProps, Connection, ReactFlowState, Position } from '../../types'; import { HandleProps, Connection, ReactFlowState, Position } from '../../types';
import { checkElementBelowIsValid, onMouseDown } from './handler'; import { checkElementBelowIsValid, handleMouseDown } from './handler';
import { getHostForElement } from '../../utils'; import { getHostForElement } from '../../utils';
import { addEdge } from '../../utils/graph'; import { addEdge } from '../../utils/graph';
@@ -35,6 +35,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
onConnect, onConnect,
children, children,
className, className,
onMouseDown,
...rest ...rest
}, },
ref ref
@@ -75,9 +76,9 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
); );
const onMouseDownHandler = useCallback( const onMouseDownHandler = useCallback(
(event: React.MouseEvent) => { (event: React.MouseEvent<HTMLDivElement>) => {
if (event.button === 0) { if (event.button === 0) {
onMouseDown( handleMouseDown(
event, event,
handleId, handleId,
nodeId, nodeId,
@@ -93,6 +94,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
onConnectEnd onConnectEnd
); );
} }
onMouseDown?.(event);
}, },
[ [
handleId, handleId,