From acc3970782a8c921d1ea0cf52eed1a8dfd8a5cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98xiuxiu-yang=E2=80=99?= <‘1974364190@qq.com’> Date: Mon, 13 Feb 2023 22:56:34 +0800 Subject: [PATCH] feat(component): add upload component --- packages/components/index.ts | 1 + packages/components/src/upload/ajax.ts | 74 +++++++++++++ packages/components/src/upload/index.ts | 8 ++ packages/components/src/upload/props.ts | 20 ++++ packages/components/src/upload/style.scss | 6 + packages/components/src/upload/types.ts | 8 ++ packages/components/src/upload/upload.vue | 129 ++++++++++++++++++++++ packages/lucky-design/src/components.ts | 13 ++- 8 files changed, 253 insertions(+), 6 deletions(-) create mode 100644 packages/components/src/upload/ajax.ts create mode 100644 packages/components/src/upload/index.ts create mode 100644 packages/components/src/upload/props.ts create mode 100644 packages/components/src/upload/style.scss create mode 100644 packages/components/src/upload/types.ts create mode 100644 packages/components/src/upload/upload.vue diff --git a/packages/components/index.ts b/packages/components/index.ts index 03805e3..00bc3b3 100644 --- a/packages/components/index.ts +++ b/packages/components/index.ts @@ -6,3 +6,4 @@ export * from './src/message' export * from './src/drawer' export * from './src/checkbox' export * from './src/icon' +export * from './src/upload' diff --git a/packages/components/src/upload/ajax.ts b/packages/components/src/upload/ajax.ts new file mode 100644 index 0000000..dea7b0f --- /dev/null +++ b/packages/components/src/upload/ajax.ts @@ -0,0 +1,74 @@ +type XMLHttpFN = (ev: ProgressEvent) => void + +export interface UploadRequestOptions { + action: string + method: string + file: File + data?: { [k: string]: string | Blob } + filename: string + withCredentials: boolean + onProgress?: XMLHttpFN + onSuccess: (res: any) => void + onError?: ( + actions: string, + options: UploadRequestOptions, + xhr: XMLHttpRequest + ) => void +} + +const getResponce = (xhr: XMLHttpRequest) => { + const text = xhr.responseText || xhr.response + + try { + return JSON.parse(text) + } + catch (error) { + return text + } +} + +export const httpRequest = (options: UploadRequestOptions): XMLHttpRequest => { + if (typeof XMLHttpRequest === 'undefined') + throw new Error('XMLHttpRequest is undefined') + const { + action, + method, + file, + data, + filename, + withCredentials, + onProgress, + onSuccess, + onError, + } = options + + const xhr = new XMLHttpRequest() + + const formData = new FormData() + + if (data) { + for (const key in data) + formData.append(key, data[key]) + } + + xhr.open(method, action, true) + + xhr.addEventListener('progress', (ev) => { + onProgress && onProgress(ev) + }) + + xhr.addEventListener('error', () => { + onError && onError(action, options, xhr) + }) + + xhr.addEventListener('load', () => { + onSuccess && onSuccess(getResponce(xhr)) + }) + + xhr.withCredentials = withCredentials + + formData.append(filename, file, file.name) + + xhr.send(formData) + return xhr +} diff --git a/packages/components/src/upload/index.ts b/packages/components/src/upload/index.ts new file mode 100644 index 0000000..e70436e --- /dev/null +++ b/packages/components/src/upload/index.ts @@ -0,0 +1,8 @@ +import { withInstall } from '@lucky-design/common' +import Upload from './upload.vue' +import './style.scss' + +export const LUpload = withInstall(Upload) + +export * from './props' +export default LUpload diff --git a/packages/components/src/upload/props.ts b/packages/components/src/upload/props.ts new file mode 100644 index 0000000..4010162 --- /dev/null +++ b/packages/components/src/upload/props.ts @@ -0,0 +1,20 @@ +import type { MethodType } from './types' +import type { UploadRequestOptions } from './ajax' + +export interface IUploadProps { + action: string + data?: { [k: string]: string | Blob } + method?: MethodType + name?: string + multiple?: boolean + withCredentials?: boolean + disabled?: boolean + beforeUpload?: (file: File) => Promise + onProgress?: (et: ProgressEvent) => void + onSuccess?: (res: any) => void + onError?: ( + actions: string, + options: UploadRequestOptions, + xhr: XMLHttpRequest + ) => void +} diff --git a/packages/components/src/upload/style.scss b/packages/components/src/upload/style.scss new file mode 100644 index 0000000..0376293 --- /dev/null +++ b/packages/components/src/upload/style.scss @@ -0,0 +1,6 @@ +.ld-upload { + @apply cursor-pointer flex-inline justify-center items-center; + .ld-upload_input { + @apply hidden; + } +} diff --git a/packages/components/src/upload/types.ts b/packages/components/src/upload/types.ts new file mode 100644 index 0000000..c3fa81b --- /dev/null +++ b/packages/components/src/upload/types.ts @@ -0,0 +1,8 @@ +export type MethodType = + | 'post' + | 'get' + | 'delete' + | 'head' + | 'option' + | 'patch' + | 'put' diff --git a/packages/components/src/upload/upload.vue b/packages/components/src/upload/upload.vue new file mode 100644 index 0000000..41f45ee --- /dev/null +++ b/packages/components/src/upload/upload.vue @@ -0,0 +1,129 @@ + + + diff --git a/packages/lucky-design/src/components.ts b/packages/lucky-design/src/components.ts index ec1ab30..cf0f5f4 100644 --- a/packages/lucky-design/src/components.ts +++ b/packages/lucky-design/src/components.ts @@ -1,8 +1,9 @@ -import { LButton, LDrawer, LMessage, LTree } from '@lucky-design/components' - -export const plugins = [ +import { LButton, - LTree, - LMessage, LDrawer, -] as const + LMessage, + LTree, + LUpload, +} from '@lucky-design/components' + +export const plugins = [LButton, LTree, LMessage, LDrawer, LUpload] as const