index.vue
1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<script lang="ts" setup name="SelectImage">
import { FormItemRest, Upload } from 'ant-design-vue'
import { Icon } from '@iconify/vue'
import { computed } from 'vue'
import ImageSelectorModal from './ImageSelectorModal.vue'
import { type ImageSelectorDataType } from '.'
import { useModal } from '@/components/Modal'
import { VariableImageSourceEnum } from '@/enums/datasource'
const props = defineProps<{
value?: ImageSelectorDataType
}>()
const emits = defineEmits(['register', 'update:value'])
const [register, { openModal }] = useModal()
const getImageSrc = computed(() => {
const { libPath = '', imageSource, path } = props.value || {}
if (libPath || path)
return imageSource === VariableImageSourceEnum.LOCAL ? path : libPath
return ''
})
const handleOpenSelectModal = () => {
openModal(true, props.value)
}
const handleSelectComplete = (record: ImageSelectorDataType) => {
emits('update:value', { ...record })
}
</script>
<template>
<Upload class="image-selector rounded-lg" v-bind="$attrs" list-type="picture-card" :open-file-dialog-on-click="false">
<main class="w-full h-full p-2 box-border" @click="handleOpenSelectModal">
<img v-if="getImageSrc" :src="getImageSrc" alt="avatar" class="w-full h-full">
<div v-else class="w-full h-full flex justify-center items-center flex-col rounded-lg">
<Icon icon="ant-design:plus-outlined" class="cursor-pointer svg:text-lg" />
<div class="ant-upload-text">
点击上传
</div>
</div>
</main>
</Upload>
<FormItemRest>
<ImageSelectorModal @register="register" @select-complete="handleSelectComplete" />
</FormItemRest>
</template>
<style lang="less" scoped>
.image-selector.ant-upload-picture-card-wrapper {
width: 100px;
height: 100px;
box-sizing: border-box;
:deep(.ant-upload-wrapper) {
width: 100px;
height: 100px;
}
:deep(.ant-upload.ant-upload-select-picture-card) {
margin: 0;
width: 100px;
height: 100px;
box-sizing: border-box;
}
}
</style>