examples: add confirm delete example (#1466)
* examples: add confirm delete example * docs: add confirm delete example
This commit is contained in:
@@ -134,6 +134,10 @@ export const routes: RouterOptions['routes'] = [
|
||||
path: '/screenshot',
|
||||
component: () => import('./src/Screenshot/ScreenshotExample.vue'),
|
||||
},
|
||||
{
|
||||
path: '/confirm-delete',
|
||||
component: () => import('./src/ConfirmDelete/ConfirmDeleteExample.vue'),
|
||||
},
|
||||
]
|
||||
|
||||
export const router = createRouter({
|
||||
|
||||
69
examples/vite/src/ConfirmDelete/ConfirmDeleteExample.vue
Normal file
69
examples/vite/src/ConfirmDelete/ConfirmDeleteExample.vue
Normal file
@@ -0,0 +1,69 @@
|
||||
<script lang="ts" setup>
|
||||
import type { Edge, EdgeChange, Node, NodeChange } from '@vue-flow/core'
|
||||
import { VueFlow, useVueFlow } from '@vue-flow/core'
|
||||
import { Background } from '@vue-flow/background'
|
||||
import { useDialog } from './useDialog'
|
||||
import Dialog from './Dialog.vue'
|
||||
|
||||
const { onConnect, addEdges, onNodesChange, onEdgesChange, applyNodeChanges, applyEdgeChanges } = useVueFlow()
|
||||
|
||||
const dialog = useDialog({ message: 'Do you really want to delete this item?' })
|
||||
|
||||
const nodes = ref<Node[]>([
|
||||
{ id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 }, class: 'light' },
|
||||
{ id: '2', label: 'Node 2', position: { x: 100, y: 100 }, class: 'light' },
|
||||
{ id: '3', label: 'Node 3', position: { x: 400, y: 100 }, class: 'light' },
|
||||
{ id: '4', label: 'Node 4', position: { x: 400, y: 200 }, class: 'light' },
|
||||
])
|
||||
|
||||
const edges = ref<Edge[]>([
|
||||
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||
{ id: 'e1-3', source: '1', target: '3' },
|
||||
])
|
||||
|
||||
onConnect(addEdges)
|
||||
|
||||
onNodesChange(async (changes) => {
|
||||
const nextChanges: NodeChange[] = []
|
||||
|
||||
for (const change of changes) {
|
||||
if (change.type === 'remove') {
|
||||
const isConfirmed = await dialog.confirm()
|
||||
|
||||
if (isConfirmed) {
|
||||
nextChanges.push(change)
|
||||
}
|
||||
} else {
|
||||
nextChanges.push(change)
|
||||
}
|
||||
}
|
||||
|
||||
applyNodeChanges(nextChanges)
|
||||
})
|
||||
|
||||
onEdgesChange(async (changes) => {
|
||||
const nextChanges: EdgeChange[] = []
|
||||
|
||||
for (const change of changes) {
|
||||
if (change.type === 'remove') {
|
||||
const isConfirmed = await dialog.confirm()
|
||||
|
||||
if (isConfirmed) {
|
||||
nextChanges.push(change)
|
||||
}
|
||||
} else {
|
||||
nextChanges.push(change)
|
||||
}
|
||||
}
|
||||
|
||||
applyEdgeChanges(nextChanges)
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<VueFlow :nodes="nodes" :edges="edges" :apply-default="false" fit-view-on-init class="vue-flow-basic-example">
|
||||
<Background />
|
||||
|
||||
<Dialog />
|
||||
</VueFlow>
|
||||
</template>
|
||||
57
examples/vite/src/ConfirmDelete/Dialog.vue
Normal file
57
examples/vite/src/ConfirmDelete/Dialog.vue
Normal file
@@ -0,0 +1,57 @@
|
||||
<script setup lang="ts">
|
||||
import { useDialogState } from './useDialog'
|
||||
|
||||
const { isVisible, message, resolve } = useDialogState()
|
||||
|
||||
function confirm() {
|
||||
resolve(true)
|
||||
isVisible.value = false
|
||||
}
|
||||
|
||||
function cancel() {
|
||||
resolve(false)
|
||||
isVisible.value = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="isVisible" class="dialog-overlay">
|
||||
<div class="dialog">
|
||||
<p>{{ message }}</p>
|
||||
|
||||
<div class="dialog-actions">
|
||||
<button @click="confirm">Confirm</button>
|
||||
<button @click="cancel">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
.dialog-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.dialog {
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 5px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.dialog-actions {
|
||||
margin-top: 20px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
}
|
||||
</style>
|
||||
30
examples/vite/src/ConfirmDelete/useDialog.ts
Normal file
30
examples/vite/src/ConfirmDelete/useDialog.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
const isVisible = ref(false)
|
||||
const message = ref('')
|
||||
let resolveCallback: (value: boolean) => void
|
||||
|
||||
export function useDialogState() {
|
||||
return {
|
||||
isVisible,
|
||||
message,
|
||||
resolve: (value: boolean) => {
|
||||
if (resolveCallback) {
|
||||
resolveCallback(value)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function useDialog(opts: { message?: string }) {
|
||||
message.value = opts.message || 'Are you sure?'
|
||||
|
||||
return {
|
||||
confirm(): Promise<boolean> {
|
||||
isVisible.value = true
|
||||
return new Promise<boolean>((resolve) => {
|
||||
resolveCallback = resolve
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,11 @@ export default {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Handle type="target" :position="Position.Left" :style="targetHandleStyle" />
|
||||
<Handle type="target" :position="Position.Left" :style="targetHandleStyle">
|
||||
<svg viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" :width="10" :height="10">
|
||||
<circle cx="8" cy="8" r="7" fill="hotpink" />
|
||||
</svg>
|
||||
</Handle>
|
||||
<div>
|
||||
Custom Color Picker Node: <strong>{{ data.color }}</strong>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user