Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(upload): 新增 clickUpload 事件 #1195

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/upload/demos/multiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
:on-success="onSuccess"
:on-remove="onRemove"
:on-select-change="onSelectChange"
:on-click-upload="onClickUpload"
>
</t-upload>
</div>
Expand Down Expand Up @@ -47,6 +48,9 @@ const onRemove = ({ index, file, e }: UploadRemoveContext) => {
const onSelectChange = (files: Array<UploadFile>) => {
console.log('====onSelectChange', files);
};
const onClickUpload = ({ e }: { e: MouseEvent }) => {
console.log('====onClickUpload', e);
};
const action = 'https://service-bv448zsw-1257786608.gz.apigw.tencentcs.com/api/upload-demo';
const files = ref([
{
Expand Down
2 changes: 2 additions & 0 deletions src/upload/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ export default {
onValidate: Function as PropType<TdUploadProps['onValidate']>,
/** 待上传文件列表发生变化时触发。`context.files` 表示事件参数为待上传文件,`context.trigger` 引起此次变化的触发来源 */
onWaitingUploadFilesChange: Function as PropType<TdUploadProps['onWaitingUploadFilesChange']>,
/** 点击上传区域时触发。TS 类型:`(context: { e: MouseEvent })` */
onClickUpload: Function as PropType<TdUploadProps['onClickUpload']>,
yaogengzhu marked this conversation as resolved.
Show resolved Hide resolved
};
4 changes: 4 additions & 0 deletions src/upload/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ export interface TdUploadProps<T extends UploadFile = UploadFile> {
files: Array<UploadFile>;
trigger: 'validate' | 'remove' | 'uploaded';
}) => void;
/**
* 点击上传区域时触发。TS 类型:`(context: { e: MouseEvent })`
*/
onClickUpload?: (context: { e: MouseEvent }) => void;
}

export interface UploadFile extends PlainObject {
Expand Down
3 changes: 3 additions & 0 deletions src/upload/upload.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ onRemove | Function | | Typescript:`(context: UploadRemoveContext) => void`<b
onSelectChange | Function | | Typescript:`(files: File[], context: UploadSelectChangeContext) => void`<br/>trigger after file choose and before upload。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface UploadSelectChangeContext { currentSelectedFiles: UploadFile[] }`<br/> | N
onSuccess | Function | | Typescript:`(context: SuccessContext) => void`<br/>trigger on all files uploaded successfully。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface SuccessContext { e?: ProgressEvent; file?: UploadFile; fileList?: UploadFile[]; currentFiles?: UploadFile[]; response?: any; results?: SuccessContext[]; XMLHttpRequest?: XMLHttpRequest }`<br/> | N
onValidate | Function | | Typescript:`(context: { type: UploadValidateType, files: UploadFile[] }) => void`<br/>trigger on length over limit, or trigger on file size over limit。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`type UploadValidateType = 'FILE_OVER_SIZE_LIMIT' \| 'FILES_OVER_LENGTH_LIMIT' \| 'FILTER_FILE_SAME_NAME' \| 'BEFORE_ALL_FILES_UPLOAD' \| 'CUSTOM_BEFORE_UPLOAD'`<br/> | N
onClickUpload | Function | - | trigger on upload area click. Typescript:`(context: { e: MouseEvent })` | N


### Upload Events

Expand All @@ -51,6 +53,7 @@ remove | `(context: UploadRemoveContext)` | trigger on file removed。[see more
select-change | `(files: File[], context: UploadSelectChangeContext)` | trigger after file choose and before upload。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface UploadSelectChangeContext { currentSelectedFiles: UploadFile[] }`<br/>
success | `(context: SuccessContext)` | trigger on all files uploaded successfully。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface SuccessContext { e?: ProgressEvent; file?: UploadFile; fileList?: UploadFile[]; currentFiles?: UploadFile[]; response?: any; results?: SuccessContext[]; XMLHttpRequest?: XMLHttpRequest }`<br/>
validate | `(context: { type: UploadValidateType, files: UploadFile[] })` | trigger on length over limit, or trigger on file size over limit。[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`type UploadValidateType = 'FILE_OVER_SIZE_LIMIT' \| 'FILES_OVER_LENGTH_LIMIT' \| 'FILTER_FILE_SAME_NAME' \| 'BEFORE_ALL_FILES_UPLOAD' \| 'CUSTOM_BEFORE_UPLOAD'`<br/>
clickUpload | `(context: { e: MouseEvent })` | trigger on upload area click.

### UploadFile

Expand Down
2 changes: 2 additions & 0 deletions src/upload/upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ onRemove | Function | | TS 类型:`(context: UploadRemoveContext) => void`<br
onSelectChange | Function | | TS 类型:`(files: File[], context: UploadSelectChangeContext) => void`<br/>选择文件或图片之后,上传之前,触发该事件。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface UploadSelectChangeContext { currentSelectedFiles: UploadFile[] }`<br/> | N
onSuccess | Function | | TS 类型:`(context: SuccessContext) => void`<br/>上传成功后触发。<br/>`context.currentFiles` 表示当次请求上传的文件(无论成功或失败),`context.fileList` 表示上传成功后的文件,`context.response` 表示上传请求的返回数据。<br/>`context.results` 表示单次选择全部文件上传成功后的响应结果,可以在这个字段存在时提醒用户上传成功或失败。<br />。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface SuccessContext { e?: ProgressEvent; file?: UploadFile; fileList?: UploadFile[]; currentFiles?: UploadFile[]; response?: any; results?: SuccessContext[]; XMLHttpRequest?: XMLHttpRequest }`<br/> | N
onValidate | Function | | TS 类型:`(context: { type: UploadValidateType, files: UploadFile[] }) => void`<br/>文件上传校验结束事件,文件数量超出、文件大小超出限制、文件同名、`beforeAllFilesUpload` 返回值为假、`beforeUpload` 返回值为假等场景会触发。<br/>注意:如果设置允许上传同名文件,即 `allowUploadDuplicateFile=true`,则不会因为文件重名触发该事件。<br/>结合 `status` 和 `tips` 可以在组件中呈现不同类型的错误(或告警)提示。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`type UploadValidateType = 'FILE_OVER_SIZE_LIMIT' \| 'FILES_OVER_LENGTH_LIMIT' \| 'FILTER_FILE_SAME_NAME' \| 'BEFORE_ALL_FILES_UPLOAD' \| 'CUSTOM_BEFORE_UPLOAD'`<br/> | N
onClickUpload | Function | - | 点击上传区域时触发。TS 类型:`(context: { e: MouseEvent })` | N

### Upload Events

Expand All @@ -51,6 +52,7 @@ remove | `(context: UploadRemoveContext)` | 移除文件时触发。[详细类
select-change | `(files: File[], context: UploadSelectChangeContext)` | 选择文件或图片之后,上传之前,触发该事件。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface UploadSelectChangeContext { currentSelectedFiles: UploadFile[] }`<br/>
success | `(context: SuccessContext)` | 上传成功后触发。<br/>`context.currentFiles` 表示当次请求上传的文件(无论成功或失败),`context.fileList` 表示上传成功后的文件,`context.response` 表示上传请求的返回数据。<br/>`context.results` 表示单次选择全部文件上传成功后的响应结果,可以在这个字段存在时提醒用户上传成功或失败。<br />。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`interface SuccessContext { e?: ProgressEvent; file?: UploadFile; fileList?: UploadFile[]; currentFiles?: UploadFile[]; response?: any; results?: SuccessContext[]; XMLHttpRequest?: XMLHttpRequest }`<br/>
validate | `(context: { type: UploadValidateType, files: UploadFile[] })` | 文件上传校验结束事件,文件数量超出、文件大小超出限制、文件同名、`beforeAllFilesUpload` 返回值为假、`beforeUpload` 返回值为假等场景会触发。<br/>注意:如果设置允许上传同名文件,即 `allowUploadDuplicateFile=true`,则不会因为文件重名触发该事件。<br/>结合 `status` 和 `tips` 可以在组件中呈现不同类型的错误(或告警)提示。[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/upload/type.ts)。<br/>`type UploadValidateType = 'FILE_OVER_SIZE_LIMIT' \| 'FILES_OVER_LENGTH_LIMIT' \| 'FILTER_FILE_SAME_NAME' \| 'BEFORE_ALL_FILES_UPLOAD' \| 'CUSTOM_BEFORE_UPLOAD'`<br/>
clickUpload | `(context: { e: MouseEvent })` | 点击上传区域时触发。

### UploadFile

Expand Down
6 changes: 5 additions & 1 deletion src/upload/upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default defineComponent({
'success',
'select-change',
'validate',
'click-upload',
],
setup(props) {
const {
Expand Down Expand Up @@ -116,10 +117,13 @@ export default defineComponent({
});
};

const triggerUpload = () => {
const triggerUpload = (e: MouseEvent) => {
const input = inputRef.value as HTMLInputElement;
if (disabled?.value) return;
input.click();
props.onClickUpload?.({
e,
});
};

const previewImgs = computed(() => {
Expand Down
Loading