ant-design/components/upload/UploadList/index.tsx
陈帅 0d1b1c40a6
merge featrue into master (#30636)
* feat: add Table expandable fixed (#29959)

* fix: Use flex gap of space (#30023)

* fix: use flex gap when supported

* test: update snapshot

* refactor: Use single hooks

* feat: Allow breadcrumb component in PageHeader (#30019)

* Allow breadcrumb component in PageHeader

* Allow breadcrumb component in PageHeader

* Allow breadcrumb component in PageHeader

* Rename variable

rename var from _breadcrumbRender to breadcrumbRenderDomFromProps

* feat: add onChange for Statistic.Countdown (#30265)

* feat: add onChange for countdown

* update the demo

* feat(upload): add onDrop (#30319)

* feat(upload): Add onDrop

* Replace "if prop" logic with existential operator

* Remove redundant conditional

* feat(upload): itemRender add actions params (#30236)

* feat(upload): itemRender add actions params

* chore: optimize type definition

* chore: update doc

* chore: rename actions

* chore: trigger ci

* chore: rename method name of actions

* feat: Add missing dutch translations (#30389)

* feat: Add missing dutch translations

* fix: Translate remaining english values

* fix: Update snapshot for ui tests

* test: increase code coverage to 100% (#30415)

* test: fix Space code coverage

* test: should use nl_BE locale for DatePicker

* fix: Switch tabIndex type (#30416)

* feat: updated Romanian internationalization (#30419)

* feat: updated Romanian internationalization

* fixed lint error

* feat: Menu support accessibility & keyboard access (#30382)

* chore: Use focus style

* fix: prefixCls

* fix: prefixCls

* fix: inline tooltip

* fix: inlineCollapse logic

* fix: ts definition

* test: Update snapshot

* test: Update snapshot

* fix: dropdown logic

* test: Update snapshot

* test: Fix some test  case

* bump rc-menu

* test: More test case

* fix test finder

* test: fix test case

* test: Update snapshot

* test: Update snapshot

* chore: Update ssr effect

* test: Update ConfigProvider snapshot

* test: Fix Table Filter test case

* test: Fix table test case

* chore: Update style

* chore: beauti css

* bump rc-menu

* test: update snapshot

* test: update snapshot

* test: Fix menu test

* test: Fix test case

* test: Coverage

* chore: clean up

* bump rc-menu

* ehance accessibility style

* feat(radioGroup): support data-* and aria-* props (#30507)

close #30501

* feat: Typography add italic type (#30458)

* Typography增加斜体字支持

* update snapshot

* 文档添加版本号

Co-authored-by: lidahao <lidahao@sisyphe.com.cn>

* chore: alpha Menu fix merge (#30546)

* chore: Update script

* bump alpha version

* chore: Update script desc

* chore: bump rc-tabs & rc-mentions

* chore: Adjust style

* chore: 4.16.0-alpha.1

* test: Fix mention test case

* fix: sider of layout width style

* chore: bump 4.16.0-alpha.2

* fix: Tabs tabBarGutter should work as expected (#30545)

close #30526

* feat: Table summary support sticky mode (#30631)

* chore: Bump rc-table

* feat: Patch summary fixed color

* fix: style className

* test: Update snapshot

Co-authored-by: xrkffgg <xrkffgg@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: 二货机器人 <smith3816@gmail.com>
Co-authored-by: gepd <guillermoepd@hotmail.com>
Co-authored-by: appleshell <appleshell@outlook.com>
Co-authored-by: Eric Bonow <ebonow@hotmail.com>
Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
Co-authored-by: Kermit <kermitlx@outlook.com>
Co-authored-by: Lewis <lewisfidlers@gmail.com>
Co-authored-by: afc163 <afc163@gmail.com>
Co-authored-by: Ștefan Filip <stefy.filip@gmail.com>
Co-authored-by: vldh <alwaysloseall@sina.com>
Co-authored-by: lidahao <lidahao@sisyphe.com.cn>
2021-05-24 16:24:00 +08:00

260 lines
7.4 KiB
TypeScript

import * as React from 'react';
import CSSMotion, { CSSMotionList, CSSMotionListProps } from 'rc-motion';
import classNames from 'classnames';
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import PaperClipOutlined from '@ant-design/icons/PaperClipOutlined';
import PictureTwoTone from '@ant-design/icons/PictureTwoTone';
import FileTwoTone from '@ant-design/icons/FileTwoTone';
import { cloneElement, isValidElement } from '../../_util/reactNode';
import { UploadListProps, UploadFile, UploadListType, InternalUploadFile } from '../interface';
import { previewImage, isImageUrl } from '../utils';
import collapseMotion from '../../_util/motion';
import { ConfigContext } from '../../config-provider';
import Button, { ButtonProps } from '../../button';
import useForceUpdate from '../../_util/hooks/useForceUpdate';
import ListItem from './ListItem';
const listItemMotion: Partial<CSSMotionListProps> = {
...collapseMotion,
};
delete listItemMotion.onAppearEnd;
delete listItemMotion.onEnterEnd;
delete listItemMotion.onLeaveEnd;
const InternalUploadList: React.ForwardRefRenderFunction<unknown, UploadListProps> = (
{
listType,
previewFile,
onPreview,
onDownload,
onRemove,
locale,
iconRender,
isImageUrl: isImgUrl,
prefixCls: customizePrefixCls,
items = [],
showPreviewIcon,
showRemoveIcon,
showDownloadIcon,
removeIcon,
downloadIcon,
progress,
appendAction,
itemRender,
},
ref,
) => {
const forceUpdate = useForceUpdate();
const [motionAppear, setMotionAppear] = React.useState(false);
// ============================= Effect =============================
React.useEffect(() => {
if (listType !== 'picture' && listType !== 'picture-card') {
return;
}
(items || []).forEach((file: InternalUploadFile) => {
if (
typeof document === 'undefined' ||
typeof window === 'undefined' ||
!(window as any).FileReader ||
!(window as any).File ||
!(file.originFileObj instanceof File || (file.originFileObj as Blob) instanceof Blob) ||
file.thumbUrl !== undefined
) {
return;
}
file.thumbUrl = '';
if (previewFile) {
previewFile(file.originFileObj as File).then((previewDataUrl: string) => {
// Need append '' to avoid dead loop
file.thumbUrl = previewDataUrl || '';
forceUpdate();
});
}
});
}, [listType, items, previewFile]);
React.useEffect(() => {
setMotionAppear(true);
}, []);
// ============================= Events =============================
const onInternalPreview = (file: UploadFile, e?: React.SyntheticEvent<HTMLElement>) => {
if (!onPreview) {
return;
}
e?.preventDefault();
return onPreview(file);
};
const onInternalDownload = (file: UploadFile) => {
if (typeof onDownload === 'function') {
onDownload(file);
} else if (file.url) {
window.open(file.url);
}
};
const onInternalClose = (file: UploadFile) => {
onRemove?.(file);
};
const internalIconRender = (file: UploadFile) => {
if (iconRender) {
return iconRender(file, listType);
}
const isLoading = file.status === 'uploading';
const fileIcon = isImgUrl && isImgUrl(file) ? <PictureTwoTone /> : <FileTwoTone />;
let icon: React.ReactNode = isLoading ? <LoadingOutlined /> : <PaperClipOutlined />;
if (listType === 'picture') {
icon = isLoading ? <LoadingOutlined /> : fileIcon;
} else if (listType === 'picture-card') {
icon = isLoading ? locale.uploading : fileIcon;
}
return icon;
};
const actionIconRender = (
customIcon: React.ReactNode,
callback: () => void,
prefixCls: string,
title?: string,
) => {
const btnProps: ButtonProps = {
type: 'text',
size: 'small',
title,
onClick: (e: React.MouseEvent<HTMLElement>) => {
callback();
if (isValidElement(customIcon) && customIcon.props.onClick) {
customIcon.props.onClick(e);
}
},
className: `${prefixCls}-list-item-card-actions-btn`,
};
if (isValidElement(customIcon)) {
const btnIcon = cloneElement(customIcon, {
...customIcon.props,
onClick: () => {},
});
return <Button {...btnProps} icon={btnIcon} />;
}
return (
<Button {...btnProps}>
<span>{customIcon}</span>
</Button>
);
};
// ============================== Ref ===============================
// Test needs
React.useImperativeHandle(ref, () => ({
handlePreview: onInternalPreview,
handleDownload: onInternalDownload,
}));
const { getPrefixCls, direction } = React.useContext(ConfigContext);
// ============================= Render =============================
const prefixCls = getPrefixCls('upload', customizePrefixCls);
const listClassNames = classNames({
[`${prefixCls}-list`]: true,
[`${prefixCls}-list-${listType}`]: true,
[`${prefixCls}-list-rtl`]: direction === 'rtl',
});
// >>> Motion config
const motionKeyList = [
...items.map(file => ({
key: file.uid,
file,
})),
];
const animationDirection = listType === 'picture-card' ? 'animate-inline' : 'animate';
// const transitionName = list.length === 0 ? '' : `${prefixCls}-${animationDirection}`;
let motionConfig: Omit<CSSMotionListProps, 'onVisibleChanged'> = {
motionDeadline: 2000,
motionName: `${prefixCls}-${animationDirection}`,
keys: motionKeyList,
motionAppear,
};
if (listType !== 'picture-card') {
motionConfig = {
...listItemMotion,
...motionConfig,
};
}
return (
<div className={listClassNames}>
<CSSMotionList {...motionConfig} component={false}>
{({ key, file, className: motionClassName, style: motionStyle }) => (
<ListItem
key={key}
locale={locale}
prefixCls={prefixCls}
className={motionClassName}
style={motionStyle}
file={file}
items={items}
progress={progress}
listType={listType}
isImgUrl={isImgUrl}
showPreviewIcon={showPreviewIcon}
showRemoveIcon={showRemoveIcon}
showDownloadIcon={showDownloadIcon}
removeIcon={removeIcon}
downloadIcon={downloadIcon}
iconRender={internalIconRender}
actionIconRender={actionIconRender}
itemRender={itemRender}
onPreview={onInternalPreview}
onDownload={onInternalDownload}
onClose={onInternalClose}
/>
)}
</CSSMotionList>
{/* Append action */}
{appendAction && (
<CSSMotion {...motionConfig}>
{({ className: motionClassName, style: motionStyle }) =>
cloneElement(appendAction, oriProps => ({
className: classNames(oriProps.className, motionClassName),
style: {
...motionStyle,
...oriProps.style,
},
}))
}
</CSSMotion>
)}
</div>
);
};
const UploadList = React.forwardRef<unknown, UploadListProps>(InternalUploadList);
UploadList.displayName = 'UploadList';
UploadList.defaultProps = {
listType: 'text' as UploadListType, // or picture
progress: {
strokeWidth: 2,
showInfo: false,
},
showRemoveIcon: true,
showDownloadIcon: false,
showPreviewIcon: true,
previewFile: previewImage,
isImageUrl,
};
export default UploadList;