first
This commit is contained in:
100
src/components/UploadArea.vue
Normal file
100
src/components/UploadArea.vue
Normal file
@@ -0,0 +1,100 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
interface Props {
|
||||
hint?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
withDefaults(defineProps<Props>(), {
|
||||
hint: '支持图片/文档/压缩包,大小不超过1G',
|
||||
disabled: false,
|
||||
})
|
||||
|
||||
const emit = defineEmits<{ upload: [file: File] }>()
|
||||
|
||||
const inputRef = ref<HTMLInputElement | null>(null)
|
||||
const dragging = ref(false)
|
||||
|
||||
const MAX_SIZE = 1024 * 1024 * 1024
|
||||
|
||||
function pick(): void {
|
||||
inputRef.value?.click()
|
||||
}
|
||||
|
||||
function handleFiles(list: FileList | null): void {
|
||||
if (!list) return
|
||||
for (const file of Array.from(list)) {
|
||||
if (file.size > MAX_SIZE) {
|
||||
ElMessage.warning(`「${file.name}」超过1G,未添加`)
|
||||
continue
|
||||
}
|
||||
emit('upload', file)
|
||||
}
|
||||
}
|
||||
|
||||
function onDrop(event: DragEvent): void {
|
||||
dragging.value = false
|
||||
handleFiles(event.dataTransfer?.files ?? null)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
class="upload-area"
|
||||
:class="{ 'is-dragging': dragging, 'is-disabled': disabled }"
|
||||
@click="pick"
|
||||
@dragover.prevent="dragging = true"
|
||||
@dragleave.prevent="dragging = false"
|
||||
@drop.prevent="onDrop"
|
||||
>
|
||||
<span class="upload-area__icon">⬆</span>
|
||||
<span class="upload-area__hint text-secondary">{{ hint }}</span>
|
||||
<input
|
||||
ref="inputRef"
|
||||
type="file"
|
||||
multiple
|
||||
class="upload-area__input"
|
||||
:disabled="disabled"
|
||||
@change="handleFiles(($event.target as HTMLInputElement).files)"
|
||||
>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.upload-area {
|
||||
.flex(column, center);
|
||||
gap: @spacing-xs;
|
||||
padding: @spacing-lg;
|
||||
border: 1px dashed @border-color-dark;
|
||||
border-radius: @radius-base;
|
||||
background: @bg-color-light;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.2s, background 0.2s;
|
||||
|
||||
&:hover,
|
||||
&.is-dragging {
|
||||
border-color: @primary-color;
|
||||
background: @primary-light;
|
||||
}
|
||||
|
||||
&.is-disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
&__icon {
|
||||
font-size: @font-size-xl;
|
||||
color: @primary-color;
|
||||
}
|
||||
|
||||
&__hint {
|
||||
font-size: @font-size-sm;
|
||||
}
|
||||
|
||||
&__input {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user