chore(useNodesData): cleanup return type
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, useHandleConnections, useNodesData } from '@xyflow/react';
|
||||
import { TextNode, isTextNode, type MyNode } from '.';
|
||||
import { isTextNode, type MyNode } from '.';
|
||||
|
||||
function ResultNode() {
|
||||
const connections = useHandleConnections({
|
||||
type: 'target',
|
||||
});
|
||||
const nodesData = useNodesData<MyNode>(connections.map((connection) => connection.source));
|
||||
const textNodes = nodesData.filter(isTextNode) as TextNode[];
|
||||
|
||||
textNodes[0].data;
|
||||
const textNodes = nodesData.filter(isTextNode);
|
||||
|
||||
return (
|
||||
<div style={{ background: '#eee', color: '#222', padding: 10, fontSize: 12, borderRadius: 10 }}>
|
||||
|
||||
@@ -20,8 +20,8 @@ export type ResultNode = Node<{}, 'result'>;
|
||||
export type UppercaseNode = Node<{ text: string }, 'uppercase'>;
|
||||
export type MyNode = TextNode | ResultNode | UppercaseNode;
|
||||
|
||||
export function isTextNode(node: any): node is TextNode | UppercaseNode {
|
||||
return node.type === 'text' || node.type === 'uppercase';
|
||||
export function isTextNode(node: any): node is TextNode {
|
||||
return node.type === 'text';
|
||||
}
|
||||
|
||||
const nodeTypes = {
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
<script context="module" lang="ts">
|
||||
import TextNode from './TextNode.svelte';
|
||||
import UppercaseNode from './UppercaseNode.svelte';
|
||||
import ResultNode from './ResultNode.svelte';
|
||||
import type { Node } from '@xyflow/svelte';
|
||||
|
||||
export function isTextNode(node: any): node is TextNode | UppercaseNode {
|
||||
return node.type === 'text' || node.type === 'uppercase';
|
||||
type TextNodeType = Node<{ text: string }, 'text'>;
|
||||
type UppercaseNodeType = Node<{ text: string }, 'uppercase'>;
|
||||
type ResultNodeType = Node<{}, 'result'>;
|
||||
|
||||
export function isTextNode(node: any): node is TextNodeType {
|
||||
return node.type === 'text';
|
||||
}
|
||||
|
||||
export type MyNode = TextNode | UppercaseNode | ResultNode;
|
||||
export type MyNode = TextNodeType | UppercaseNodeType | ResultNodeType;
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -18,19 +20,22 @@
|
||||
Background,
|
||||
BackgroundVariant,
|
||||
MiniMap,
|
||||
type Node,
|
||||
type NodeTypes,
|
||||
type Edge
|
||||
} from '@xyflow/svelte';
|
||||
import '@xyflow/svelte/dist/style.css';
|
||||
|
||||
import TextNode from './TextNode.svelte';
|
||||
import UppercaseNode from './UppercaseNode.svelte';
|
||||
import ResultNode from './ResultNode.svelte';
|
||||
|
||||
const nodeTypes: NodeTypes = {
|
||||
text: TextNode,
|
||||
uppercase: UppercaseNode,
|
||||
result: ResultNode
|
||||
};
|
||||
|
||||
const nodes = writable<Node[]>([
|
||||
const nodes = writable<MyNode[]>([
|
||||
{
|
||||
id: '1',
|
||||
type: 'text',
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
useNodesData,
|
||||
type NodeProps
|
||||
} from '@xyflow/svelte';
|
||||
import { isTextNode } from './+page.svelte';
|
||||
import { isTextNode, type MyNode } from './+page.svelte';
|
||||
|
||||
type $$Props = NodeProps;
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
type: 'target'
|
||||
});
|
||||
|
||||
$: nodeData = useNodesData($connections.map((connection) => connection.source));
|
||||
$: nodeData = useNodesData<MyNode>($connections.map((connection) => connection.source));
|
||||
$: textNodes = $nodeData.filter(isTextNode);
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user