From 9d573068ab5e433efaa954783645884beb8c7d87 Mon Sep 17 00:00:00 2001 From: Rairn <958414905@qq.com> Date: Tue, 27 Jun 2023 14:10:07 +0800 Subject: [PATCH] fix: fix the upload status not being updated if the upload was successful --- .../schema-component/antd/upload/Upload.tsx | 32 ++++++++++++++++--- .../schema-component/antd/upload/shared.ts | 5 ++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/upload/Upload.tsx b/packages/core/client/src/schema-component/antd/upload/Upload.tsx index e3e4e5128cd8c..cb58a5c3d8cf2 100644 --- a/packages/core/client/src/schema-component/antd/upload/Upload.tsx +++ b/packages/core/client/src/schema-component/antd/upload/Upload.tsx @@ -1,10 +1,10 @@ import { DeleteOutlined, DownloadOutlined, InboxOutlined, LoadingOutlined, PlusOutlined } from '@ant-design/icons'; import { usePrefixCls } from '@formily/antd/esm/__builtins__'; import { connect, mapProps, mapReadPretty } from '@formily/react'; -import { Upload as AntdUpload, Button, Progress, Space } from 'antd'; +import { Upload as AntdUpload, Button, Progress, Space, UploadFile } from 'antd'; import cls from 'classnames'; import { saveAs } from 'file-saver'; -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import Lightbox from 'react-image-lightbox'; import 'react-image-lightbox/style.css'; // This only needs to be imported once in your app @@ -31,9 +31,12 @@ Upload.Attachment = connect((props: UploadProps) => { const [photoIndex, setPhotoIndex] = useState(0); const [visible, setVisible] = useState(false); const { t } = useTranslation(); + const internalFileList = useRef([]); useEffect(() => { if (sync) { - setFileList(toFileList(value)); + const fileList = toFileList(value); + setFileList(fileList); + internalFileList.current = fileList; } }, [value, sync]); const uploadProps = useUploadProps({ ...props }); @@ -105,6 +108,9 @@ Upload.Attachment = connect((props: UploadProps) => { } const index = prevFileList.indexOf(file); prevFileList.splice(index, 1); + internalFileList.current = internalFileList.current.filter( + (item) => item.uid !== file.uid, + ); onChange(toValue([...prevFileList])); return [...prevFileList]; }); @@ -131,12 +137,17 @@ Upload.Attachment = connect((props: UploadProps) => { listType={'picture-card'} fileList={fileList} onChange={(info) => { + // info.fileList 有 BUG,会导致上传状态一直是 uploading + // 所以这里仿照 antd 源码,自己维护一个 fileList + const list = updateFileList(info.file, internalFileList.current); + internalFileList.current = list; + setSync(false); if (multiple) { if (info.file.status === 'done') { - onChange(toValue(info.fileList)); + onChange(toValue(list)); } - setFileList(info.fileList.map(toItem)); + setFileList(list.map(toItem)); } else { if (info.file.status === 'done') { // TODO(BUG): object 的联动有问题,不响应,折中的办法先置空再赋值 @@ -247,3 +258,14 @@ Upload.DraggerV2 = connect( ); export default Upload; + +export function updateFileList(file: UploadFile, fileList: (UploadFile | Readonly)[]) { + const nextFileList = [...fileList]; + const fileIndex = nextFileList.findIndex(({ uid }) => uid === file.uid); + if (fileIndex === -1) { + nextFileList.push(file); + } else { + nextFileList[fileIndex] = file; + } + return nextFileList; +} diff --git a/packages/core/client/src/schema-component/antd/upload/shared.ts b/packages/core/client/src/schema-component/antd/upload/shared.ts index 936bc56b1c26f..242040f037a6b 100644 --- a/packages/core/client/src/schema-component/antd/upload/shared.ts +++ b/packages/core/client/src/schema-component/antd/upload/shared.ts @@ -208,7 +208,10 @@ export function useUploadProps({ serviceEr export const toItem = (file) => { if (file?.response?.data) { - file = file.response.data; + file = { + uid: file.uid, + ...file.response.data, + }; } return { ...file,