ldc-cocotte/resources/ts/bo/components/ProductDragAndDropComponent.vue
Jérémy Munsch 44ffc4d34e
feat(DiscountProductDto): add picture property and update fromModel method
feat(ProductDragAndDropComponent): enhance product display with picture and improve layout
2026-10-28 10:02:43 +01:00

243 lines
5.9 KiB
Vue

<template>
<div class="product-drag-drop">
<div class="mb-4">
<fieldset class="border border-secondary-subtle rounded p-2">
<legend class="mb-3 h5">
{{ __('Available products') }}
</legend>
<div class="d-flex justify-content-between align-items-center mb-3">
<div
class="input-group input-group-sm w-auto"
style="max-width: 250px;"
>
<input
v-model="searchQuery"
type="text"
class="form-control"
:placeholder="__('Search...')"
:aria-label="__('Search...')"
>
<button
v-if="searchQuery"
class="btn btn-outline-secondary"
type="button"
:title="__('Clear')"
@click="searchQuery = ''"
>
<i class="fa-solid fa-xmark" />
</button>
</div>
</div>
<draggable
v-model="filteredAvailable"
group="products"
item-key="id"
class="drag-container"
ghost-class="ghost-card"
animation="200"
>
<template #item="{ element }">
<div class="card product-card">
<img
v-if="element.picture"
:src="`/${element.picture}`"
:alt="element.name"
class="card-img-top product-img"
>
<div class="card-body p-2 text-center">
<p
class="card-title m-0 product-name"
:title="element.name"
>
{{ element.name }}
</p>
</div>
</div>
</template>
</draggable>
</fieldset>
<p
v-if="filteredAvailable.length === 0"
class="text-muted small mt-2"
>
<span v-if="searchQuery">{{ __('No product matches your search.') }}</span>
<span v-else>{{ __('All products have been selected.') }}</span>
</p>
</div>
<fieldset class="border border-primary-subtle rounded p-2">
<legend class="mb-3 h5">
{{ __('Selected products') }}
</legend>
<draggable
v-model="selected"
group="products"
item-key="id"
class="drag-container"
ghost-class="ghost-card"
animation="200"
>
<template #item="{ element }">
<div class="card product-card selected-card">
<img
v-if="element.picture"
:src="`/${element.picture}`"
:alt="element.name"
class="card-img-top product-img"
>
<div class="card-body p-2 text-center">
<p
class="card-title m-0 product-name"
:title="element.name"
>
{{ element.name }}
</p>
</div>
</div>
</template>
</draggable>
<small
v-if="selected.length === 0"
class="text-muted small fst-italic text-center d-block"
>
{{ __('Drag products here to select them.') }}
</small>
</fieldset>
<input
v-for="(sel, selIdx) of selected"
:key="selIdx"
type="hidden"
:name="`${name}[]`"
:value="sel.id"
>
</div>
</template>
<script setup lang="ts">
import { computed, onBeforeMount, onMounted, ref, useAttrs } from "vue";
import draggable from "vuedraggable";
import { __ } from "@modules/trans";
defineOptions({ name: "ProductDragAndDrop" });
export interface Product {
id: string | number;
name: string;
picture: string | null;
}
const attrs = useAttrs();
const name = ref<string>("");
const available = ref<Product[]>([]);
const selected = ref<Product[]>([]);
const searchQuery = ref("");
const filteredAvailable = computed({
get() {
if (!searchQuery.value) return available.value;
const lower = searchQuery.value.toLowerCase();
return available.value.filter((p) => p.name.toLowerCase().includes(lower));
},
set(newVal) {
if (!searchQuery.value) {
available.value = newVal;
return;
}
const lower = searchQuery.value.toLowerCase();
const hidden = available.value.filter((p) => !p.name.toLowerCase().includes(lower));
available.value = [...hidden, ...newVal];
}
});
onBeforeMount(() => {
if (!attrs.json) {
throw new Error("this component requires json");
}
});
onMounted(() => {
const json = String(attrs.json ?? "{}");
const data = JSON.parse(json);
name.value = String(data.name);
available.value = data.available ?? [];
selected.value = data.selected ?? [];
});
</script>
<style lang="scss" scoped>
.product-drag-drop {
.drag-container {
display: flex;
overflow-x: auto;
gap: 1rem;
padding-bottom: 1rem;
min-height: 220px;
align-items: stretch;
&::-webkit-scrollbar {
height: 8px;
}
&::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
&::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
}
&::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
}
.product-card {
min-width: 160px;
max-width: 160px;
cursor: grab;
flex-shrink: 0;
transition: box-shadow 0.2s ease;
&:active {
cursor: grabbing;
}
&:hover {
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
&.selected-card {
border-color: var(--bs-primary, #0d6efd);
}
.product-img {
height: 120px;
object-fit: contain;
padding: 0.5rem;
background-color: #fff;
}
.product-name {
font-size: 0.875rem;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}
}
.ghost-card {
opacity: 0.5;
background: #f8f9fa;
border: 2px dashed #adb5bd;
}
}
</style>