Skip to content

Commit e678f6f

Browse files
committed
chore(examples): update custom node example
Signed-off-by: braks <[email protected]>
1 parent 1897094 commit e678f6f

File tree

2 files changed

+58
-60
lines changed

2 files changed

+58
-60
lines changed

examples/vite/src/CustomNode/ColorSelectorNode.vue

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<script lang="ts" setup>
22
import type { CSSProperties } from 'vue'
3-
import type { Connection, Edge, NodeProps } from '@vue-flow/core'
3+
import type { NodeProps } from '@vue-flow/core'
44
import { Handle, Position } from '@vue-flow/core'
55
6-
interface ColorSelectorNodeProps extends NodeProps {
7-
data: {
8-
color: string
9-
onChange: (event: any) => void
10-
}
6+
interface Data {
7+
color: string
8+
onChange: (event: InputEvent) => void
9+
}
10+
11+
interface ColorSelectorNodeProps extends NodeProps<Data, {}, 'selectorNode'> {
12+
data: Data
1113
}
1214
const props = defineProps<ColorSelectorNodeProps>()
1315
1416
const targetHandleStyle: CSSProperties = { background: '#555' }
1517
const sourceHandleStyleA: CSSProperties = { ...targetHandleStyle, top: '10px' }
1618
const sourceHandleStyleB: CSSProperties = { ...targetHandleStyle, bottom: '10px', top: 'auto' }
17-
18-
const onConnect = (params: Connection | Edge) => console.log('handle onConnect', params)
1919
</script>
2020

2121
<script lang="ts">
@@ -25,7 +25,7 @@ export default {
2525
</script>
2626

2727
<template>
28-
<Handle type="target" :position="Position.Left" :style="targetHandleStyle" :on-connect="onConnect" />
28+
<Handle type="target" :position="Position.Left" :style="targetHandleStyle" />
2929
<div>
3030
Custom Color Picker Node: <strong>{{ data.color }}</strong>
3131
</div>

examples/vite/src/CustomNode/CustomNode.vue

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,78 @@ import { MiniMap } from '@vue-flow/minimap'
77
88
import ColorSelectorNode from './ColorSelectorNode.vue'
99
10-
const elements = ref<Elements>([])
1110
const bgColor = ref('#1A192B')
1211
const connectionLineStyle = { stroke: '#fff' }
1312
const snapGrid: SnapGrid = [16, 16]
1413
15-
const nodeStroke = (n: Node): string => {
14+
const elements = ref<Elements>([
15+
{
16+
id: '1',
17+
type: 'input',
18+
label: 'An input node',
19+
position: { x: 0, y: 50 },
20+
sourcePosition: Position.Right,
21+
},
22+
{
23+
id: '2',
24+
type: 'selectorNode',
25+
data: { onChange, color: bgColor },
26+
style: { border: '1px solid #777', padding: '10px' },
27+
parentNode: '1',
28+
position: { x: 250, y: 50 },
29+
},
30+
{
31+
id: '3',
32+
type: 'output',
33+
label: 'Output A',
34+
position: { x: 650, y: 25 },
35+
targetPosition: Position.Left,
36+
},
37+
{
38+
id: '4',
39+
type: 'output',
40+
label: 'Output B',
41+
position: { x: 650, y: 100 },
42+
targetPosition: Position.Left,
43+
},
44+
45+
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } },
46+
{ id: 'e2a-3', source: '2', sourceHandle: 'a', target: '3', animated: true, style: { stroke: '#fff' } },
47+
{ id: 'e2b-4', source: '2', sourceHandle: 'b', target: '4', animated: true, style: { stroke: '#fff' } },
48+
])
49+
50+
useVueFlow({
51+
connectionMode: ConnectionMode.Loose,
52+
connectionLineOptions: {
53+
style: connectionLineStyle,
54+
},
55+
snapToGrid: true,
56+
snapGrid,
57+
})
58+
59+
function nodeStroke(n: Node) {
1660
if (n.type === 'input') return '#0041d0'
1761
if (n.type === 'selectorNode') return bgColor.value
1862
if (n.type === 'output') return '#ff0072'
1963
return '#eee'
2064
}
21-
const nodeColor = (n: Node): string => {
65+
function nodeColor(n: Node) {
2266
if (n.type === 'selectorNode') return bgColor.value
2367
return '#fff'
2468
}
2569
26-
const onChange = (event: Event) => {
70+
function onChange(event: InputEvent) {
2771
elements.value.forEach((e) => {
2872
if (isEdge(e) || e.id !== '2') return e
2973
bgColor.value = (event.target as HTMLInputElement).value
3074
})
3175
}
32-
33-
onMounted(() => {
34-
elements.value = [
35-
{
36-
id: '1',
37-
type: 'input',
38-
label: 'An input node',
39-
position: { x: 0, y: 50 },
40-
sourcePosition: Position.Right,
41-
},
42-
{
43-
id: '2',
44-
type: 'selectorNode',
45-
data: { onChange, color: bgColor },
46-
style: { border: '1px solid #777', padding: '10px' },
47-
position: { x: 250, y: 50 },
48-
},
49-
{
50-
id: '3',
51-
type: 'output',
52-
label: 'Output A',
53-
position: { x: 650, y: 25 },
54-
targetPosition: Position.Left,
55-
},
56-
{
57-
id: '4',
58-
type: 'output',
59-
label: 'Output B',
60-
position: { x: 650, y: 100 },
61-
targetPosition: Position.Left,
62-
},
63-
64-
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } },
65-
{ id: 'e2a-3', source: '2', sourceHandle: 'a', target: '3', animated: true, style: { stroke: '#fff' } },
66-
{ id: 'e2b-4', source: '2', sourceHandle: 'b', target: '4', animated: true, style: { stroke: '#fff' } },
67-
]
68-
})
69-
70-
useVueFlow({
71-
connectionMode: ConnectionMode.Loose,
72-
connectionLineOptions: {
73-
style: connectionLineStyle,
74-
},
75-
snapToGrid: true,
76-
snapGrid,
77-
})
7876
</script>
7977

8078
<template>
8179
<VueFlow v-model="elements" fit-view-on-init :style="{ backgroundColor: bgColor }">
8280
<template #node-selectorNode="props">
83-
<ColorSelectorNode v-bind="props" />
81+
<ColorSelectorNode :data="props.data" :parent-node="props.parentNode" :position="props.position" />
8482
</template>
8583
<MiniMap :node-stroke-color="nodeStroke" :node-color="nodeColor" />
8684
<Controls />

0 commit comments

Comments
 (0)