Files
cz-web/src/components/UploadArea.vue
2026-10-01 17:24:57 +08:00

101 lines
2.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<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;
min-height: 69px;
padding: @spacing-base;
border: none;
border-radius: @radius-sm;
background: @demand-upload-bg;
cursor: pointer;
transition: background 0.2s;
&:hover,
&.is-dragging {
background: darken(@demand-upload-bg, 5%);
}
&.is-disabled {
opacity: 0.5;
cursor: not-allowed;
}
&__icon {
font-size: @font-size-lg;
color: @text-color-disabled;
}
&__hint {
font-size: @font-size-sm;
}
&__input {
display: none;
}
}
</style>