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';
|
2022-06-21 10:24:52 +08:00
|
|
|
import * as React from 'react';
|
2022-08-09 16:29:48 +08:00
|
|
|
import { flushSync } from 'react-dom';
|
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';
|
2022-06-21 10:24:52 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|
|
|
import defaultLocale from '../locale/default';
|
|
|
|
import warning from '../_util/warning';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type {
|
2018-04-19 11:54:28 +08:00
|
|
|
RcFile,
|
2020-08-26 18:43:34 +08:00
|
|
|
ShowUploadListInterface,
|
2022-06-21 10:24:52 +08:00
|
|
|
UploadChangeParam,
|
2018-04-16 10:58:17 +08:00
|
|
|
UploadFile,
|
2022-06-21 10:24:52 +08:00
|
|
|
UploadListType,
|
2018-04-16 10:58:17 +08:00
|
|
|
UploadLocale,
|
|
|
|
UploadType,
|
|
|
|
} from './interface';
|
2022-05-07 14:31:54 +08:00
|
|
|
import { UploadProps } from './interface';
|
2022-06-21 10:24:52 +08:00
|
|
|
import UploadList from './UploadList';
|
2021-03-13 23:46:32 +08:00
|
|
|
import { file2Obj, getFileItem, removeFileItem, updateFileList } from './utils';
|
2021-02-26 15:18:37 +08:00
|
|
|
|
2022-04-25 18:46:53 +08:00
|
|
|
import useStyle from './style';
|
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
|
|
|
|
|
|
|
export { UploadProps };
|
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (props, ref) => {
|
|
|
|
const {
|
2021-02-26 15:18:37 +08:00
|
|
|
fileList,
|
2020-08-18 15:44:31 +08:00
|
|
|
defaultFileList,
|
|
|
|
onRemove,
|
|
|
|
showUploadList,
|
|
|
|
listType,
|
|
|
|
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,
|
|
|
|
type,
|
|
|
|
children,
|
|
|
|
style,
|
2020-09-17 16:14:42 +08:00
|
|
|
itemRender,
|
2020-12-15 17:09:52 +08:00
|
|
|
maxCount,
|
2020-08-18 15:44:31 +08:00
|
|
|
} = props;
|
|
|
|
|
2022-07-18 14:21:52 +08:00
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = customDisabled || disabled;
|
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
const [mergedFileList, setMergedFileList] = useMergedState(defaultFileList || [], {
|
|
|
|
value: fileList,
|
2021-03-10 17:32:32 +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
|
|
|
|
|
|
|
const upload = React.useRef<any>();
|
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
|
|
|
'fileList' in props || !('value' in props),
|
|
|
|
'Upload',
|
|
|
|
'`value` is not a valid prop, do you mean `fileList`?',
|
|
|
|
);
|
2020-12-15 15:56:35 +08:00
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
|
|
|
!('transformFile' in props),
|
|
|
|
'Upload',
|
|
|
|
'`transformFile` is deprecated. Please use `beforeUpload` directly.',
|
|
|
|
);
|
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
|
|
|
|
2020-12-15 17:09:52 +08:00
|
|
|
// Cut to match count
|
|
|
|
if (maxCount === 1) {
|
|
|
|
cloneList = cloneList.slice(-1);
|
|
|
|
} else if (maxCount) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange?.(changeInfo);
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-02-26 15:18:37 +08:00
|
|
|
const onBatchStart: RcUploadProps['onBatchStart'] = batchFileInfoList => {
|
|
|
|
// Skip file which marked as `LIST_IGNORE`, these file will not add to file list
|
|
|
|
const filteredFileInfoList = batchFileInfoList.filter(info => !(info.file as any)[LIST_IGNORE]);
|
|
|
|
|
|
|
|
// Nothing to do since no file need upload
|
|
|
|
if (!filteredFileInfoList.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-13 23:46:32 +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];
|
|
|
|
|
2021-02-26 15:18:37 +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;
|
2021-05-08 18:14:57 +08:00
|
|
|
let clone;
|
|
|
|
|
|
|
|
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;
|
2021-05-08 18:14:57 +08:00
|
|
|
} catch (e) {
|
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') {
|
|
|
|
response = JSON.parse(response);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
} catch (e) {
|
|
|
|
/* 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;
|
2019-09-23 10:16:09 +08:00
|
|
|
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(ret => {
|
|
|
|
// 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' };
|
|
|
|
mergedFileList?.forEach(item => {
|
|
|
|
const matchKey = currentFile.uid !== undefined ? 'uid' : 'name';
|
|
|
|
if (item[matchKey] === currentFile[matchKey] && !Object.isFrozen(item)) {
|
|
|
|
item.status = 'removed';
|
|
|
|
}
|
|
|
|
});
|
2021-01-16 18:06:26 +08:00
|
|
|
upload.current?.abort(currentFile);
|
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,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const { getPrefixCls, direction } = 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,
|
2021-12-07 21:45:11 +08:00
|
|
|
...(props as RcUploadProps),
|
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,
|
2020-08-18 15:44:31 +08:00
|
|
|
};
|
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
|
|
|
|
2022-04-25 18:46:53 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2022-01-25 11:20:46 +08:00
|
|
|
const renderUploadList = (button?: React.ReactNode, buttonVisible?: boolean) =>
|
2020-08-24 16:33:24 +08:00
|
|
|
showUploadList ? (
|
|
|
|
<LocaleReceiver componentName="Upload" defaultLocale={defaultLocale.Upload}>
|
|
|
|
{(locale: UploadLocale) => {
|
2021-09-08 09:30:09 +08:00
|
|
|
const {
|
|
|
|
showRemoveIcon,
|
|
|
|
showPreviewIcon,
|
|
|
|
showDownloadIcon,
|
|
|
|
removeIcon,
|
|
|
|
previewIcon,
|
|
|
|
downloadIcon,
|
|
|
|
} =
|
2020-08-26 18:43:34 +08:00
|
|
|
typeof showUploadList === 'boolean' ? ({} as ShowUploadListInterface) : showUploadList;
|
2020-08-24 16:33:24 +08:00
|
|
|
return (
|
|
|
|
<UploadList
|
2022-04-15 19:36:38 +08:00
|
|
|
prefixCls={prefixCls}
|
2020-08-24 16:33:24 +08:00
|
|
|
listType={listType}
|
2021-02-26 15:18:37 +08:00
|
|
|
items={mergedFileList}
|
2020-08-24 16:33:24 +08:00
|
|
|
previewFile={previewFile}
|
|
|
|
onPreview={onPreview}
|
|
|
|
onDownload={onDownload}
|
|
|
|
onRemove={handleRemove}
|
2022-07-18 14:21:52 +08:00
|
|
|
showRemoveIcon={!mergedDisabled && showRemoveIcon}
|
2020-08-24 16:33:24 +08:00
|
|
|
showPreviewIcon={showPreviewIcon}
|
|
|
|
showDownloadIcon={showDownloadIcon}
|
|
|
|
removeIcon={removeIcon}
|
2021-09-08 09:30:09 +08:00
|
|
|
previewIcon={previewIcon}
|
2020-08-24 16:33:24 +08:00
|
|
|
downloadIcon={downloadIcon}
|
|
|
|
iconRender={iconRender}
|
|
|
|
locale={{ ...locale, ...propLocale }}
|
|
|
|
isImageUrl={isImageUrl}
|
|
|
|
progress={progress}
|
|
|
|
appendAction={button}
|
2022-01-25 11:20:46 +08:00
|
|
|
appendActionVisible={buttonVisible}
|
2020-09-17 16:14:42 +08:00
|
|
|
itemRender={itemRender}
|
2020-08-24 16:33:24 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</LocaleReceiver>
|
2020-08-26 18:43:34 +08:00
|
|
|
) : (
|
|
|
|
button
|
|
|
|
);
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2022-04-25 18:46:53 +08:00
|
|
|
const rtlCls = {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
};
|
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
if (type === 'drag') {
|
|
|
|
const dragCls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-drag`]: true,
|
2021-02-26 15:18:37 +08:00
|
|
|
[`${prefixCls}-drag-uploading`]: mergedFileList.some(file => file.status === 'uploading'),
|
2020-08-18 15:44:31 +08:00
|
|
|
[`${prefixCls}-drag-hover`]: dragState === 'dragover',
|
2022-07-18 14:21:52 +08:00
|
|
|
[`${prefixCls}-disabled`]: mergedDisabled,
|
2020-08-18 15:44:31 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2022-04-25 18:46:53 +08:00
|
|
|
hashId,
|
2020-08-18 15:44:31 +08:00
|
|
|
);
|
2022-04-25 18:46:53 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<span className={classNames(`${prefixCls}-wrapper`, rtlCls, className, hashId)}>
|
2020-08-18 15:44:31 +08:00
|
|
|
<div
|
|
|
|
className={dragCls}
|
|
|
|
onDrop={onFileDrop}
|
|
|
|
onDragOver={onFileDrop}
|
|
|
|
onDragLeave={onFileDrop}
|
|
|
|
style={style}
|
|
|
|
>
|
|
|
|
<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
|
|
|
|
2022-01-25 11:20:46 +08:00
|
|
|
const renderUploadButton = (uploadButtonStyle?: React.CSSProperties) => (
|
|
|
|
<div className={uploadButtonCls} style={uploadButtonStyle}>
|
2020-08-18 15:44:31 +08:00
|
|
|
<RcUpload {...rcUploadProps} ref={upload} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2022-06-23 16:45:24 +08:00
|
|
|
const uploadButton = renderUploadButton(children ? undefined : { display: 'none' });
|
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
if (listType === 'picture-card') {
|
2022-04-25 18:46:53 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<span
|
|
|
|
className={classNames(
|
|
|
|
`${prefixCls}-wrapper`,
|
|
|
|
`${prefixCls}-picture-card-wrapper`,
|
|
|
|
rtlCls,
|
|
|
|
className,
|
|
|
|
hashId,
|
|
|
|
)}
|
|
|
|
>
|
2022-06-23 16:45:24 +08:00
|
|
|
{renderUploadList(uploadButton, !!children)}
|
2022-04-25 18:46:53 +08:00
|
|
|
</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
|
|
|
|
2022-04-25 18:46:53 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<span className={classNames(`${prefixCls}-wrapper`, rtlCls, className, hashId)}>
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-24 20:55:18 +08:00
|
|
|
const Upload = React.forwardRef<unknown, UploadProps>(InternalUpload);
|
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
|
|
|
|
|
|
|
Upload.defaultProps = {
|
|
|
|
type: 'select' as UploadType,
|
|
|
|
multiple: false,
|
|
|
|
action: '',
|
|
|
|
data: {},
|
|
|
|
accept: '',
|
|
|
|
showUploadList: true,
|
|
|
|
listType: 'text' as UploadListType, // or picture
|
|
|
|
className: '',
|
|
|
|
disabled: false,
|
|
|
|
supportServerRender: true,
|
|
|
|
};
|
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
export default Upload;
|