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): add abort method #3894

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## NEXT_VERSION

### Features

- `n-upload` adds `abort` method, closes [#3881](https://github.com/tusen-ai/naive-ui/issues/3881).

## 2.41.0

### Breaking Changes
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## NEXT_VERSION

### Features

- `n-upload` 新增 `abort` 方法,关闭 [#3881](https://github.com/tusen-ai/naive-ui/issues/3881)

## 2.41.0

`2025-01-05`
Expand Down
1 change: 1 addition & 0 deletions src/upload/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ interface UploadCustomRequestOptions {
| clear | `() => void` | Clear current upload list. | 2.24.2 |
| openOpenFileDialog | `() => void` | Open the file dialog window. | |
| submit | `(fileId?: string)` | Submit all files with pending status. | |
| abort | `(fileId?: string)` | abort file with pending status, abort all if fileId is not specifield | NEXT_VERSION |

### Upload Slots

Expand Down
20 changes: 12 additions & 8 deletions src/upload/demos/enUS/submit-manually.demo.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<markdown>
# Uncontrolled manually submit
# Uncontrolled manually submit & abort

You can use a `ref` to get a handle on files uploaded, and the `submit` method to submit them when you're ready.
</markdown>
Expand All @@ -21,20 +21,24 @@ export default defineComponent({
},
handleClick() {
uploadRef.value?.submit()
},
handleAbort() {
uploadRef.value?.abort()
}
}
}
})
</script>

<template>
<n-button
:disabled="!fileListLength"
style="margin-bottom: 12px"
@click="handleClick"
>
Upload File
</n-button>
<n-space style="margin-bottom: 12px">
<n-button :disabled="!fileListLength" @click="handleClick">
Upload File
</n-button>
<n-button @click="handleAbort">
Abort Uploading
</n-button>
</n-space>
<n-upload
ref="upload"
action="__HTTP__://www.mocky.io/v2/5e4bafc63100007100d8b70f"
Expand Down
1 change: 1 addition & 0 deletions src/upload/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ interface UploadCustomRequestOptions {
| clear | `() => void` | 清空上传列表 | 2.24.2 |
| openOpenFileDialog | `() => void` | 打开文件选择对话框 | |
| submit | `(fileId?: string)` | 提交当前所有处于 pending 状态的文件 | |
| abort | `(fileId?: string)` | 终止当前处于 uploading 状态的文件,未指定 fileId 则取消所有 | NEXT_VERSION |

### Upload Slots

Expand Down
20 changes: 12 additions & 8 deletions src/upload/demos/zhCN/submit-manually.demo.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<markdown>
# 非受控手动提交
# 非受控手动提交 & 终止上传

你可以使用 submit 方法来进行非受控状态下的手动提交。当然你也可以在受控模式下完全控制提交行为。
</markdown>
Expand All @@ -21,20 +21,24 @@ export default defineComponent({
},
handleClick() {
uploadRef.value?.submit()
},
handleAbort() {
uploadRef.value?.abort()
}
}
}
})
</script>

<template>
<n-button
:disabled="!fileListLength"
style="margin-bottom: 12px"
@click="handleClick"
>
上传文件
</n-button>
<n-space style="margin-bottom: 12px">
<n-button :disabled="!fileListLength" @click="handleClick">
上传文件
</n-button>
<n-button @click="handleAbort">
终止上传
</n-button>
</n-space>
<n-upload
ref="upload"
action="__HTTP__://www.mocky.io/v2/5e4bafc63100007100d8b70f"
Expand Down
45 changes: 44 additions & 1 deletion src/upload/src/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,46 @@ export default defineComponent({
warn('upload', 'File has no corresponding id in current file list.')
}
}
function doRemove(file: UploadSettledFileInfo, index: number): void {
void Promise.resolve(
props.onRemove
? props.onRemove({
file: Object.assign({}, file),
fileList: mergedFileListRef.value,
index
})
: true
).then((result) => {
if (result === false)
return
const fileAfterChange = Object.assign({}, file, {
status: 'removed'
})
xhrMap.delete(file.id)
doChange(fileAfterChange, undefined, {
remove: true
})
})
}
function doAbort(fileId?: string): void {
const fileIdsToBeRemoved: string[] = []
if (fileId === undefined) {
xhrMap.forEach((xhr, key) => {
xhr.abort()
fileIdsToBeRemoved.push(key)
})
}
else {
xhrMap.get(fileId)?.abort()
fileIdsToBeRemoved.push(fileId)
}
const fileList = mergedFileListRef.value.filter(file =>
fileIdsToBeRemoved.includes(file.id)
)
fileList.forEach((file, index) => {
doRemove(file, index)
})
}
function handleFileAddition(
fileAndEntries: FileAndEntry[] | null,
e?: Event
Expand Down Expand Up @@ -708,6 +748,8 @@ export default defineComponent({
xhrMap,
submit,
doChange,
doRemove,
doAbort,
showPreviewButtonRef: toRef(props, 'showPreviewButton'),
onPreviewRef: toRef(props, 'onPreview'),
getFileThumbnailUrlResolver,
Expand Down Expand Up @@ -738,7 +780,8 @@ export default defineComponent({
uncontrolledFileListRef.value = []
},
submit,
openOpenFileDialog
openOpenFileDialog,
abort: doAbort
}

return {
Expand Down
37 changes: 2 additions & 35 deletions src/upload/src/UploadFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ export default defineComponent({
e.preventDefault()
const { file } = props
if (['finished', 'pending', 'error'].includes(file.status)) {
handleRemove(file)
NUpload.doRemove(file, props.index)
}
else if (['uploading'].includes(file.status)) {
handleAbort(file)
NUpload.doAbort(file.id)
}
else {
warn('upload', 'The button clicked type is unknown.')
Expand All @@ -148,33 +148,6 @@ export default defineComponent({
e.preventDefault()
handleDownload(props.file)
}
function handleRemove(file: UploadSettledFileInfo): void {
const {
xhrMap,
doChange,
onRemoveRef: { value: onRemove },
mergedFileListRef: { value: mergedFileList }
} = NUpload
void Promise.resolve(
onRemove
? onRemove({
file: Object.assign({}, file),
fileList: mergedFileList,
index: props.index
})
: true
).then((result) => {
if (result === false)
return
const fileAfterChange = Object.assign({}, file, {
status: 'removed'
})
xhrMap.delete(file.id)
doChange(fileAfterChange, undefined, {
remove: true
})
})
}
function handleDownload(file: UploadSettledFileInfo): void {
const {
onDownloadRef: { value: onDownload }
Expand All @@ -187,12 +160,6 @@ export default defineComponent({
}
})
}
function handleAbort(file: UploadSettledFileInfo): void {
const { xhrMap } = NUpload
const xhr = xhrMap.get(file.id)
xhr?.abort()
handleRemove(Object.assign({}, file))
}
function handlePreviewClick(e: MouseEvent): void {
const {
onPreviewRef: { value: onPreview }
Expand Down
2 changes: 2 additions & 0 deletions src/upload/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export interface UploadInjection {
triggerClassRef: Ref<string | undefined>
triggerStyleRef: Ref<CSSProperties | string | undefined>
doChange: DoChange
doRemove: (file: UploadSettledFileInfo, index: number) => void
doAbort: (fileId?: string) => void
onRender: undefined | (() => void)
submit: (fileId?: string) => void
onRetryRef: Ref<undefined | OnRetry>
Expand Down
1 change: 1 addition & 0 deletions src/upload/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface UploadInst {
openOpenFileDialog: () => void
submit: (fileId?: string) => void
clear: () => void
abort: (fileId?: string) => void
}

export interface UploadFileInfo {
Expand Down