Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent node click event if node has been dragged #78

Merged
merged 2 commits into from
Jan 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/components/Node/Node.wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import Draggable from 'react-draggable'
import Draggable, { DraggableData } from 'react-draggable'
import ResizeObserver from 'react-resize-observer'
import {
IConfig, ILink, INode, INodeInnerDefaultProps, IOnDragNode,
Expand Down Expand Up @@ -58,6 +58,26 @@ export const NodeWrapper = ({
}: INodeWrapperProps) => {
const [size, setSize] = React.useState<ISize>({ width: 0, height: 0 })

const isDragging = React.useRef(false);
const onStart = React.useCallback((e: MouseEvent) => {
// Stop propagation so the canvas does not move
e.stopPropagation()
isDragging.current = false
},[]);
const onDrag = React.useCallback((event: MouseEvent, data: DraggableData) => {
isDragging.current = true
onDragNode({ config, event, data, id: node.id })
}, [onDragNode, config, node.id]);

const onClick = React.useCallback((e: React.MouseEvent) => {
if (!config.readonly) {
e.stopPropagation()
if (!isDragging.current) {
onNodeClick({ config, nodeId: node.id })
}
}
}, [config, node.id])

const compRef = React.useRef<HTMLElement>(null)

// TODO: probably should add an observer to track node component size changes
Expand Down Expand Up @@ -111,23 +131,15 @@ export const NodeWrapper = ({
axis="both"
position={node.position}
grid={[1,1]}
onStart={ (e) => {
// Stop propagation so the canvas does not move
e.stopPropagation()
}}
onDrag={(event, data) => onDragNode({ config, event, data, id: node.id })}
onStart={onStart}
onDrag={onDrag}
disabled={config.readonly}
>
<Component
config={config}
ref={compRef}
children={children}
onClick={(e) => {
if (!config.readonly) {
onNodeClick({ config, nodeId: node.id })
e.stopPropagation()
}
}}
onClick={onClick}
isSelected={isSelected}
node={node}
/>
Expand Down