NodeProps<T>
当你实现 自定义节点 时,它被封装在一个组件中,该组件启用了选择和拖动等基本功能。你的自定义节点接收以下属性:
¥When you implement a custom node it is wrapped in a component that enables basic functionality like selection and dragging. Your custom node receives the following props:
export type NodeProps<NodeType extends Node = Node> = {
id: string;
data: Node['data'];
dragHandle?: boolean;
type?: string;
selected?: boolean;
isConnectable?: boolean;
zIndex?: number;
positionAbsoluteX: number;
positionAbsoluteY: number;
dragging: boolean;
targetPosition?: Position;
sourcePosition?: Position;
};
用法
¥Usage
import { useState } from 'react';
import { NodeProps, Node } from '@xyflow/react';
export type CounterNode = Node<
{
initialCount?: number;
},
'counter'
>;
export default function CounterNode(props: NodeProps<CounterNode>) {
const [count, setCount] = useState(props.data?.initialCount ?? 0);
return (
<div>
<p>Count: {count}</p>
<button className="nodrag" onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}
请记住通过将自定义节点添加到 <ReactFlow />
组件的 nodeTypes
prop 来注册它。
¥Remember to register your custom node by adding it to the
nodeTypes
prop of your
<ReactFlow />
component.
import { ReactFlow } from '@xyflow/react';
import CounterNode from './CounterNode';
const nodeTypes = {
counterNode: CounterNode,
};
export default function App() {
return <ReactFlow nodeTypes={nodeTypes} ... />
}
你可以在我们的 自定义节点指南 中阅读更多内容。
¥You can read more in our custom node guide.
字段
¥Fields
Name | Type | Default |
---|---|---|
id | NodeType["id"] Unique id of a node. | |
data | NodeType["data"] Arbitrary data passed to a node. | |
width | NodeType["width"] | |
height | NodeType["height"] | |
sourcePosition | NodeType["sourcePosition"] Only relevant for default, source, target nodeType. Controls source position. | |
targetPosition | NodeType["targetPosition"] Only relevant for default, source, target nodeType. Controls target position. | |
dragHandle | NodeType["dragHandle"] A class name that can be applied to elements inside the node that allows those elements to act as drag handles, letting the user drag the node by clicking and dragging on those elements. | |
parentId | NodeType["parentId"] Parent node id, used for creating sub-flows. | |
type | NodeType["type"] Type of node defined in | |
dragging | NodeType["dragging"] Whether or not the node is currently being dragged. | |
zIndex | NodeType["zIndex"] | |
selectable | NodeType["selectable"] | |
deletable | NodeType["deletable"] | |
selected | NodeType["selected"] | |
draggable | NodeType["draggable"] Whether or not the node is able to be dragged. | |
isConnectable | boolean Whether a node is connectable or not. | |
positionAbsoluteX | number Position absolute x value. | |
positionAbsoluteY | number Position absolute y value. |