ldc-cocotte/resources/ts/bo/components/BelongsToManyDropdownComponent.vue
Jérémy Munsch bf3e7a93fd
refactor(i18n): harmonize translation keys to English and complete French translations
- Convert hardcoded strings and French translation keys to English keys across Blade templates, Vue components, TypeScript modules, models, and routes
- Complete and synchronize lang/fr.json and lang/en.json translation dictionaries
- Add timezone attribute in lang/fr/validation.php and lang/en/validation.php
- Add missing English lang files for parity: crud.php, models.php, search.php, and vue.php
2026-10-27 09:58:47 +01:00

191 lines
4.6 KiB
Vue

<template>
<div class="belongs-to-many-dropdown">
<Transition name="fade">
<p
v-if="message"
class="message"
>
{{ message }}
</p>
</Transition>
<div class="d-inline-block tagInput w-100">
<Multiselect
v-model="selectedItems"
mode="tags"
:options="allItems"
:searchable="true"
:object="true"
value-prop="id"
label="text"
track-by="text"
:max="max ?? undefined"
:placeholder="placeholder"
:create-option="allowNew"
:no-results-text="trans('No results found.')"
:no-options-text="trans('The list is empty.')"
@select="checkMaxLimit"
@deselect="checkMinLimit"
/>
</div>
<input
v-if="!selectedItems.length"
:name="name"
type="hidden"
value=""
>
<template
v-for="(sItem, index) in selectedItems"
:key="index"
>
<p class="text-danger m-0">
{{ parseValidationInput(`${name}[${String(index)}][name]`, allErrors) }}
</p>
<template v-if="!isNewItem(sItem)">
<input
:name="`${name}[${String(index)}][id]`"
type="hidden"
:value="sItem.id"
>
<input
:name="`${name}[${String(index)}][value]`"
type="hidden"
:value="sItem.id"
>
</template>
<input
:name="`${name}[${String(index)}][name]`"
type="hidden"
:value="sItem.text"
>
<input
:name="`${name}[${String(index)}][label]`"
type="hidden"
:value="sItem.text"
>
<input
:name="`${name}[${String(index)}][title]`"
type="hidden"
:value="sItem.text"
>
</template>
</div>
</template>
<script setup lang="ts">
import Multiselect from "@vueform/multiselect";
import slugify from "slugify";
import { nextTick, onBeforeMount, onMounted, ref, useAttrs } from "vue";
import { parseValidationInput } from "@modules/parseLaravelErrorMessages";
import { trans } from "@modules/trans";
defineOptions({ name: "BelongsToManyDropdownComponent" });
interface CustomTag {
id?: string | number;
name?: string;
label?: string;
title?: string;
text?: string;
slug?: string;
}
const attrs = useAttrs();
const name = ref<string>("");
const selectedItems = ref<Array<CustomTag>>([]);
const allItems = ref<Array<CustomTag>>([]);
const placeholder = ref<string>("");
const min = ref<number>(0);
const max = ref<number | null>(null);
const allErrors = ref<Record<string, Array<string>>>({});
const message = ref<string | null>(null);
const allowNew = ref<boolean>(false);
onBeforeMount(() => {
if (!attrs.json) {
throw new Error("this component requires json");
}
});
onMounted(() => {
const json = String(attrs.json ?? "{}");
const data = JSON.parse(json);
name.value = data.name;
selectedItems.value = parseTags(data.value || []);
allItems.value = parseTags(data.itemsList || []);
placeholder.value = data.placeholder;
const rawAllowNew = data.allowNew ?? attrs.allowNew ?? false;
allowNew.value = rawAllowNew === true || rawAllowNew === "true";
const parsedMin = Number.parseInt((data?.itemLimit ?? [])[0] ?? 0);
min.value = Math.max(0, parsedMin);
const parsedMax = Number.parseInt((data?.itemLimit ?? [])[1] ?? 0);
max.value = parsedMax <= min.value ? null : parsedMax;
allErrors.value = data.errors || {};
});
const parseTags = (items: Array<CustomTag>): Array<CustomTag> => {
return items.map((i: CustomTag) => {
i.text = String(i.label ?? i.name ?? i.title);
i.slug = String(i.slug ?? slugify(i.text));
return i;
});
};
const isNewItem = (item: CustomTag): boolean => {
return !allItems.value.some((i) => String(i.id) === String(item.id));
};
const checkMaxLimit = () => {
if (max.value && selectedItems.value.length >= max.value) {
message.value = trans("Maximum number of elements reached.");
setTimeout(() => (message.value = null), 1500);
}
};
const checkMinLimit = async (removedOption: CustomTag) => {
if (selectedItems.value.length < min.value) {
message.value = trans("Minimum number of elements reached.");
setTimeout(() => (message.value = null), 1500);
await nextTick();
selectedItems.value.push(removedOption);
}
};
</script>
<style lang="scss">
@import '@vueform/multiselect/themes/default.css';
.belongs-to-many-dropdown {
.message {
color: var(--bs-danger);
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
:root {
--ms-tag-bg: var(--bs-blue);
--ms-tag-color: #ffffff;
--ms-radius: 0.375rem;
}
}
</style>