Skip to content

Commit 9784b42

Browse files
committed
refactor: upload types, close #5047
1 parent 06516ec commit 9784b42

16 files changed

+92
-212
lines changed

components/components.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export {
214214
TypographyTitle,
215215
} from './typography';
216216

217-
export type { UploadProps } from './upload';
217+
export type { UploadProps, UploadListProps, UploadChangeParam } from './upload';
218218

219219
export { default as Upload, UploadDragger } from './upload';
220220

components/upload/Dragger.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { defineComponent } from 'vue';
22
import { getOptionProps, getSlot } from '../_util/props-util';
33
import Upload from './Upload';
4-
import { UploadProps } from './interface';
4+
import { uploadProps } from './interface';
55

66
export default defineComponent({
77
name: 'AUploadDragger',
88
inheritAttrs: false,
9-
props: UploadProps,
9+
props: uploadProps,
1010
render() {
1111
const props = getOptionProps(this);
1212
const { height, ...restProps } = props;

components/upload/Upload.tsx

+3-23
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,19 @@ import defaultLocale from '../locale-provider/default';
1010
import { defaultConfigProvider } from '../config-provider';
1111
import Dragger from './Dragger';
1212
import UploadList from './UploadList';
13-
import { UploadProps } from './interface';
13+
import type { UploadFile } from './interface';
14+
import { uploadProps } from './interface';
1415
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './utils';
1516
import { defineComponent, inject } from 'vue';
1617
import { getDataAndAriaProps } from '../_util/util';
1718
import { useInjectFormItemContext } from '../form/FormItemContext';
1819

19-
export type UploadFileStatus = 'error' | 'success' | 'done' | 'uploading' | 'removed';
20-
export interface UploadFile<T = any> {
21-
uid: string;
22-
size?: number;
23-
name: string;
24-
fileName?: string;
25-
lastModified?: number;
26-
lastModifiedDate?: Date;
27-
url?: string;
28-
status?: UploadFileStatus;
29-
percent?: number;
30-
thumbUrl?: string;
31-
originFileObj?: any;
32-
response?: T;
33-
error?: any;
34-
linkProps?: any;
35-
type?: string;
36-
xhr?: T;
37-
preview?: string;
38-
}
39-
4020
export default defineComponent({
4121
name: 'AUpload',
4222
mixins: [BaseMixin],
4323
inheritAttrs: false,
4424
Dragger,
45-
props: initDefaultProps(UploadProps, {
25+
props: initDefaultProps(uploadProps, {
4626
type: 'select',
4727
multiple: false,
4828
action: '',

components/upload/UploadList.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import EyeOutlined from '@ant-design/icons-vue/EyeOutlined';
2020
import Tooltip from '../tooltip';
2121
import Progress from '../progress';
2222
import classNames from '../_util/classNames';
23-
import { UploadListProps } from './interface';
23+
import { uploadListProps } from './interface';
2424

2525
export default defineComponent({
2626
name: 'AUploadList',
2727
mixins: [BaseMixin],
28-
props: initDefaultProps(UploadListProps, {
28+
props: initDefaultProps(uploadListProps, {
2929
listType: 'text', // or picture
3030
progressAttr: {
3131
strokeWidth: 2,

components/upload/__tests__/upload.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { mount } from '@vue/test-utils';
22
import Upload from '..';
33
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from '../utils';
44
import PropsTypes from '../../_util/vue-types';
5-
import { UploadListProps } from '../interface';
5+
import { uploadListProps } from '../interface';
66
import { setup, teardown } from './mock';
77

8-
UploadListProps.items = PropsTypes.any;
8+
uploadListProps.items = PropsTypes.any;
99

1010
describe('Upload', () => {
1111
beforeEach(() => setup());

components/upload/__tests__/uploadlist.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import * as Vue from 'vue';
33
import Upload from '..';
44
import { errorRequest, successRequest } from './requests';
55
import PropsTypes from '../../_util/vue-types';
6-
import { UploadListProps } from '../interface';
6+
import { uploadListProps } from '../interface';
77
import { sleep } from '../../../tests/utils';
88
import { h } from 'vue';
99

10-
UploadListProps.items = PropsTypes.any;
10+
uploadListProps.items = PropsTypes.any;
1111

1212
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
1313
const fileList = [

components/upload/demo/avatar.vue

+3-18
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,7 @@ Click to upload user's avatar, and validate size and format of picture with `bef
4242
import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue';
4343
import { message } from 'ant-design-vue';
4444
import { defineComponent, ref } from 'vue';
45-
46-
interface FileItem {
47-
uid: string;
48-
name?: string;
49-
status?: string;
50-
response?: string;
51-
url?: string;
52-
type?: string;
53-
size: number;
54-
originFileObj: any;
55-
}
56-
57-
interface FileInfo {
58-
file: FileItem;
59-
fileList: FileItem[];
60-
}
45+
import type { UploadChangeParam, UploadProps } from 'ant-design-vue';
6146
6247
function getBase64(img: Blob, callback: (base64Url: string) => void) {
6348
const reader = new FileReader();
@@ -74,7 +59,7 @@ export default defineComponent({
7459
const loading = ref<boolean>(false);
7560
const imageUrl = ref<string>('');
7661
77-
const handleChange = (info: FileInfo) => {
62+
const handleChange = (info: UploadChangeParam) => {
7863
if (info.file.status === 'uploading') {
7964
loading.value = true;
8065
return;
@@ -92,7 +77,7 @@ export default defineComponent({
9277
}
9378
};
9479
95-
const beforeUpload = (file: FileItem) => {
80+
const beforeUpload = (file: UploadProps['fileList'][number]) => {
9681
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
9782
if (!isJpgOrPng) {
9883
message.error('You can only upload JPG file!');

components/upload/demo/basic.vue

+2-14
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,14 @@ Classic mode. File selection dialog pops up when upload button is clicked.
3434
import { message } from 'ant-design-vue';
3535
import { UploadOutlined } from '@ant-design/icons-vue';
3636
import { defineComponent, ref } from 'vue';
37-
38-
interface FileItem {
39-
uid: string;
40-
name?: string;
41-
status?: string;
42-
response?: string;
43-
url?: string;
44-
}
45-
46-
interface FileInfo {
47-
file: FileItem;
48-
fileList: FileItem[];
49-
}
37+
import type { UploadChangeParam } from 'ant-design-vue';
5038
5139
export default defineComponent({
5240
components: {
5341
UploadOutlined,
5442
},
5543
setup() {
56-
const handleChange = (info: FileInfo) => {
44+
const handleChange = (info: UploadChangeParam) => {
5745
if (info.file.status !== 'uploading') {
5846
console.log(info.file, info.fileList);
5947
}

components/upload/demo/defaultFileList.vue

+3-14
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,14 @@ Use `defaultFileList` for uploaded files when page init.
2626
<script lang="ts">
2727
import { UploadOutlined } from '@ant-design/icons-vue';
2828
import { defineComponent, ref } from 'vue';
29-
30-
interface FileItem {
31-
uid: string;
32-
name?: string;
33-
status?: string;
34-
response?: string;
35-
url?: string;
36-
}
37-
interface FileInfo {
38-
file: FileItem;
39-
fileList: FileItem[];
40-
}
29+
import type { UploadChangeParam, UploadProps } from 'ant-design-vue';
4130
4231
export default defineComponent({
4332
components: {
4433
UploadOutlined,
4534
},
4635
setup() {
47-
const fileList = ref<FileItem[]>([
36+
const fileList = ref<UploadProps['fileList']>([
4837
{
4938
uid: '1',
5039
name: 'xxx.png',
@@ -67,7 +56,7 @@ export default defineComponent({
6756
},
6857
]);
6958
70-
const handleChange = ({ file, fileList }: FileInfo) => {
59+
const handleChange = ({ file, fileList }: UploadChangeParam) => {
7160
if (file.status !== 'uploading') {
7261
console.log(file, fileList);
7362
}

components/upload/demo/drag.vue

+2-14
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,14 @@ We can upload serveral files at once in modern browsers by giving the input the
4141
import { InboxOutlined } from '@ant-design/icons-vue';
4242
import { message } from 'ant-design-vue';
4343
import { defineComponent, ref } from 'vue';
44-
45-
interface FileItem {
46-
uid: string;
47-
name?: string;
48-
status?: string;
49-
response?: string;
50-
url?: string;
51-
}
52-
53-
interface FileInfo {
54-
file: FileItem;
55-
fileList: FileItem[];
56-
}
44+
import type { UploadChangeParam } from 'ant-design-vue';
5745
5846
export default defineComponent({
5947
components: {
6048
InboxOutlined,
6149
},
6250
setup() {
63-
const handleChange = (info: FileInfo) => {
51+
const handleChange = (info: UploadChangeParam) => {
6452
const status = info.file.status;
6553
if (status !== 'uploading') {
6654
console.log(info.file, info.fileList);

components/upload/demo/fileList.vue

+3-15
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,22 @@ You can gain full control over filelist by configuring `fileList`. You can accom
3939
<script lang="ts">
4040
import { UploadOutlined } from '@ant-design/icons-vue';
4141
import { defineComponent, ref } from 'vue';
42-
43-
interface FileItem {
44-
uid: string;
45-
name?: string;
46-
status?: string;
47-
response?: Response;
48-
url: string;
49-
}
50-
51-
interface FileInfo {
52-
file: FileItem;
53-
fileList: FileItem[];
54-
}
42+
import type { UploadChangeParam, UploadProps } from 'ant-design-vue';
5543
5644
export default defineComponent({
5745
components: {
5846
UploadOutlined,
5947
},
6048
setup() {
61-
const fileList = ref<FileItem[]>([
49+
const fileList = ref<UploadProps['fileList']>([
6250
{
6351
uid: '-1',
6452
name: 'xxx.png',
6553
status: 'done',
6654
url: 'http://www.baidu.com/xxx.png',
6755
},
6856
]);
69-
const handleChange = (info: FileInfo) => {
57+
const handleChange = (info: UploadChangeParam) => {
7058
let resFileList = [...info.fileList];
7159
7260
// 1. Limit the number of uploaded files

components/upload/demo/picture-card.vue

+4-19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ After users upload picture, the thumbnail will be shown in list. The upload butt
3636
<script lang="ts">
3737
import { PlusOutlined } from '@ant-design/icons-vue';
3838
import { defineComponent, ref } from 'vue';
39+
import type { UploadChangeParam, UploadProps } from 'ant-design-vue';
3940
4041
function getBase64(file: File) {
4142
return new Promise((resolve, reject) => {
@@ -46,22 +47,6 @@ function getBase64(file: File) {
4647
});
4748
}
4849
49-
interface FileItem {
50-
uid: string;
51-
name?: string;
52-
status?: string;
53-
response?: string;
54-
percent?: number;
55-
url?: string;
56-
preview?: string;
57-
originFileObj?: any;
58-
}
59-
60-
interface FileInfo {
61-
file: FileItem;
62-
fileList: FileItem[];
63-
}
64-
6550
export default defineComponent({
6651
components: {
6752
PlusOutlined,
@@ -70,7 +55,7 @@ export default defineComponent({
7055
const previewVisible = ref<boolean>(false);
7156
const previewImage = ref<string | undefined>('');
7257
73-
const fileList = ref<FileItem[]>([
58+
const fileList = ref<UploadProps['fileList']>([
7459
{
7560
uid: '-1',
7661
name: 'image.png',
@@ -105,14 +90,14 @@ export default defineComponent({
10590
const handleCancel = () => {
10691
previewVisible.value = false;
10792
};
108-
const handlePreview = async (file: FileItem) => {
93+
const handlePreview = async (file: UploadProps['fileList'][number]) => {
10994
if (!file.url && !file.preview) {
11095
file.preview = (await getBase64(file.originFileObj)) as string;
11196
}
11297
previewImage.value = file.url || file.preview;
11398
previewVisible.value = true;
11499
};
115-
const handleChange = ({ fileList: newFileList }: FileInfo) => {
100+
const handleChange = ({ fileList: newFileList }: UploadChangeParam) => {
116101
fileList.value = newFileList;
117102
};
118103

components/upload/demo/picture-style.vue

+3-11
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,14 @@ If uploaded file is a picture, the thumbnail can be shown. `IE8/9` do not suppor
4646
<script lang="ts">
4747
import { UploadOutlined } from '@ant-design/icons-vue';
4848
import { defineComponent, ref } from 'vue';
49-
50-
interface FileItem {
51-
uid: string;
52-
name?: string;
53-
status?: string;
54-
response?: string;
55-
url?: string;
56-
thumbUrl?: string;
57-
}
49+
import type { UploadProps } from 'ant-design-vue';
5850
5951
export default defineComponent({
6052
components: {
6153
UploadOutlined,
6254
},
6355
setup() {
64-
const fileList = ref<FileItem[]>([
56+
const fileList = ref<UploadProps['fileList']>([
6557
{
6658
uid: '-1',
6759
name: 'xxx.png',
@@ -77,7 +69,7 @@ export default defineComponent({
7769
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
7870
},
7971
]);
80-
const fileList1 = ref<FileItem[]>([
72+
const fileList1 = ref<UploadProps['fileList']>([
8173
{
8274
uid: '-1',
8375
name: 'xxx.png',

0 commit comments

Comments
 (0)