|
|
|
import classNames from 'classnames';
|
|
|
|
import type { UploadProps as RcUploadProps } from 'rc-upload';
|
|
|
|
import RcUpload from 'rc-upload';
|
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
|
|
|
import * as React from 'react';
|
|
|
|
import { flushSync } from 'react-dom';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|
|
|
import defaultLocale from '../locale/en_US';
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
import type { RcFile, ShowUploadListInterface, UploadChangeParam, UploadFile } from './interface';
|
|
|
|
import { UploadProps } from './interface';
|
|
|
|
import UploadList from './UploadList';
|
|
|
|
import { file2Obj, getFileItem, removeFileItem, updateFileList } from './utils';
|
|
|
|
|
|
|
|
import useStyle from './style';
|
|
|
|
|
|
|
|
export const LIST_IGNORE = `__LIST_IGNORE_${Date.now()}__`;
|
|
|
|
|
|
|
|
export { UploadProps };
|
|
|
|
|
|
|
|
const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (props, ref) => {
|
|
|
|
const {
|
|
|
|
fileList,
|
|
|
|
defaultFileList,
|
|
|
|
onRemove,
|
|
|
|
showUploadList = true,
|
|
|
|
listType = 'text',
|
|
|
|
onPreview,
|
|
|
|
onDownload,
|
|
|
|
onChange,
|
|
|
|
onDrop,
|
|
|
|
previewFile,
|
|
|
|
disabled: customDisabled,
|
|
|
|
locale: propLocale,
|
|
|
|
iconRender,
|
|
|
|
isImageUrl,
|
|
|
|
progress,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
type = 'select',
|
|
|
|
children,
|
|
|
|
style,
|
|
|
|
itemRender,
|
|
|
|
maxCount,
|
|
|
|
data = {},
|
|
|
|
multiple = false,
|
|
|
|
action = '',
|
|
|
|
accept = '',
|
|
|
|
supportServerRender = true,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
|
|
|
|
|
|
|
const [mergedFileList, setMergedFileList] = useMergedState(defaultFileList || [], {
|
|
|
|
value: fileList,
|
|
|
|
postState: list => list ?? [],
|
|
|
|
});
|
|
|
|
|
|
|
|
const [dragState, setDragState] = React.useState<string>('drop');
|
|
|
|
|
|
|
|
const upload = React.useRef<RcUpload>(null);
|
|
|
|
|
|
|
|
warning(
|
|
|
|
'fileList' in props || !('value' in props),
|
|
|
|
'Upload',
|
|
|
|
'`value` is not a valid prop, do you mean `fileList`?',
|
|
|
|
);
|
|
|
|
|
|
|