refactor(components): [upload] improve code and typings (#6762)

This commit is contained in:
三咲智子 2022-03-23 18:36:31 +08:00 committed by GitHub
parent 8ae7acc68e
commit a9e579fef5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 33 deletions

View File

@ -4,6 +4,7 @@ import { uploadBaseProps } from './upload'
import type { ExtractPropTypes } from 'vue'
import type {
UploadUserFile,
UploadRawFile,
UploadFile,
UploadProgressEvent,
@ -17,7 +18,7 @@ export const uploadContentProps = buildProps({
// override
fileList: {
type: definePropType<UploadFile[]>(Array),
type: definePropType<UploadUserFile[]>(Array),
default: () => mutable([] as const),
},

View File

@ -75,7 +75,7 @@ export interface UploadHooks {
uploadFile: UploadFile,
uploadFiles: UploadFiles
) => void
onExceed: (files: File[], uploadFiles: UploadFiles) => void
onExceed: (files: File[], uploadFiles: UploadUserFile[]) => void
}
export const uploadBaseProps = buildProps({

View File

@ -64,7 +64,7 @@ import { useDisabled } from '@element-plus/hooks'
import UploadList from './upload-list.vue'
import UploadContent from './upload-content.vue'
import { useHandlers } from './use-handlers'
import { uploadProps, type UploadFiles } from './upload'
import { uploadProps } from './upload'
import type {
UploadContentInstance,
UploadContentProps,
@ -75,6 +75,8 @@ defineOptions({
})
const props = defineProps(uploadProps)
const slots = useSlots()
const disabled = useDisabled()
const uploadRef = shallowRef<UploadContentInstance>()
@ -92,6 +94,16 @@ const {
const isPictureCard = computed(() => props.listType === 'picture-card')
// did not use `defineComponent` for performance
const uploadContentProps = computed<UploadContentProps>(() => ({
...props,
onStart: handleStart,
onProgress: handleProgress,
onSuccess: handleSuccess,
onError: handleError,
onRemove: handleRemove,
}))
onBeforeUnmount(() => {
uploadFiles.value.forEach(({ url }) => {
if (url?.startsWith('blob:')) URL.revokeObjectURL(url)
@ -114,34 +126,4 @@ defineExpose({
/** @description remove the file manually */
handleRemove,
})
const slots = useSlots()
// did not use `defineComponent` for performance
const uploadContentProps = computed<UploadContentProps>(() => ({
type: props.type,
drag: props.drag,
action: props.action,
multiple: props.multiple,
withCredentials: props.withCredentials,
headers: props.headers,
method: props.method,
name: props.name,
data: props.data,
accept: props.accept,
autoUpload: props.autoUpload,
listType: props.listType,
disabled: props.disabled,
limit: props.limit,
fileList: props.fileList as UploadFiles,
showFileList: props.showFileList,
httpRequest: props.httpRequest,
beforeUpload: props.beforeUpload,
onExceed: props.onExceed,
onStart: handleStart,
onProgress: handleProgress,
onSuccess: handleSuccess,
onError: handleError,
onRemove: handleRemove,
}))
</script>