Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@thedanchez
Copy link

@thedanchez thedanchez commented Mar 21, 2025

Summary

Similar to #5102 -- this PR updates the existing generic typing for XYDrag as I saw there were gaps in it. This one could perhaps be more sensitive than the other change as it was more additive in nature compared to this one. I'm uncertain about the sensitivity of the change, but just wanted to make sure that the NodeType/EdgeType generics get passed throughout the system's type definitions.

Feel free to poke holes in this PR or reject it all together. Hoping it can be helpful 🙂

@changeset-bot
Copy link

changeset-bot bot commented Mar 21, 2025

⚠️ No Changeset found

Latest commit: 04c71ee

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Comment on lines -90 to -91
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function XYDrag<OnNodeDrag extends (e: any, nodes: any, node: any) => void | undefined>({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was definitely keen on removing references to any here. Hopefully the adjustments I made won't be breaking for consumers. To mitigate that, we lean on default type assignments for the generics.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The system package is not documented and I assume that no one besides us is using it currently, so this should be fine. For a React Flow user this should not have any effect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this would be helpful to others who want to build on the underlying system to build Flow wrappers for other libraries. Would be good for system to have a good generic foundation 🙂

nodes: NodeBase[];
nodeLookup: Map<string, InternalNodeBase>;
edges: EdgeBase[];
type OnNodeDrag<NodeType extends NodeBase = NodeBase> = (e: MouseEvent | TouchEvent, node: NodeType, nodes: NodeType[]) => void | undefined;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I converted the original OnDrag type generic to that was originally defined using any.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. It seems there is a mismatch with the current OnNodeDrag type that is defined in the react package. There the event is just a MouseEvent. That could be a bug in the library 🤔

nodes: NodeBase[];
nodeLookup: Map<string, InternalNodeBase>;
edges: EdgeBase[];
type OnNodeDrag<NodeType extends NodeBase = NodeBase> = (e: MouseEvent | TouchEvent, node: NodeType, nodes: NodeType[]) => void | undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. It seems there is a mismatch with the current OnNodeDrag type that is defined in the react package. There the event is just a MouseEvent. That could be a bug in the library 🤔

@thedanchez thedanchez force-pushed the xydrag-type-generics branch from 4258e1b to a4bf2ed Compare June 14, 2025 01:37
@thedanchez thedanchez force-pushed the xydrag-type-generics branch from a4bf2ed to 04c71ee Compare June 14, 2025 01:42
onSelectionDragStart?: OnSelectionDrag<NodeType>;
onSelectionDrag?: OnSelectionDrag<NodeType>;
onSelectionDragStop?: OnSelectionDrag<NodeType>;
updateNodePositions: UpdateNodePositions<InternalNodeBase<NodeType>>;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this wont be a problem as UpdateNodePositions accepts a generic type based on InternalNodeBase and this time we're passing the NodeType info through along with it.

Comment on lines +97 to +103
export function XYDrag<NodeType extends NodeBase = NodeBase, EdgeType extends EdgeBase = EdgeBase>({
onNodeMouseDown,
getStoreItems,
onDragStart,
onDrag,
onDragStop,
}: XYDragParams<OnNodeDrag>): XYDragInstance {
}: XYDragParams<NodeType, EdgeType>): XYDragInstance {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we're passing both NodeType + EdgeType to XYDragParams which will type the underlying store getter as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants