fix: Upload types for pass generic (#33543)

* fix: Upload types for pass generic

* fix: Upload type

* refactor: Upload type

* feat: add Upload type test case
This commit is contained in:
uchanlee 2022-01-05 20:15:28 +09:00 committed by GitHub
parent 9a6ae9bb82
commit 8ebb4a3d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 8 deletions

View File

@ -422,15 +422,20 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
);
};
interface CompoundedComponent
extends React.ForwardRefExoticComponent<
React.PropsWithChildren<UploadProps> & React.RefAttributes<any>
> {
const ForwardUpload = React.forwardRef<unknown, UploadProps>(InternalUpload) as <T>(
props: React.PropsWithChildren<UploadProps<T>> & React.RefAttributes<any>,
) => React.ReactElement;
type InternalUploadType = typeof ForwardUpload;
interface UploadInterface extends InternalUploadType {
defaultProps?: Partial<UploadProps>;
displayName?: string;
Dragger: typeof Dragger;
LIST_IGNORE: string;
}
const Upload = React.forwardRef<unknown, UploadProps>(InternalUpload) as CompoundedComponent;
const Upload = ForwardUpload as UploadInterface;
Upload.Dragger = Dragger;

View File

@ -1,5 +1,5 @@
import React from 'react';
import Upload from '..';
import Upload, { UploadProps } from '..';
describe('Upload.typescript', () => {
it('Upload', () => {
@ -11,6 +11,29 @@ describe('Upload.typescript', () => {
expect(upload).toBeTruthy();
});
it('onChange', () => {
const upload = (
<Upload<File> onChange={({ file }) => file}>
<span>click to upload</span>
</Upload>
);
expect(upload).toBeTruthy();
});
it('onChange in UploadProps', () => {
const uploadProps: UploadProps<File> = {
onChange: ({ file }) => file,
};
const upload = (
<Upload {...uploadProps}>
<span>click to upload</span>
</Upload>
);
expect(upload).toBeTruthy();
});
it('showUploadList', () => {
const upload = (
<Upload

View File

@ -40,7 +40,7 @@ export interface InternalUploadFile<T = any> extends UploadFile<T> {
originFileObj: RcFile;
}
export interface UploadChangeParam<T extends object = UploadFile> {
export interface UploadChangeParam<T = UploadFile> {
// https://github.com/ant-design/ant-design/issues/14420
file: T;
fileList: UploadFile[];
@ -104,7 +104,7 @@ export interface UploadProps<T = any> extends Pick<RcUploadProps, 'capture'> {
file: RcFile,
FileList: RcFile[],
) => BeforeUploadValueType | Promise<BeforeUploadValueType>;
onChange?: (info: UploadChangeParam) => void;
onChange?: (info: UploadChangeParam<T>) => void;
onDrop?: (event: React.DragEvent<HTMLDivElement>) => void;
listType?: UploadListType;
className?: string;