Skip to content

Commit c7049e3

Browse files
committed
docs: update controlled flow page
1 parent ff05551 commit c7049e3

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

docs/src/guide/controlled-flow.md

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ This guide will show you how to take control of Vue Flow and apply these changes
1717

1818
## The `applyChanges` option
1919

20-
The `applyChanges` option is a flag that can be passed to the `VueFlow` component to enable or disable automatic change handling.
20+
The `applyChanges` option is a prop that can be passed to the `<VueFlow>` component to enable or disable automatic change handling.
2121

22-
By setting this option to `false`, we tell Vue Flow to not apply changes automatically anymore.
22+
By setting this option to `false`, we tell Vue Flow to not apply changes automatically anymore,
23+
that way we can take control of changes and apply them manually.
2324

2425
```vue
2526
<template>
26-
<VueFlow v-model="elements" :apply-changes="false" />
27+
<VueFlow :nodes="nodes" :edges="edges" :apply-changes="false" />
2728
</template>
2829
```
2930

@@ -34,14 +35,28 @@ These events are emitted regardless of the `applyChanges` option, so you can use
3435

3536
```vue
3637
<script setup>
38+
import { VueFlow, useVueFlow } from '@vue-flow/core'
39+
40+
const { onNodesChange, onEdgesChange } = useVueFlow()
41+
42+
onNodesChange((changes) => {
43+
// changes are arrays of type `NodeChange`
44+
console.log(changes)
45+
})
46+
47+
onEdgesChange((changes) => {
48+
// changes are arrays of type `EdgeChange`
49+
console.log(changes)
50+
})
51+
3752
const onChange = (changes) => {
3853
// changes are arrays of type `NodeChange` or `EdgeChange`
39-
console.log(changes) // will log the changes
54+
console.log(changes)
4055
}
4156
</script>
4257
4358
<template>
44-
<VueFlow v-model="elements" @nodes-change="onChange" @edges-change="onChange" />
59+
<VueFlow :nodes="nodes" :edges="edges" @nodes-change="onChange" @edges-change="onChange" />
4560
</template>
4661
```
4762

@@ -72,52 +87,60 @@ In this example, we will first disable automatic change handlers with `applyChan
7287
then use the `onNodesChange` event to listen to changes and validate delete changes and,
7388
if they are valid, use `applyNodeChanges` to apply them.
7489

90+
::: info
91+
Checkout the [confirm delete example](/examples/confirm).
92+
:::
93+
7594
```vue
7695
<script setup>
77-
import { useVueFlow, VueFlow } from '@vue-flow/core'
7896
import { ref } from 'vue'
97+
import { useVueFlow, VueFlow } from '@vue-flow/core'
7998
8099
const { applyNodeChanges } = useVueFlow();
81100
82101
const { confirm } = useConfirm();
83102
84-
const elements = ref([
103+
const nodes = ref([
85104
{
86105
id: '1',
87-
label: 'Node 1',
88106
position: { x: 0, y: 0 },
107+
data: { label: 'Node 1' },
89108
},
90109
{
91110
id: '2',
92-
label: 'Node 2',
93111
position: { x: 100, y: 100 },
112+
data: { label: 'Node 2' },
94113
},
114+
])
115+
116+
const edges = ref([
95117
{
96-
id: 'e1-2',
118+
id: 'e1->2',
97119
source: '1',
98120
target: '2',
99121
},
100122
])
101123
102-
const onNodesChange = (changes) => {
103-
changes.forEach(async (change) => {
104-
// if the change is a remove change, we want to validate it first
105-
if (change.type === 'remove') {
106-
const isConfirmed = await confirm();
107-
108-
if (isConfirmed) {
109-
// if confirmed, apply the change
110-
applyNodeChanges([change])
111-
}
112-
} else {
113-
// apply all other changes
114-
applyNodeChanges([change])
115-
}
116-
})
124+
const onNodesChange = async (changes) => {
125+
const nextChanges = []
126+
127+
for (const change of changes) {
128+
if (change.type === 'remove') {
129+
const isConfirmed = await confirm('Are you sure you want to delete this node?')
130+
131+
if (isConfirmed) {
132+
nextChanges.push(change)
133+
}
134+
} else {
135+
nextChanges.push(change)
136+
}
137+
}
138+
139+
applyNodeChanges(nextChanges)
117140
}
118141
</script>
119142
120143
<template>
121-
<VueFlow v-model="elements" :apply-changes="false" @nodes-change="onNodesChange" />
144+
<VueFlow :nodes="nodes" :edges="edges" :apply-changes="false" @nodes-change="onNodesChange" />
122145
</template>
123146
```

0 commit comments

Comments
 (0)