61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { type Ref, type RefAttributes, forwardRef, JSX } from 'react';
|
|
import { isNodeBase, isEdgeBase } from '@xyflow/system';
|
|
|
|
import type { Edge, Node } from '../types';
|
|
|
|
/**
|
|
* Test whether an object is usable as an [`Node`](/api-reference/types/node).
|
|
* In TypeScript this is a type guard that will narrow the type of whatever you pass in to
|
|
* [`Node`](/api-reference/types/node) if it returns `true`.
|
|
*
|
|
* @public
|
|
* @remarks In TypeScript this is a type guard that will narrow the type of whatever you pass in to Node if it returns true
|
|
* @param element - The element to test.
|
|
* @returns Tests whether the provided value can be used as a `Node`. If you're using TypeScript,
|
|
* this function acts as a type guard and will narrow the type of the value to `Node` if it returns
|
|
* `true`.
|
|
*
|
|
* @example
|
|
* ```js
|
|
*import { isNode } from '@xyflow/react';
|
|
*
|
|
*if (isNode(node)) {
|
|
* // ...
|
|
*}
|
|
*```
|
|
*/
|
|
export const isNode = <NodeType extends Node = Node>(element: unknown): element is NodeType =>
|
|
isNodeBase<NodeType>(element);
|
|
|
|
/**
|
|
* Test whether an object is usable as an [`Edge`](/api-reference/types/edge).
|
|
* In TypeScript this is a type guard that will narrow the type of whatever you pass in to
|
|
* [`Edge`](/api-reference/types/edge) if it returns `true`.
|
|
*
|
|
* @public
|
|
* @remarks In TypeScript this is a type guard that will narrow the type of whatever you pass in to Edge if it returns true
|
|
* @param element - The element to test
|
|
* @returns Tests whether the provided value can be used as an `Edge`. If you're using TypeScript,
|
|
* this function acts as a type guard and will narrow the type of the value to `Edge` if it returns
|
|
* `true`.
|
|
*
|
|
* @example
|
|
* ```js
|
|
*import { isEdge } from '@xyflow/react';
|
|
*
|
|
*if (isEdge(edge)) {
|
|
* // ...
|
|
*}
|
|
*```
|
|
*/
|
|
export const isEdge = <EdgeType extends Edge = Edge>(element: unknown): element is EdgeType =>
|
|
isEdgeBase<EdgeType>(element);
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
export function fixedForwardRef<T, P = {}>(
|
|
render: (props: P, ref: Ref<T>) => JSX.Element
|
|
): (props: P & RefAttributes<T>) => JSX.Element {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
return forwardRef(render) as any;
|
|
}
|