2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { flushSync } from 'react-dom';
|
2022-06-21 10:24:52 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { UploadProps as RcUploadProps } from 'rc-upload';
|
|
|
|
import RcUpload from 'rc-upload';
|
2021-02-26 15:18:37 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-06-21 10:24:52 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-07-18 14:21:52 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2023-03-21 13:08:43 +08:00
|
|
|
import { useLocale } from '../locale';
|
2023-06-13 11:48:01 +08:00
|
|
|
import defaultLocale from '../locale/en_US';
|
2023-05-04 17:03:04 +08:00
|
|
|
import type {
|
|
|
|
RcFile,
|
|
|
|
ShowUploadListInterface,
|
|
|
|
UploadChangeParam,
|
|
|
|
UploadFile,
|
|
|
|
UploadProps,
|
|
|
|
} from './interface';
|
2022-04-25 18:46:53 +08:00
|
|
|
import useStyle from './style';
|
2023-09-11 17:28:04 +08:00
|
|
|
import UploadList from './UploadList';
|
2023-06-13 11:48:01 +08:00
|
|
|
import { file2Obj, getFileItem, removeFileItem, updateFileList } from './utils';
|
2021-02-26 15:18:37 +08:00
|
|
|
|
2022-04-24 20:55:18 +08:00
|
|
|
export const LIST_IGNORE = `__LIST_IGNORE_${Date.now()}__`;
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2023-05-04 17:03:04 +08:00
|
|
|
export type { UploadProps };
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2023-06-13 11:48:01 +08:00
|
|
|
export interface UploadRef<T = any> {
|
|
|
|
onBatchStart: RcUploadProps['onBatchStart'];
|
|
|
|
onSuccess: (response: any, file: RcFile, xhr: any) => void;
|
|
|
|
onProgress: (e: { percent: number }, file: RcFile) => void;
|
|
|
|
onError: (error: Error, response: any, file: RcFile) => void;
|
|
|
|
fileList: UploadFile<T>[];
|
|
|
|
upload: RcUpload | null;
|
2024-04-01 21:42:17 +08:00
|
|
|
/**
|
|
|
|
* Get native element for wrapping upload
|
|
|
|
* @since 5.17.0
|
|
|
|
*/
|
|
|
|
nativeElement: HTMLSpanElement | null;
|
2023-06-13 11:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const InternalUpload: React.ForwardRefRenderFunction<UploadRef, UploadProps> = (props, ref) => {
|
2020-08-18 15:44:31 +08:00
|
|
|
const {
|
2021-02-26 15:18:37 +08:00
|
|
|
fileList,
|
2020-08-18 15:44:31 +08:00
|
|
|
defaultFileList,
|
|
|
|
onRemove,
|
2022-10-05 00:04:29 +08:00
|
|
|
showUploadList = true,
|
|
|
|
listType = 'text',
|
2020-08-18 15:44:31 +08:00
|
|
|
onPreview,
|
|
|
|
onDownload,
|
2020-12-15 17:09:52 +08:00
|
|
|
onChange,
|
2021-04-30 22:11:31 +08:00
|
|
|
onDrop,
|
2020-08-18 15:44:31 +08:00
|
|
|
previewFile,
|
2022-07-18 14:21:52 +08:00
|
|
|
disabled: customDisabled,
|
2020-08-18 15:44:31 +08:00
|
|
|
locale: propLocale,
|
|
|
|
iconRender,
|
|
|
|
isImageUrl,
|
|
|
|
progress,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2022-10-05 00:04:29 +08:00
|
|
|
type = 'select',
|
2020-08-18 15:44:31 +08:00
|
|
|
children,
|
|
|
|
style,
|
2020-09-17 16:14:42 +08:00
|
|
|
itemRender,
|
2020-12-15 17:09:52 +08:00
|
|
|
maxCount,
|
2022-10-05 00:04:29 +08:00
|
|
|
data = {},
|
|
|
|
multiple = false,
|
2023-12-30 15:21:52 +08:00
|
|
|
hasControlInside = true,
|
2022-10-05 00:04:29 +08:00
|
|
|
action = '',
|
|
|
|
accept = '',
|
|
|
|
supportServerRender = true,
|
2023-08-07 18:04:44 +08:00
|
|
|
rootClassName,
|
2020-08-18 15:44:31 +08:00
|
|
|
} = props;
|
|
|
|
|
2022-07-18 14:21:52 +08:00
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
2022-09-20 16:48:59 +08:00
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
2022-07-18 14:21:52 +08:00
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
const [mergedFileList, setMergedFileList] = useMergedState(defaultFileList || [], {
|
|
|
|
value: fileList,
|
2022-11-19 13:47:33 +08:00
|
|
|
postState: (list) => list ?? [],
|
2021-02-26 15:18:37 +08:00
|
|
|
});
|
2020-08-29 21:28:44 +08:00
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
const [dragState, setDragState] = React.useState<string>('drop');
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2022-11-04 18:36:54 +08:00
|
|
|
const upload = React.useRef<RcUpload>(null);
|
2024-04-01 21:42:17 +08:00
|
|
|
const wrapRef = React.useRef<HTMLSpanElement>(null);
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Upload');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
warning(
|
|
|
|
'fileList' in props || !('value' in props),
|
|
|
|
'usage',
|
|
|
|
'`value` is not a valid prop, do you mean `fileList`?',
|
|
|
|
);
|
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(!('transformFile' in props), 'transformFile', 'beforeUpload');
|
2023-09-11 17:28:04 +08:00
|
|
|
}
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2021-01-13 14:31:49 +08:00
|
|
|
// Control mode will auto fill file uid if not provided
|
2021-04-13 09:49:58 +08:00
|
|
|
React.useMemo(() => {
|
2021-01-13 14:31:49 +08:00
|
|
|
const timestamp = Date.now();
|
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
(fileList || []).forEach((file, index) => {
|
2021-03-30 11:59:30 +08:00
|
|
|
if (!file.uid && !Object.isFrozen(file)) {
|
2021-02-26 15:18:37 +08:00
|
|
|
file.uid = `__AUTO__${timestamp}_${index}__`;
|
|
|
|
}
|
2021-01-13 14:31:49 +08:00
|
|
|
});
|
2021-02-26 15:18:37 +08:00
|
|
|
}, [fileList]);
|
2021-01-13 14:31:49 +08:00
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
const onInternalChange = (
|
|
|
|
file: UploadFile,
|
|
|
|
changedFileList: UploadFile[],
|
|
|
|
event?: { percent: number },
|
|
|
|
) => {
|
|
|
|
let cloneList = [...changedFileList];
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2023-06-16 00:47:55 +08:00
|
|
|
let exceedMaxCount = false;
|
|
|
|
|
2020-12-15 17:09:52 +08:00
|
|
|
// Cut to match count
|
|
|
|
if (maxCount === 1) {
|
|
|
|
cloneList = cloneList.slice(-1);
|
|
|
|
} else if (maxCount) {
|
2023-06-26 19:09:17 +08:00
|
|
|
exceedMaxCount = cloneList.length > maxCount;
|
2020-12-15 17:09:52 +08:00
|
|
|
cloneList = cloneList.slice(0, maxCount);
|
2020-08-18 15:44:31 +08:00
|
|
|
}
|
2020-12-15 17:09:52 +08:00
|
|
|
|
2022-08-09 16:29:48 +08:00
|
|
|
// Prevent React18 auto batch since input[upload] trigger process at same time
|
|
|
|
// which makes fileList closure problem
|
|
|
|
flushSync(() => {
|
|
|
|
setMergedFileList(cloneList);
|
|
|
|
});
|
2020-12-15 17:09:52 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const changeInfo: UploadChangeParam<UploadFile> = {
|
|
|
|
file: file as UploadFile,
|
2020-12-15 17:09:52 +08:00
|
|
|
fileList: cloneList,
|
2021-02-26 15:18:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
changeInfo.event = event;
|
|
|
|
}
|
|
|
|
|
2023-06-16 00:47:55 +08:00
|
|
|
if (
|
|
|
|
!exceedMaxCount ||
|
2024-03-28 10:14:34 +08:00
|
|
|
file.status === 'removed' ||
|
2023-06-16 00:47:55 +08:00
|
|
|
// We should ignore event if current file is exceed `maxCount`
|
|
|
|
cloneList.some((f) => f.uid === file.uid)
|
|
|
|
) {
|
|
|
|
flushSync(() => {
|
|
|
|
onChange?.(changeInfo);
|
|
|
|
});
|
|
|
|
}
|
2021-02-26 15:18:37 +08:00
|
|
|
};
|
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const mergedBeforeUpload = async (file: RcFile, fileListArgs: RcFile[]) => {
|
|
|
|
const { beforeUpload, transformFile } = props;
|
|
|
|
|
|
|
|
let parsedFile: File | Blob | string = file;
|
|
|
|
if (beforeUpload) {
|
|
|
|
const result = await beforeUpload(file, fileListArgs);
|
|
|
|
|
|
|
|
if (result === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hack for LIST_IGNORE, we add additional info to remove from the list
|
|
|
|
delete (file as any)[LIST_IGNORE];
|
|
|
|
if ((result as any) === LIST_IGNORE) {
|
|
|
|
Object.defineProperty(file, LIST_IGNORE, {
|
|
|
|
value: true,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof result === 'object' && result) {
|
|
|
|
parsedFile = result as File;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transformFile) {
|
|
|
|
parsedFile = await transformFile(parsedFile as any);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedFile as RcFile;
|
|
|
|
};
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const onBatchStart: RcUploadProps['onBatchStart'] = (batchFileInfoList) => {
|
2021-02-26 15:18:37 +08:00
|
|
|
// Skip file which marked as `LIST_IGNORE`, these file will not add to file list
|
2022-11-19 13:47:33 +08:00
|
|
|
const filteredFileInfoList = batchFileInfoList.filter(
|
|
|
|
(info) => !(info.file as any)[LIST_IGNORE],
|
|
|
|
);
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
// Nothing to do since no file need upload
|
|
|
|
if (!filteredFileInfoList.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const objectFileList = filteredFileInfoList.map((info) => file2Obj(info.file as RcFile));
|
2021-02-26 15:18:37 +08:00
|
|
|
|
|
|
|
// Concat new files with prev files
|
2021-03-13 23:46:32 +08:00
|
|
|
let newFileList = [...mergedFileList];
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
objectFileList.forEach((fileObj) => {
|
2021-03-13 23:46:32 +08:00
|
|
|
// Replace file if exist
|
|
|
|
newFileList = updateFileList(fileObj, newFileList);
|
2020-12-15 17:09:52 +08:00
|
|
|
});
|
2021-02-26 15:18:37 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
objectFileList.forEach((fileObj, index) => {
|
|
|
|
// Repeat trigger `onChange` event for compatible
|
|
|
|
let triggerFileObj: UploadFile = fileObj;
|
|
|
|
|
|
|
|
if (!filteredFileInfoList[index].parsedFile) {
|
|
|
|
// `beforeUpload` return false
|
|
|
|
const { originFileObj } = fileObj;
|
2024-06-22 21:59:12 +08:00
|
|
|
let clone: UploadFile;
|
2021-05-08 18:14:57 +08:00
|
|
|
|
|
|
|
try {
|
2021-09-08 09:30:09 +08:00
|
|
|
clone = new File([originFileObj], originFileObj.name, {
|
2021-05-08 18:14:57 +08:00
|
|
|
type: originFileObj.type,
|
2021-09-08 09:30:09 +08:00
|
|
|
}) as any as UploadFile;
|
2024-08-04 12:24:35 +08:00
|
|
|
} catch {
|
2021-09-08 09:30:09 +08:00
|
|
|
clone = new Blob([originFileObj], {
|
2021-05-08 18:14:57 +08:00
|
|
|
type: originFileObj.type,
|
2021-09-08 09:30:09 +08:00
|
|
|
}) as any as UploadFile;
|
2021-05-08 18:14:57 +08:00
|
|
|
clone.name = originFileObj.name;
|
|
|
|
clone.lastModifiedDate = new Date();
|
|
|
|
clone.lastModified = new Date().getTime();
|
|
|
|
}
|
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
clone.uid = fileObj.uid;
|
|
|
|
triggerFileObj = clone;
|
|
|
|
} else {
|
|
|
|
// Inject `uploading` status
|
|
|
|
fileObj.status = 'uploading';
|
|
|
|
}
|
2018-09-19 22:37:35 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
onInternalChange(triggerFileObj, newFileList);
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const onSuccess = (response: any, file: RcFile, xhr: any) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
try {
|
|
|
|
if (typeof response === 'string') {
|
2024-06-22 21:59:12 +08:00
|
|
|
// biome-ignore lint/style/noParameterAssign: we need to modify response
|
2017-03-17 11:47:05 +08:00
|
|
|
response = JSON.parse(response);
|
|
|
|
}
|
2024-08-04 12:24:35 +08:00
|
|
|
} catch {
|
2018-12-07 20:02:01 +08:00
|
|
|
/* do nothing */
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
2021-03-04 16:59:06 +08:00
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
2021-03-04 16:59:06 +08:00
|
|
|
if (!getFileItem(file, mergedFileList)) {
|
2017-03-17 11:47:05 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-03-04 16:59:06 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const targetItem = file2Obj(file);
|
2017-03-17 11:47:05 +08:00
|
|
|
targetItem.status = 'done';
|
2021-03-04 16:59:06 +08:00
|
|
|
targetItem.percent = 100;
|
2017-03-17 11:47:05 +08:00
|
|
|
targetItem.response = response;
|
2019-11-04 10:44:43 +08:00
|
|
|
targetItem.xhr = xhr;
|
2021-03-04 16:59:06 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const nextFileList = updateFileList(targetItem, mergedFileList);
|
2021-03-04 16:59:06 +08:00
|
|
|
|
|
|
|
onInternalChange(targetItem, nextFileList);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const onProgress = (e: { percent: number }, file: RcFile) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
2021-03-04 16:59:06 +08:00
|
|
|
if (!getFileItem(file, mergedFileList)) {
|
2017-03-17 11:47:05 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-03-04 16:59:06 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const targetItem = file2Obj(file);
|
2021-03-04 16:59:06 +08:00
|
|
|
targetItem.status = 'uploading';
|
2017-03-17 11:47:05 +08:00
|
|
|
targetItem.percent = e.percent;
|
2021-03-04 16:59:06 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const nextFileList = updateFileList(targetItem, mergedFileList);
|
2021-03-04 16:59:06 +08:00
|
|
|
|
|
|
|
onInternalChange(targetItem, nextFileList, e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const onError = (error: Error, response: any, file: RcFile) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
2021-03-04 16:59:06 +08:00
|
|
|
if (!getFileItem(file, mergedFileList)) {
|
2017-03-17 11:47:05 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-03-04 16:59:06 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const targetItem = file2Obj(file);
|
2017-03-17 11:47:05 +08:00
|
|
|
targetItem.error = error;
|
|
|
|
targetItem.response = response;
|
|
|
|
targetItem.status = 'error';
|
2021-03-04 16:59:06 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
const nextFileList = updateFileList(targetItem, mergedFileList);
|
2021-03-04 16:59:06 +08:00
|
|
|
|
|
|
|
onInternalChange(targetItem, nextFileList);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const handleRemove = (file: UploadFile) => {
|
2021-01-01 21:40:42 +08:00
|
|
|
let currentFile: UploadFile;
|
2022-11-19 13:47:33 +08:00
|
|
|
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then((ret) => {
|
2019-09-23 10:16:09 +08:00
|
|
|
// Prevent removing file
|
|
|
|
if (ret === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
const removedFileList = removeFileItem(file, mergedFileList);
|
2019-09-23 10:16:09 +08:00
|
|
|
|
|
|
|
if (removedFileList) {
|
2022-07-25 17:38:28 +08:00
|
|
|
currentFile = { ...file, status: 'removed' };
|
2022-11-19 13:47:33 +08:00
|
|
|
mergedFileList?.forEach((item) => {
|
2022-07-25 17:38:28 +08:00
|
|
|
const matchKey = currentFile.uid !== undefined ? 'uid' : 'name';
|
|
|
|
if (item[matchKey] === currentFile[matchKey] && !Object.isFrozen(item)) {
|
|
|
|
item.status = 'removed';
|
|
|
|
}
|
|
|
|
});
|
2022-11-04 18:36:54 +08:00
|
|
|
upload.current?.abort(currentFile as RcFile);
|
2022-07-25 17:38:28 +08:00
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
onInternalChange(currentFile, removedFileList);
|
2019-09-23 10:16:09 +08:00
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
setDragState(e.type);
|
2021-04-30 22:11:31 +08:00
|
|
|
|
|
|
|
if (e.type === 'drop') {
|
|
|
|
onDrop?.(e);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
// Test needs
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
2021-03-13 23:46:32 +08:00
|
|
|
onBatchStart,
|
2020-08-18 15:44:31 +08:00
|
|
|
onSuccess,
|
|
|
|
onProgress,
|
|
|
|
onError,
|
2021-02-26 15:18:37 +08:00
|
|
|
fileList: mergedFileList,
|
2020-08-18 15:44:31 +08:00
|
|
|
upload: upload.current,
|
2024-04-01 21:42:17 +08:00
|
|
|
nativeElement: wrapRef.current,
|
2020-08-18 15:44:31 +08:00
|
|
|
}));
|
|
|
|
|
2023-07-03 16:38:56 +08:00
|
|
|
const { getPrefixCls, direction, upload: ctxUpload } = React.useContext(ConfigContext);
|
2020-05-16 14:25:49 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const rcUploadProps = {
|
2021-02-26 15:18:37 +08:00
|
|
|
onBatchStart,
|
2020-08-18 15:44:31 +08:00
|
|
|
onError,
|
|
|
|
onProgress,
|
|
|
|
onSuccess,
|
2022-10-05 00:04:29 +08:00
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
multiple,
|
|
|
|
action,
|
|
|
|
accept,
|
|
|
|
supportServerRender,
|
2020-08-18 15:44:31 +08:00
|
|
|
prefixCls,
|
2022-07-18 14:21:52 +08:00
|
|
|
disabled: mergedDisabled,
|
2021-02-26 15:18:37 +08:00
|
|
|
beforeUpload: mergedBeforeUpload,
|
2020-12-17 15:09:18 +08:00
|
|
|
onChange: undefined,
|
2023-12-30 15:21:52 +08:00
|
|
|
hasControlInside,
|
2024-04-01 15:49:45 +08:00
|
|
|
} as any;
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
delete rcUploadProps.className;
|
|
|
|
delete rcUploadProps.style;
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
// Remove id to avoid open by label when trigger is hidden
|
|
|
|
// !children: https://github.com/ant-design/ant-design/issues/14298
|
|
|
|
// disabled: https://github.com/ant-design/ant-design/issues/16478
|
|
|
|
// https://github.com/ant-design/ant-design/issues/24197
|
2022-07-18 14:21:52 +08:00
|
|
|
if (!children || mergedDisabled) {
|
2020-08-18 15:44:31 +08:00
|
|
|
delete rcUploadProps.id;
|
|
|
|
}
|
2019-07-22 19:18:47 +08:00
|
|
|
|
2023-11-29 17:23:45 +08:00
|
|
|
const wrapperCls = `${prefixCls}-wrapper`;
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, wrapperCls);
|
2022-04-25 18:46:53 +08:00
|
|
|
|
2023-02-24 10:51:59 +08:00
|
|
|
const [contextLocale] = useLocale('Upload', defaultLocale.Upload);
|
2023-02-22 18:18:26 +08:00
|
|
|
|
|
|
|
const {
|
|
|
|
showRemoveIcon,
|
|
|
|
showPreviewIcon,
|
|
|
|
showDownloadIcon,
|
|
|
|
removeIcon,
|
|
|
|
previewIcon,
|
|
|
|
downloadIcon,
|
2024-07-26 17:45:11 +08:00
|
|
|
extra,
|
2023-02-22 18:18:26 +08:00
|
|
|
} = typeof showUploadList === 'boolean' ? ({} as ShowUploadListInterface) : showUploadList;
|
|
|
|
|
2023-11-09 16:44:50 +08:00
|
|
|
// use showRemoveIcon if it is specified explicitly
|
|
|
|
const realShowRemoveIcon =
|
2024-08-07 11:42:33 +08:00
|
|
|
typeof showRemoveIcon === 'undefined' ? !mergedDisabled : showRemoveIcon;
|
2023-11-09 16:44:50 +08:00
|
|
|
|
2023-02-22 18:18:26 +08:00
|
|
|
const renderUploadList = (button?: React.ReactNode, buttonVisible?: boolean) => {
|
|
|
|
if (!showUploadList) {
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<UploadList
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
listType={listType}
|
|
|
|
items={mergedFileList}
|
|
|
|
previewFile={previewFile}
|
|
|
|
onPreview={onPreview}
|
|
|
|
onDownload={onDownload}
|
|
|
|
onRemove={handleRemove}
|
2023-11-09 16:44:50 +08:00
|
|
|
showRemoveIcon={realShowRemoveIcon}
|
2023-02-22 18:18:26 +08:00
|
|
|
showPreviewIcon={showPreviewIcon}
|
|
|
|
showDownloadIcon={showDownloadIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
previewIcon={previewIcon}
|
|
|
|
downloadIcon={downloadIcon}
|
|
|
|
iconRender={iconRender}
|
2024-07-26 17:45:11 +08:00
|
|
|
extra={extra}
|
2023-02-22 18:18:26 +08:00
|
|
|
locale={{ ...contextLocale, ...propLocale }}
|
|
|
|
isImageUrl={isImageUrl}
|
|
|
|
progress={progress}
|
|
|
|
appendAction={button}
|
|
|
|
appendActionVisible={buttonVisible}
|
|
|
|
itemRender={itemRender}
|
2023-05-04 17:03:04 +08:00
|
|
|
disabled={mergedDisabled}
|
2023-02-22 18:18:26 +08:00
|
|
|
/>
|
2020-08-26 18:43:34 +08:00
|
|
|
);
|
2023-02-22 18:18:26 +08:00
|
|
|
};
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const mergedCls = classNames(
|
|
|
|
wrapperCls,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
hashId,
|
|
|
|
cssVarCls,
|
|
|
|
ctxUpload?.className,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-picture-card-wrapper`]: listType === 'picture-card',
|
|
|
|
[`${prefixCls}-picture-circle-wrapper`]: listType === 'picture-circle',
|
|
|
|
},
|
|
|
|
);
|
2023-07-03 16:38:56 +08:00
|
|
|
|
|
|
|
const mergedStyle: React.CSSProperties = { ...ctxUpload?.style, ...style };
|
2022-04-25 18:46:53 +08:00
|
|
|
|
2024-04-01 21:42:17 +08:00
|
|
|
// ======================== Render ========================
|
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
if (type === 'drag') {
|
2023-07-03 16:38:56 +08:00
|
|
|
const dragCls = classNames(hashId, prefixCls, `${prefixCls}-drag`, {
|
|
|
|
[`${prefixCls}-drag-uploading`]: mergedFileList.some((file) => file.status === 'uploading'),
|
|
|
|
[`${prefixCls}-drag-hover`]: dragState === 'dragover',
|
|
|
|
[`${prefixCls}-disabled`]: mergedDisabled,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
|
2023-11-13 19:44:27 +08:00
|
|
|
return wrapCSSVar(
|
2024-04-01 21:42:17 +08:00
|
|
|
<span className={mergedCls} ref={wrapRef}>
|
2020-08-18 15:44:31 +08:00
|
|
|
<div
|
|
|
|
className={dragCls}
|
2023-07-03 16:38:56 +08:00
|
|
|
style={mergedStyle}
|
2020-08-18 15:44:31 +08:00
|
|
|
onDrop={onFileDrop}
|
|
|
|
onDragOver={onFileDrop}
|
|
|
|
onDragLeave={onFileDrop}
|
|
|
|
>
|
|
|
|
<RcUpload {...rcUploadProps} ref={upload} className={`${prefixCls}-btn`}>
|
|
|
|
<div className={`${prefixCls}-drag-container`}>{children}</div>
|
|
|
|
</RcUpload>
|
|
|
|
</div>
|
2020-08-24 16:33:24 +08:00
|
|
|
{renderUploadList()}
|
2022-04-25 18:46:53 +08:00
|
|
|
</span>,
|
2017-03-17 11:47:05 +08:00
|
|
|
);
|
2020-08-18 15:44:31 +08:00
|
|
|
}
|
|
|
|
|
2022-04-25 18:46:53 +08:00
|
|
|
const uploadButtonCls = classNames(prefixCls, `${prefixCls}-select`, {
|
2022-07-18 14:21:52 +08:00
|
|
|
[`${prefixCls}-disabled`]: mergedDisabled,
|
2020-08-18 15:44:31 +08:00
|
|
|
});
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2024-02-02 11:14:22 +08:00
|
|
|
const uploadButton = (
|
|
|
|
<div className={uploadButtonCls} style={children ? undefined : { display: 'none' }}>
|
2020-08-18 15:44:31 +08:00
|
|
|
<RcUpload {...rcUploadProps} ref={upload} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2023-01-31 14:31:34 +08:00
|
|
|
if (listType === 'picture-card' || listType === 'picture-circle') {
|
2023-11-13 19:44:27 +08:00
|
|
|
return wrapCSSVar(
|
2024-04-01 21:42:17 +08:00
|
|
|
<span className={mergedCls} ref={wrapRef}>
|
|
|
|
{renderUploadList(uploadButton, !!children)}
|
|
|
|
</span>,
|
2020-08-18 15:44:31 +08:00
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2023-11-13 19:44:27 +08:00
|
|
|
return wrapCSSVar(
|
2024-04-01 21:42:17 +08:00
|
|
|
<span className={mergedCls} ref={wrapRef}>
|
2022-06-23 16:45:24 +08:00
|
|
|
{uploadButton}
|
2020-08-24 16:33:24 +08:00
|
|
|
{renderUploadList()}
|
2022-04-25 18:46:53 +08:00
|
|
|
</span>,
|
2020-08-18 15:44:31 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-06-13 11:48:01 +08:00
|
|
|
const Upload = React.forwardRef<UploadRef, UploadProps>(InternalUpload);
|
2022-10-05 00:04:29 +08:00
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Upload.displayName = 'Upload';
|
|
|
|
}
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
export default Upload;
|