useConfirmDelete.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { h } from 'vue'
  2. import { NIcon, useDialog } from 'naive-ui'
  3. import { AlertCircleOutline } from '@vicons/ionicons5'
  4. /**
  5. * 统一的删除确认弹窗:居中、蓝色调、风格一致
  6. *
  7. * 用法:
  8. * const confirmDelete = useConfirmDelete()
  9. * confirmDelete({
  10. * name: skill.name,
  11. * itemType: '技能',
  12. * onConfirm: async () => { await api.delete(id) }
  13. * })
  14. *
  15. * 选项:
  16. * - name: 被删项名称(用于展示)
  17. * - itemType: 类型标签,如 "技能"/"模型"/"智能体"/"分类",默认空
  18. * - content: 自定义正文(覆盖默认模板)
  19. * - onConfirm: 点击"确认删除"后的回调(async)
  20. */
  21. export function useConfirmDelete() {
  22. const dialog = useDialog()
  23. return function confirmDelete(options = {}) {
  24. const {
  25. name = '',
  26. itemType = '',
  27. content,
  28. onConfirm,
  29. } = options
  30. const subject = itemType ? `${itemType}「${name}」` : `「${name}」`
  31. const body = content || `确定要删除${subject}吗?此操作不可恢复。`
  32. dialog.create({
  33. title: '确认删除',
  34. content: body,
  35. maskClosable: false,
  36. positiveText: '确认删除',
  37. negativeText: '取消',
  38. icon() {
  39. return h(NIcon, { color: 'var(--color-primary)', size: 24 }, {
  40. default: () => h(AlertCircleOutline),
  41. })
  42. },
  43. actionStyle: {
  44. // 让按钮区域有一点上边距,整体更紧凑
  45. paddingTop: '8px',
  46. },
  47. onPositiveClick: async () => {
  48. if (typeof onConfirm === 'function') {
  49. await onConfirm()
  50. }
  51. },
  52. })
  53. }
  54. }