@arco.design upload 已上传文件,点击删除 popconfirm 询问删除

在这里插入图片描述

实现

借助 upload 的 两个属性 on-before-removecustom-icon

custom-icon

官方给的例子是更换图标,这里借助 h 函数返回的 vnode

const getCustomIcon = () => {
  return {
    retryIcon: () => h(IconUpload),
    cancelIcon: () => h(IconClose),
    fileIcon: () => h(IconFileAudio),
    removeIcon: () => h(IconClose),
    errorIcon: () => h(IconFaceFrownFill),
    fileName: (file) => {
      return `文件名: ${file.name}`
    },
  };
};

在这里插入图片描述
既然是 vnode,那就好办了

<a-upload
  v-model:file-list="uploadValue"
  ...
  :custom-icon="getCustomIcon()"
>
  <template #upload-button>
      <a-button type="primary"><icon-upload />点击上传</a-button>
  </template>
</a-upload>
import { h } from 'vue'
import { Popconfirm } from '@arco-design/web-vue'
import { IconDelete } from '@arco-design/web-vue/es/icon'

const getCustomIcon = () => {
  return {
    removeIcon: () =>
     h(
       Popconfirm,
       {
         content: '是否要删除附件?',
         type: 'warning',
         onOk: () => {
         	console.log('确认删除')
         },
       },
       {
         default: () => h(IconDelete),
       }
     ),
  }
}

h 函数参数

function h(
  type: string | Component,
  props?: object | null,
  children?: Children | Slot | Slots
): VNode

试验下,发现此时我们点击删除按钮的时候还是会直接删除文件

原因是由于另一个属性 on-before-remove,需要我们做如下处理
<a-upload
  v-model:file-list="uploadValue"
  ...
  :custom-icon="getCustomIcon()"
  @before-remove="beforeRemove"
>
  <template #upload-button>
      <a-button type="primary"><icon-upload />点击上传</a-button>
  </template>
</a-upload>
// 缓存要删除的文件
const cacheRemoveFile = ref()
const beforeRemove = (file) => {
  cacheRemoveFile.value = file
  return false
}

这里要缓存一下要删除的文件, Popconfirm onOk 的时候需要

完整代码:

<a-upload
  v-model:file-list="uploadValue"
  ...
  :custom-icon="getCustomIcon()"
  @before-remove="beforeRemove"
>
  ...
</a-upload>
 
<script setup>
import { ref } from 'vue'
import { Popconfirm } from '@arco-design/web-vue'
import { IconDelete } from '@arco-design/web-vue/es/icon'
const getCustomIcon = () => {
  return {
    removeIcon: () =>
      h(
        Popconfirm,
        {
          content: '是否要删除附件?',
          type: 'warning',
          onOk: () => {
            let delFile = cacheRemoveFile.value
            let delIndex = uploadValue.value.findIndex((item) => item.uid == delFile.uid)
            if (delIndex >= 0) {
               // 真正删除文件
			   uploadValue.value.splice(index, 1)
            }
          },
        },
        {
          default: () => h(IconDelete),
        }
      ),
  }
}
// 缓存的待删除文件
const cacheRemoveFile = ref()
const beforeRemove = (file) => {
  cacheRemoveFile.value = file
  return false
}
</script>

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 19:28:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 19:28:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 19:28:03       87 阅读
  4. Python语言-面向对象

    2024-06-14 19:28:03       96 阅读

热门阅读

  1. C++学习(20)

    2024-06-14 19:28:03       28 阅读
  2. 第壹章第15节 C#和TS语言对比-泛型

    2024-06-14 19:28:03       23 阅读
  3. C++的算法:Kosaraju算法与Tarjan算法

    2024-06-14 19:28:03       29 阅读
  4. 模拟面试题卷一

    2024-06-14 19:28:03       34 阅读