339 lines
7.0 KiB
Vue
339 lines
7.0 KiB
Vue
<template>
|
|
<div class="photo-section">
|
|
<div class="t-form__label" v-text="title" />
|
|
<div class="photo-shell" :class="{ 'without-add': !showAdd }">
|
|
<div class="items">
|
|
<div v-if="!displayPhotoList.length" class="empty" v-text="emptyText" />
|
|
<div
|
|
v-for="(item, index) in displayPhotoList"
|
|
:key="`${item.url}-${item.attachType}-${index}`"
|
|
class="item"
|
|
>
|
|
<t-button variant="text" class="preview-trigger" @click="openPhotoViewer(index)">
|
|
<t-image
|
|
class="image"
|
|
:src="item.url"
|
|
:alt="getAttachTypeText(item.attachType)"
|
|
fit="cover"
|
|
/>
|
|
</t-button>
|
|
<div
|
|
class="type-label white glass-black clip-text"
|
|
v-text="getAttachTypeText(item.attachType)"
|
|
/>
|
|
<t-button
|
|
v-if="showRemove && modelValue.length"
|
|
theme="danger"
|
|
class="remove"
|
|
shape="circle"
|
|
size="extra-small"
|
|
@click="removePhoto(index)"
|
|
>
|
|
<t-icon name="close" size="1rem" />
|
|
</t-button>
|
|
</div>
|
|
</div>
|
|
<div v-if="showAdd" class="add-trigger">
|
|
<t-button
|
|
v-if="addActionList.length === 1"
|
|
theme="primary"
|
|
variant="dashed"
|
|
class="add"
|
|
:loading="loading"
|
|
:disabled="loading"
|
|
@click="handleAddAction(addActionList[0])"
|
|
>
|
|
<span v-text="addActionList[0].text" />
|
|
</t-button>
|
|
<t-popover
|
|
v-else
|
|
v-model:visible="addPopoverVisible"
|
|
placement="left"
|
|
theme="light"
|
|
>
|
|
<t-button
|
|
class="add"
|
|
theme="primary"
|
|
variant="dashed"
|
|
:loading="loading"
|
|
:disabled="loading"
|
|
>
|
|
<span v-text="addText" />
|
|
</t-button>
|
|
<template #content>
|
|
<div class="add-actions">
|
|
<t-button
|
|
v-for="(action, index) in addActionList"
|
|
:key="`${action.emit}-${index}`"
|
|
variant="text"
|
|
:theme="action.theme"
|
|
class="add-action"
|
|
:disabled="loading"
|
|
@click="handleAddAction(action)"
|
|
>
|
|
<template #icon v-if="action.icon">
|
|
<t-icon :name="action.icon" />
|
|
</template>
|
|
<span v-text="action.text" />
|
|
</t-button>
|
|
</div>
|
|
</template>
|
|
</t-popover>
|
|
</div>
|
|
</div>
|
|
<t-image-viewer
|
|
v-model:visible="photoViewerVisible"
|
|
:images="photoViewerImages"
|
|
show-index
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import type { ButtonProps } from "tdesign-mobile-vue";
|
|
|
|
interface Props {
|
|
addActions?: readonly AddAction[];
|
|
addText?: string;
|
|
emptyText?: string;
|
|
fallbackImage?: string;
|
|
loading?: boolean;
|
|
modelValue: string[];
|
|
photoItems?: readonly PhotoItem[];
|
|
showAdd?: boolean;
|
|
showRemove?: boolean;
|
|
title?: string;
|
|
}
|
|
|
|
type AddAction = {
|
|
icon?: string;
|
|
emit: string;
|
|
text: string;
|
|
theme?: ButtonProps["theme"];
|
|
};
|
|
|
|
type PhotoItem = {
|
|
attachType?: string;
|
|
url: string;
|
|
};
|
|
|
|
const ATTACH_TYPE_TEXT_MAP: Record<string, string> = {
|
|
PHOTO: "照片",
|
|
POINT_CARD: "积分卡",
|
|
OTHER: "其他文件"
|
|
};
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
addActions: () => [],
|
|
title: "照片列表",
|
|
addText: "拍照",
|
|
emptyText: "暂无照片",
|
|
loading: false,
|
|
showAdd: true,
|
|
showRemove: true
|
|
});
|
|
|
|
type Emits = {
|
|
(event: string): void;
|
|
(event: "update:modelValue", value: string[]): void;
|
|
};
|
|
|
|
const emit = defineEmits<Emits>();
|
|
|
|
const photoViewerVisible = ref(false);
|
|
const photoViewerImages = ref<string[]>([]);
|
|
const addPopoverVisible = ref(false);
|
|
const addActionList = computed<readonly AddAction[]>(() => props.addActions.length ? props.addActions : [{
|
|
emit: "add",
|
|
text: props.addText,
|
|
theme: "default"
|
|
}]);
|
|
const displayPhotoList = computed<PhotoItem[]>(() => {
|
|
if (props.photoItems?.length) {
|
|
return [...props.photoItems];
|
|
}
|
|
if (props.modelValue.length) {
|
|
return props.modelValue.map(url => ({ url, attachType: "PHOTO" }));
|
|
}
|
|
|
|
return props.fallbackImage ? [{ url: props.fallbackImage, attachType: "PHOTO" }] : [];
|
|
});
|
|
|
|
function getAttachTypeText(attachType?: string): string {
|
|
return ATTACH_TYPE_TEXT_MAP[attachType || "PHOTO"] || "其他文件";
|
|
}
|
|
|
|
function handleAddAction(action?: AddAction): void {
|
|
if (!action) {
|
|
return;
|
|
}
|
|
addPopoverVisible.value = false;
|
|
emit(action.emit);
|
|
}
|
|
|
|
function removePhoto(index: number): void {
|
|
if (index < 0 || props.modelValue.length <= index) {
|
|
return;
|
|
}
|
|
|
|
const nextValue = props.modelValue.filter((_, itemIndex) => itemIndex !== index);
|
|
emit("update:modelValue", nextValue);
|
|
}
|
|
|
|
function openPhotoViewer(index: number): void {
|
|
const photoList = [...displayPhotoList.value.slice(index), ...displayPhotoList.value.slice(0, index)];
|
|
photoViewerImages.value = photoList.map(item => item.url);
|
|
photoViewerVisible.value = true;
|
|
}
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.photo-section {
|
|
gap: .375rem;
|
|
width: 100%;
|
|
display: grid;
|
|
|
|
.photo-shell {
|
|
display: grid;
|
|
gap: .75rem;
|
|
grid-template-columns: minmax(0, 1fr) 5.5rem;
|
|
align-items: start;
|
|
|
|
&.without-add {
|
|
grid-template-columns: minmax(0, 1fr);
|
|
}
|
|
|
|
.items {
|
|
gap: .75rem;
|
|
width: 100%;
|
|
height: 100%;
|
|
min-width: 0;
|
|
display: flex;
|
|
overflow-x: auto;
|
|
overflow-y: hidden;
|
|
-webkit-overflow-scrolling: touch;
|
|
scrollbar-width: thin;
|
|
|
|
.empty {
|
|
width: 100%;
|
|
color: #52607a;
|
|
display: flex;
|
|
padding: 1rem 0;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: #f8fbff;
|
|
border-radius: 1rem;
|
|
}
|
|
|
|
.item {
|
|
width: 5.5rem;
|
|
height: 5.5rem;
|
|
flex: 0 0 5.5rem;
|
|
position: relative;
|
|
overflow: hidden;
|
|
border-radius: 1rem;
|
|
|
|
.preview-trigger {
|
|
padding: 0;
|
|
border: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
display: block;
|
|
cursor: pointer;
|
|
background: transparent;
|
|
|
|
.image {
|
|
height: 100%;
|
|
}
|
|
}
|
|
|
|
.type-label {
|
|
width: 100%;
|
|
bottom: 0;
|
|
padding: .125rem .5rem;
|
|
position: absolute;
|
|
overflow: hidden;
|
|
font-size: .875rem;
|
|
text-align: center;
|
|
line-height: 1.2;
|
|
border-radius: 0 0 1rem 1rem;
|
|
pointer-events: none;
|
|
}
|
|
|
|
.remove {
|
|
top: .375rem;
|
|
right: .375rem;
|
|
position: absolute;
|
|
|
|
:deep(.t-button__content) {
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
.add-trigger {
|
|
--td-popover-padding: 0 .5rem;
|
|
width: 5.5rem;
|
|
height: 5.5rem;
|
|
|
|
:deep(.t-popover__content) {
|
|
border-radius: .875rem;
|
|
}
|
|
|
|
.add {
|
|
--td-button-border-radius: .875rem;
|
|
|
|
width: 5.5rem;
|
|
height: 5.5rem;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
.add-actions {
|
|
gap: .25rem;
|
|
display: grid;
|
|
padding: .5rem 0;
|
|
|
|
.add-action {
|
|
--td-button-medium-height: 32px;
|
|
--td-button-medium-padding-horizontal: 10px;
|
|
|
|
gap: .5rem;
|
|
width: 100%;
|
|
position: relative;
|
|
font-weight: unset;
|
|
justify-content: start;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
:global(html.theme-dark) {
|
|
.photo-section {
|
|
.field-title,
|
|
.photo-empty {
|
|
color: #94a3b8;
|
|
}
|
|
|
|
.photo-empty {
|
|
color: #e2e8f0;
|
|
background: rgba(15, 23, 42, .72);
|
|
border-color: rgba(148, 163, 184, .16);
|
|
}
|
|
|
|
.add {
|
|
background: rgba(37, 99, 235, .16);
|
|
border-color: rgba(96, 165, 250, .28);
|
|
|
|
span {
|
|
color: #93c5fd;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</style>
|