fix(OnSelectionChangeFunc): pass node and edge type generics #5023

This commit is contained in:
moklick
2025-02-23 18:32:43 +01:00
parent 1f6ac8b197
commit d094ef0581
4 changed files with 20 additions and 16 deletions
@@ -1,10 +1,10 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useStoreApi } from './useStore'; import { useStoreApi } from './useStore';
import type { OnSelectionChangeFunc } from '../types'; import type { OnSelectionChangeFunc, Node, Edge } from '../types';
export type UseOnSelectionChangeOptions = { export type UseOnSelectionChangeOptions<NodeType extends Node = Node, EdgeType extends Edge = Edge> = {
onChange: OnSelectionChangeFunc; onChange: OnSelectionChangeFunc<NodeType, EdgeType>;
}; };
/** /**
@@ -45,8 +45,10 @@ export type UseOnSelectionChangeOptions = {
* *
* @remarks You need to memoize the passed `onChange` handler, otherwise the hook will not work correctly. * @remarks You need to memoize the passed `onChange` handler, otherwise the hook will not work correctly.
*/ */
export function useOnSelectionChange({ onChange }: UseOnSelectionChangeOptions) { export function useOnSelectionChange<NodeType extends Node = Node, EdgeType extends Edge = Edge>({
const store = useStoreApi(); onChange,
}: UseOnSelectionChangeOptions<NodeType, EdgeType>) {
const store = useStoreApi<NodeType, EdgeType>();
useEffect(() => { useEffect(() => {
const nextOnSelectionChangeHandlers = [...store.getState().onSelectionChangeHandlers, onChange]; const nextOnSelectionChangeHandlers = [...store.getState().onSelectionChangeHandlers, onChange];
+2 -2
View File
@@ -216,7 +216,7 @@ export interface ReactFlowProps<NodeType extends Node = Node, EdgeType extends E
/** This event handler gets called when a user stops panning or zooming the viewport */ /** This event handler gets called when a user stops panning or zooming the viewport */
onMoveEnd?: OnMoveEnd; onMoveEnd?: OnMoveEnd;
/** This event handler gets called when a user changes group of selected elements in the flow */ /** This event handler gets called when a user changes group of selected elements in the flow */
onSelectionChange?: OnSelectionChangeFunc; onSelectionChange?: OnSelectionChangeFunc<NodeType, EdgeType>;
/** This event handler gets called when user scroll inside the pane */ /** This event handler gets called when user scroll inside the pane */
onPaneScroll?: (event?: WheelEvent) => void; onPaneScroll?: (event?: WheelEvent) => void;
/** This event handler gets called when user clicks inside the pane */ /** This event handler gets called when user clicks inside the pane */
@@ -316,7 +316,7 @@ export interface ReactFlowProps<NodeType extends Node = Node, EdgeType extends E
* *
* By setting this prop to null you can disable this functionality. * By setting this prop to null you can disable this functionality.
* @default 'Meta' for macOS, "Ctrl" for other systems * @default 'Meta' for macOS, "Ctrl" for other systems
* *
*/ */
zoomActivationKeyCode?: KeyCode | null; zoomActivationKeyCode?: KeyCode | null;
/** Set this prop to make the flow snap to the grid */ /** Set this prop to make the flow snap to the grid */
+9 -7
View File
@@ -85,17 +85,19 @@ export type EdgeTypes = Record<
> >
>; >;
export type UnselectNodesAndEdgesParams = { export type UnselectNodesAndEdgesParams<NodeType extends Node = Node, EdgeType extends Edge = Edge> = {
nodes?: Node[]; nodes?: NodeType[];
edges?: Edge[]; edges?: EdgeType[];
}; };
export type OnSelectionChangeParams = { export type OnSelectionChangeParams<NodeType extends Node = Node, EdgeType extends Edge = Edge> = {
nodes: Node[]; nodes: NodeType[];
edges: Edge[]; edges: EdgeType[];
}; };
export type OnSelectionChangeFunc = (params: OnSelectionChangeParams) => void; export type OnSelectionChangeFunc<NodeType extends Node = Node, EdgeType extends Edge = Edge> = (
params: OnSelectionChangeParams<NodeType, EdgeType>
) => void;
export type FitViewParams<NodeType extends Node = Node> = FitViewParamsBase<NodeType>; export type FitViewParams<NodeType extends Node = Node> = FitViewParamsBase<NodeType>;
+2 -2
View File
@@ -134,7 +134,7 @@ export type ReactFlowStore<NodeType extends Node = Node, EdgeType extends Edge =
onViewportChangeEnd?: OnViewportChange; onViewportChangeEnd?: OnViewportChange;
onBeforeDelete?: OnBeforeDelete<NodeType, EdgeType>; onBeforeDelete?: OnBeforeDelete<NodeType, EdgeType>;
onSelectionChangeHandlers: OnSelectionChangeFunc[]; onSelectionChangeHandlers: OnSelectionChangeFunc<NodeType, EdgeType>[];
ariaLiveMessage: string; ariaLiveMessage: string;
autoPanOnConnect: boolean; autoPanOnConnect: boolean;
@@ -155,7 +155,7 @@ export type ReactFlowActions<NodeType extends Node, EdgeType extends Edge> = {
updateNodeInternals: (updates: Map<string, InternalNodeUpdate>, params?: { triggerFitView: boolean }) => void; updateNodeInternals: (updates: Map<string, InternalNodeUpdate>, params?: { triggerFitView: boolean }) => void;
updateNodePositions: UpdateNodePositions; updateNodePositions: UpdateNodePositions;
resetSelectedElements: () => void; resetSelectedElements: () => void;
unselectNodesAndEdges: (params?: UnselectNodesAndEdgesParams) => void; unselectNodesAndEdges: (params?: UnselectNodesAndEdgesParams<NodeType, EdgeType>) => void;
addSelectedNodes: (nodeIds: string[]) => void; addSelectedNodes: (nodeIds: string[]) => void;
addSelectedEdges: (edgeIds: string[]) => void; addSelectedEdges: (edgeIds: string[]) => void;
setMinZoom: (minZoom: number) => void; setMinZoom: (minZoom: number) => void;