Skip to content

Commit

Permalink
fix(upload): change the timing of doUpdateFileList call
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jun 23, 2021
1 parent 8289753 commit e806865
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/upload/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ default-files
| show-retry-button | `boolean` | `true` | Whether to show retry button (at file error status). |
| with-credentials | `boolean` | `false` | If cookie attached. |
| on-change | `(options: { file: UploadFile, fileList: Array<UploadFile>, event?: Event }) => void` | `() => {}` | The callback of status change of the component. Any file status change would fire the callback. |
| on-update:file-list | `(fileList: UploadFile[]) => void` | `undefined` | Callback function triggered on checked fileList changes. |
| on-finish | `(options: { file: UploadFile }) => UploadFile \| void` | `({ file }) => file` | The callback of file upload finish. You can modify the UploadFile or retun a new UploadFile. |
| on-remove | `(options: { file: UploadFile, fileList: Array<UploadFile> }) => boolean \| Promise<boolean> \| any` | `() => true` | The callback of file removal. Return false, promise resolve false or promise reject will cancel this removal. |
| on-update:file-list | `(fileList: Array<UploadFile>) => void` | `undefined` | Callback function triggered on checked fileList changes. |

### UploadFile Type

Expand Down
2 changes: 1 addition & 1 deletion src/upload/demos/zhCN/controlled.demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default defineComponent({
}
},
handleFileListChange (value) {
message.info("是的, file-list 的值变了")
message.info("是的file-list 的值变了")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/upload/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ default-files
| with-credentials | `boolean` | `false` | 是否携带 Cookie |
| on-change | `(options: { file: UploadFile, fileList: Array<UploadFile>, event?: Event }) => void` | `() => {}` | 组件状态变化的回调,组件的任何文件状态变化都会触发回调 |
| on-finish | `(options: { file: UploadFile }) => UploadFile \| void` | `({ file }) => file` | 文件上传结束的回调,可以修改传入的 UploadFile 或者返回一个新的 UploadFile |
| on-update:file-list | `(fileList: UploadFile[]) => void` | `undefined` | 当 file-list 改变时触发的回调函数 |
| on-remove | `(options: { file: UploadFile, fileList: Array<UploadFile> }) => boolean \| Promise<boolean> \| any` | `() => true` | 文件移除的回调,返回 false 或者 promise resolve false 或者 promise reject 会不执行这次删除 |
| on-update:file-list | `(fileList: Array<UploadFile>) => void` | `undefined` | 当 file-list 改变时触发的回调函数 |

### UploadFile Type

Expand Down
21 changes: 9 additions & 12 deletions src/upload/src/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
} from './interface'
import { useMergedState } from 'vooks'
import { uploadDraggerKey } from './UploadDragger'
import { zip } from 'lodash'

/**
* fils status ['pending', 'uploading', 'finished', 'removed', 'error']
Expand Down Expand Up @@ -302,14 +301,13 @@ export default defineComponent({
// May have bug! set to null?
target.value = ''
}
function handleFileListChange (): void {
const fileListAfterChange = mergedFileListRef.value
const {
'onUpdate:fileList': _onUpdateFileList,
onUpdateFileList,
} = props
if (_onUpdateFileList) call(_onUpdateFileList, fileListAfterChange)
if (onUpdateFileList) call(onUpdateFileList, fileListAfterChange)
function doUpdateFileList (files: FileInfo[]): void {
const {
'onUpdate:fileList': _onUpdateFileList,
onUpdateFileList,
} = props
if (_onUpdateFileList) call(_onUpdateFileList, files)
if (onUpdateFileList) call(onUpdateFileList, files)
}
function handleFileAddition (files: FileList | null, e?: Event): void {
if (!files) return
Expand All @@ -329,7 +327,6 @@ export default defineComponent({
if (props.defaultUpload) {
submit()
}
handleFileListChange()
}
function submit (fileId?: string): void {
const {
Expand Down Expand Up @@ -398,6 +395,7 @@ export default defineComponent({
})
}
uncontrolledFileListRef.value = fileListAfterChange
doUpdateFileList(fileListAfterChange)
} else if (__DEV__) {
warn('upload', 'File has no corresponding id in current file list.')
}
Expand All @@ -414,8 +412,7 @@ export default defineComponent({
mergedFileListRef: mergedFileListRef,
XhrMap,
submit,
doChange,
handleFileListChange
doChange
})
return {
mergedClsPrefix: mergedClsPrefixRef,
Expand Down
5 changes: 1 addition & 4 deletions src/upload/src/UploadFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ export default defineComponent({
XhrMap,
doChange,
onRemoveRef: { value: onRemove },
mergedFileListRef: { value: mergedFileList },
handleFileListChange

mergedFileListRef: { value: mergedFileList }
} = NUpload
void Promise.resolve(
onRemove
Expand All @@ -105,7 +103,6 @@ export default defineComponent({
doChange(fileAfterChange, undefined, {
remove: true
})
handleFileListChange()
})
}
function handleDownload (file: FileInfo): void {
Expand Down
3 changes: 1 addition & 2 deletions src/upload/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export interface UploadInjection {
onDownloadRef: Ref<OnDownload | undefined>
XhrMap: Map<string, XMLHttpRequest>
submit: (fileId?: string) => void
doChange: DoChange,
handleFileListChange: () => void
doChange: DoChange
}

export const uploadInjectionKey: InjectionKey<UploadInjection> =
Expand Down

0 comments on commit e806865

Please sign in to comment.