chore(docs): update validation example

This commit is contained in:
braks
2024-06-17 18:52:11 +02:00
parent 889797e684
commit 55a76cd9ad
4 changed files with 39 additions and 23 deletions
+14 -10
View File
@@ -9,38 +9,42 @@ const { addEdges } = useVueFlow()
const nodes = ref([ const nodes = ref([
{ {
id: '0', id: '0',
type: 'custominput', type: 'input',
position: { x: 0, y: 150 }, position: { x: 0, y: 150 },
isValidTargetPos: (connection) => connection.target === 'B', // only target `B` is valid for this node
data: { validTarget: 'B', validSource: '0' },
}, },
{ {
id: 'A', id: 'A',
type: 'custom', type: 'custom',
position: { x: 250, y: 0 }, position: { x: 250, y: 0 },
isValidSourcePos: () => false, // no valid connections can be made for this node
data: {},
}, },
{ {
id: 'B', id: 'B',
type: 'custom', type: 'custom',
position: { x: 250, y: 150 }, position: { x: 250, y: 150 },
isValidSourcePos: (connection) => connection.target === 'B', // only source `0` is valid for this node
data: { validTarget: 'B', validSource: '0' },
}, },
{ {
id: 'C', id: 'C',
type: 'custom', type: 'custom',
position: { x: 250, y: 300 }, position: { x: 250, y: 300 },
isValidSourcePos: (connection) => connection.target === 'B', // no valid connections can be made for this node
data: {},
}, },
]) ])
const edges = ref([]) const edges = ref([])
function onConnectStart({ nodeId, handleType }) { function onConnectStart({ nodeId, handleType }) {
return console.log('on connect start', { nodeId, handleType }) console.log('on connect start', { nodeId, handleType })
} }
function onConnectEnd(event) { function onConnectEnd(event) {
return console.log('on connect end', event) console.log('on connect end', event)
} }
function onConnect(params) { function onConnect(params) {
@@ -59,12 +63,12 @@ function onConnect(params) {
@connect-start="onConnectStart" @connect-start="onConnectStart"
@connect-end="onConnectEnd" @connect-end="onConnectEnd"
> >
<template #node-custominput="props"> <template #node-input="props">
<CustomInput v-bind="props" /> <CustomInput :data="props.data" />
</template> </template>
<template #node-custom="props"> <template #node-custom="props">
<CustomNode v-bind="props" /> <CustomNode :id="props.id" :data="props.data" />
</template> </template>
</VueFlow> </VueFlow>
</template> </template>
+9 -5
View File
@@ -2,11 +2,15 @@
import { Handle, Position } from '@vue-flow/core' import { Handle, Position } from '@vue-flow/core'
const props = defineProps({ const props = defineProps({
isValidTargetPos: { data: {
type: Function, type: Object,
required: false, required: true,
}, },
}) })
function isValidConnection(connection) {
return connection.target === props.data.validTarget && connection.source === props.data.validSource
}
</script> </script>
<script> <script>
@@ -16,6 +20,6 @@ export default {
</script> </script>
<template> <template>
<div>Only connectable with B</div> <div>Only connectable with {{ data.validTarget }}</div>
<Handle type="source" :position="Position.Right" :is-valid-connection="props.isValidTargetPos" /> <Handle type="source" :position="Position.Right" :is-valid-connection="isValidConnection" />
</template> </template>
+8 -4
View File
@@ -6,11 +6,15 @@ const props = defineProps({
type: String, type: String,
required: true, required: true,
}, },
isValidSourcePos: { data: {
type: Function, type: Object,
required: false, required: true,
}, },
}) })
function isValidConnection(connection) {
return connection.target === props.data.validTarget && connection.source === props.data.validSource
}
</script> </script>
<script> <script>
@@ -20,6 +24,6 @@ export default {
</script> </script>
<template> <template>
<Handle type="target" :position="Position.Left" :is-valid-connection="props.isValidSourcePos" /> <Handle type="target" :position="Position.Left" :is-valid-connection="isValidConnection" />
<div>{{ props.id }}</div> <div>{{ props.id }}</div>
</template> </template>
+8 -4
View File
@@ -9,10 +9,14 @@ This function will be applied to *all* connections in the flow (even existing on
## Using a handle in a custom node ## Using a handle in a custom node
```vue ```vue
<div> <script setup>
<!-- ... --> import { Handle, Position } from '@vue-flow/core'
<Handle type="source" :is-valid-connection="isValidConnection"/> </script>
</div>
<template>
<Handle type="source" :position="Position.Right" />
<Handle type="target" :position="Position.Left" />
</template>
``` ```
<div class="mt-6"> <div class="mt-6">