refactor(types): use types for prop type functions

This commit is contained in:
moklick
2020-05-15 21:53:13 +02:00
parent b887568ac8
commit 1e447a204c
8 changed files with 32 additions and 36 deletions
+2 -1
View File
@@ -1,4 +1,4 @@
import React, { memo, MouseEvent as ReactMouseEvent } from 'react'; import React, { memo, MouseEvent as ReactMouseEvent, CSSProperties } from 'react';
import cx from 'classnames'; import cx from 'classnames';
import { HandleType, ElementId, Position, XYPosition, OnConnectFunc, Connection } from '../../types'; import { HandleType, ElementId, Position, XYPosition, OnConnectFunc, Connection } from '../../types';
@@ -16,6 +16,7 @@ interface BaseHandleProps {
isValidConnection: ValidConnectionFunc; isValidConnection: ValidConnectionFunc;
id?: ElementId | boolean; id?: ElementId | boolean;
className?: string; className?: string;
style?: CSSProperties;
} }
type Result = { type Result = {
+4 -18
View File
@@ -4,36 +4,22 @@ import { useStoreActions, useStoreState } from '../../store/hooks';
import BaseHandle from './BaseHandle'; import BaseHandle from './BaseHandle';
import NodeIdContext from '../../contexts/NodeIdContext'; import NodeIdContext from '../../contexts/NodeIdContext';
import { import { HandleProps, ElementId, Position, Connection } from '../../types';
HandleType,
ElementId,
Position,
Connection,
OnConnectFunc,
} from '../../types';
interface HandleProps {
type: HandleType;
position: Position;
onConnect?: OnConnectFunc;
isValidConnection?: (connection: Connection) => boolean;
id?: string;
}
const Handle = memo( const Handle = memo(
({ ({
onConnect = _ => {},
type = 'source', type = 'source',
position = Position.Top, position = Position.Top,
onConnect = () => {},
isValidConnection = () => true, isValidConnection = () => true,
...rest ...rest
}: HandleProps) => { }: HandleProps) => {
const nodeId = useContext(NodeIdContext) as ElementId; const nodeId = useContext(NodeIdContext) as ElementId;
const { setPosition, setSourceId } = useStoreActions(a => ({ const { setPosition, setSourceId } = useStoreActions((a) => ({
setPosition: a.setConnectionPosition, setPosition: a.setConnectionPosition,
setSourceId: a.setConnectionSourceId, setSourceId: a.setConnectionSourceId,
})); }));
const onConnectAction = useStoreState(s => s.onConnect); const onConnectAction = useStoreState((s) => s.onConnect);
const onConnectExtended = (params: Connection) => { const onConnectExtended = (params: Connection) => {
onConnectAction(params); onConnectAction(params);
onConnect(params); onConnect(params);
+1 -1
View File
@@ -60,7 +60,7 @@ const onDrag = (
}; };
const onStop = ( const onStop = (
onNodeDragStop: (params: Node) => void, onNodeDragStop: (node: Node) => void,
isDragging: boolean, isDragging: boolean,
setDragging: (isDragging: boolean) => void, setDragging: (isDragging: boolean) => void,
id: ElementId, id: ElementId,
+1 -1
View File
@@ -11,7 +11,7 @@ interface EdgeRendererProps {
edgeTypes: any; edgeTypes: any;
connectionLineStyle?: CSSProperties; connectionLineStyle?: CSSProperties;
connectionLineType?: string; connectionLineType?: string;
onElementClick?: () => void; onElementClick?: (element: Node | Edge) => void;
} }
interface EdgePositions { interface EdgePositions {
+4 -4
View File
@@ -13,14 +13,14 @@ import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler';
import useElementUpdater from '../../hooks/useElementUpdater'; import useElementUpdater from '../../hooks/useElementUpdater';
import { getDimensions } from '../../utils'; import { getDimensions } from '../../utils';
import { fitView, zoomIn, zoomOut } from '../../utils/graph'; import { fitView, zoomIn, zoomOut } from '../../utils/graph';
import { Elements, NodeTypesType, EdgeTypesType, GridType, OnLoadFunc } from '../../types'; import { Elements, NodeTypesType, EdgeTypesType, GridType, OnLoadFunc, Node, Edge, Connection } from '../../types';
export interface GraphViewProps { export interface GraphViewProps {
elements: Elements; elements: Elements;
onElementClick: () => void; onElementClick: (element: Node | Edge) => void;
onElementsRemove: (elements: Elements) => void; onElementsRemove: (elements: Elements) => void;
onNodeDragStop: () => void; onNodeDragStop: (node: Node) => void;
onConnect: () => void; onConnect: (conneciton: Connection) => void;
onLoad: OnLoadFunc; onLoad: OnLoadFunc;
onMove: () => void; onMove: () => void;
selectionKeyCode: number; selectionKeyCode: number;
+5 -5
View File
@@ -2,12 +2,12 @@ import React, { memo, ComponentType } from 'react';
import { useStoreState } from '../../store/hooks'; import { useStoreState } from '../../store/hooks';
import { getNodesInside } from '../../utils/graph'; import { getNodesInside } from '../../utils/graph';
import { Node, Transform, NodeTypesType, WrapNodeProps, Elements } from '../../types'; import { Node, Transform, NodeTypesType, WrapNodeProps, Elements, Edge } from '../../types';
interface NodeRendererProps { interface NodeRendererProps {
nodeTypes: NodeTypesType; nodeTypes: NodeTypesType;
onElementClick: () => void; onElementClick: (element: Node | Edge) => void;
onNodeDragStop: () => void; onNodeDragStop: (node: Node) => void;
onlyRenderVisibleNodes?: boolean; onlyRenderVisibleNodes?: boolean;
} }
@@ -47,7 +47,7 @@ function renderNode(
} }
const NodeRenderer = memo(({ onlyRenderVisibleNodes = true, ...props }: NodeRendererProps) => { const NodeRenderer = memo(({ onlyRenderVisibleNodes = true, ...props }: NodeRendererProps) => {
const { nodes, transform, selectedElements, width, height, isInteractive } = useStoreState(s => ({ const { nodes, transform, selectedElements, width, height, isInteractive } = useStoreState((s) => ({
nodes: s.nodes, nodes: s.nodes,
transform: s.transform, transform: s.transform,
selectedElements: s.selectedElements, selectedElements: s.selectedElements,
@@ -67,7 +67,7 @@ const NodeRenderer = memo(({ onlyRenderVisibleNodes = true, ...props }: NodeRend
return ( return (
<div className="react-flow__nodes" style={transformStyle}> <div className="react-flow__nodes" style={transformStyle}>
{renderNodes.map(node => renderNode(node, props, transform, selectedElements, isInteractive))} {renderNodes.map((node) => renderNode(node, props, transform, selectedElements, isInteractive))}
</div> </div>
); );
}); });
+4 -4
View File
@@ -18,16 +18,16 @@ import StraightEdge from '../../components/Edges/StraightEdge';
import StepEdge from '../../components/Edges/StepEdge'; import StepEdge from '../../components/Edges/StepEdge';
import { createEdgeTypes } from '../EdgeRenderer/utils'; import { createEdgeTypes } from '../EdgeRenderer/utils';
import store from '../../store'; import store from '../../store';
import { Elements, NodeTypesType, EdgeTypesType, GridType, OnLoadFunc } from '../../types'; import { Elements, NodeTypesType, EdgeTypesType, GridType, OnLoadFunc, Node, Edge, Connection } from '../../types';
import '../../style.css'; import '../../style.css';
export interface ReactFlowProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onLoad'> { export interface ReactFlowProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onLoad'> {
elements: Elements; elements: Elements;
onElementClick: () => void; onElementClick: (element: Node | Edge) => void;
onElementsRemove: (elements: Elements) => void; onElementsRemove: (elements: Elements) => void;
onNodeDragStop: () => void; onNodeDragStop: (node: Node) => void;
onConnect: () => void; onConnect: (connection: Connection) => void;
onLoad: OnLoadFunc; onLoad: OnLoadFunc;
onMove: () => void; onMove: () => void;
nodeTypes: NodeTypesType; nodeTypes: NodeTypesType;
+11 -2
View File
@@ -109,7 +109,7 @@ export interface NodeComponentProps {
targetPosition?: Position; targetPosition?: Position;
sourcePosition?: Position; sourcePosition?: Position;
onClick?: (node: Node) => void | undefined; onClick?: (node: Node) => void | undefined;
onNodeDragStop?: () => any; onNodeDragStop?: (node: Node) => void;
style?: CSSProperties; style?: CSSProperties;
} }
@@ -147,13 +147,22 @@ export type Connection = {
target: ElementId | null; target: ElementId | null;
}; };
export type OnConnectFunc = (params: Connection) => void; export type OnConnectFunc = (connection: Connection) => void;
export interface HandleElement extends XYPosition, Dimensions { export interface HandleElement extends XYPosition, Dimensions {
id?: ElementId | null; id?: ElementId | null;
position: Position; position: Position;
} }
export interface HandleProps {
type: HandleType;
position: Position;
onConnect?: OnConnectFunc;
isValidConnection?: (connection: Connection) => boolean;
id?: string;
style?: CSSProperties;
}
export interface EdgeCompProps { export interface EdgeCompProps {
id: ElementId; id: ElementId;
source: ElementId; source: ElementId;