| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- <!--
- Composant de sélection hiérarchique à plusieurs niveaux permettant de naviguer
- et sélectionner des éléments organisés en catégories et sous-catégories.
- ## Exemple d'utilisation
- ```vue
- <TreeSelect
- v-model="selectedValues"
- :items="hierarchicalItems"
- :max-visible-chips="3"
- label="Sélectionner des éléments"
- />
- ```
- -->
- <template>
- <v-select
- :model-value="modelValue"
- v-bind="$attrs"
- :items="flattenedItems"
- item-title="label"
- item-value="value"
- multiple
- chips
- closable-chips
- :menu-props="{ maxHeight: 400 }"
- @update:menu="onMenuUpdate"
- @update:model-value="$emit('update:modelValue', $event)"
- >
- <template #prepend-item>
- <!-- Champs de recherche textuelle -->
- <v-text-field
- v-model="searchText"
- density="compact"
- hide-details
- :placeholder="$t('search') + '...'"
- prepend-inner-icon="fas fa-magnifying-glass"
- variant="outlined"
- clearable
- class="mx-2 my-2"
- @click.stop
- @input="onSearchInputDebounced"
- @click:clear.stop="onSearchClear"
- />
- <v-divider class="mt-2" />
- </template>
- <template #selection="{ item, index }">
- <v-chip
- v-if="maxVisibleChips && index < maxVisibleChips"
- :key="item.raw.value"
- closable
- @click:close="removeItem(item.raw.value!)"
- >
- <!-- Always prioritize the mapping for consistent labels, fall back to item label if available -->
- {{
- selectedItemsMap[item.raw.value] ||
- (item.raw.label && item.raw.label !== item.raw.value ? item.raw.label : item.raw.value)
- }}
- </v-chip>
- <span
- v-if="
- maxVisibleChips &&
- index === maxVisibleChips &&
- modelValue.length > maxVisibleChips
- "
- class="text-grey text-caption align-self-center"
- >
- (+{{ modelValue.length - maxVisibleChips }} {{ $t('others') }})
- </span>
- </template>
- <template #item="{ item }">
- <template v-if="item.raw.type === 'category'">
- <v-list-item
- :ripple="false"
- :class="{
- 'v-list-item--active': expandedCategories.has(item.raw.id),
- }"
- @click.stop="toggleCategory(item.raw.id)"
- >
- <template #prepend>
- <v-icon
- :icon="
- 'fas ' +
- (expandedCategories.has(item.raw.id)
- ? 'fa-angle-down'
- : 'fa-angle-right')
- "
- size="small"
- />
- </template>
- <v-list-item-title class="font-weight-medium">
- {{ item.raw.label }}
- </v-list-item-title>
- </v-list-item>
- </template>
- <template v-else-if="item.raw.type === 'subcategory'">
- <v-list-item
- :ripple="false"
- :class="{
- 'v-list-item--active': expandedSubcategories.has(item.raw.id),
- 'pl-8': true,
- }"
- @click.stop="toggleSubcategory(item.raw.id)"
- >
- <template #prepend>
- <v-icon
- :icon="
- 'fas ' +
- (expandedSubcategories.has(item.raw.id)
- ? 'fa-angle-down'
- : 'fa-angle-right')
- "
- size="small"
- />
- </template>
- <v-list-item-title>
- {{ item.raw.label }}
- </v-list-item-title>
- </v-list-item>
- </template>
- <template v-else>
- <v-list-item
- :active="modelValue.includes(item.raw.value!)"
- :class="{
- 'd-flex': true,
- 'pl-12': item.raw.level === 2,
- 'pl-8': item.raw.level === 1,
- }"
- @click="toggleItem(item.raw.value!)"
- >
- <template #prepend>
- <v-checkbox
- :model-value="modelValue.includes(item.raw.value!)"
- color="primary"
- :hide-details="true"
- @click.stop="toggleItem(item.raw.value!)"
- />
- </template>
- <v-list-item-title>
- {{ item.raw.label }}
- </v-list-item-title>
- </v-list-item>
- </template>
- </template>
- </v-select>
- </template>
- <script setup lang="ts">
- import StringUtils from '~/services/utils/stringUtils'
- import _ from 'lodash'
- interface SelectItem {
- id: string
- label: string
- normalizedLabel?: string
- value?: string
- type: 'category' | 'subcategory' | 'item'
- parentId?: string
- level: number
- }
- const props = defineProps({
- modelValue: {
- type: Array as PropType<string[]>,
- required: true,
- },
- items: {
- type: Array as PropType<SelectItem[]>,
- required: true,
- },
- maxVisibleChips: {
- type: Number,
- required: false,
- default: null,
- },
- })
- /**
- * A computed property that normalizes the labels of all items upfront.
- * This avoids having to normalize labels during each search operation.
- */
- const normalizedItems = computed(() => {
- return props.items.map(item => ({
- ...item,
- normalizedLabel: StringUtils.normalize(item.label)
- }))
- })
- const emit = defineEmits(['update:modelValue'])
- const expandedCategories: Ref<Set<string>> = ref(new Set())
- const expandedSubcategories: Ref<Set<string>> = ref(new Set())
- const searchText: Ref<string> = ref('')
- /**
- * A callback function that is triggered when the menu's open state is updated.
- */
- const onMenuUpdate = (isOpen: boolean) => {
- // Réinitialiser la recherche quand le menu se ferme
- if (!isOpen && searchText.value) {
- searchText.value = ''
- onSearchInput()
- }
- }
- /**
- * Toggles the expanded state of a given category. If the category is currently
- * expanded, it will collapse the category and also collapse its subcategories.
- * If the category is not expanded, it will expand the category.
- *
- * @param {string} categoryId - The unique identifier of the category to toggle.
- */
- const toggleCategory = (categoryId: string) => {
- if (expandedCategories.value.has(categoryId)) {
- expandedCategories.value.delete(categoryId)
- // Fermer aussi les sous-catégories
- const subcategories = normalizedItems.value.filter(
- (i) => i.parentId === categoryId && i.type === 'subcategory',
- )
- subcategories.forEach((sub) => {
- expandedSubcategories.value.delete(sub.id)
- })
- } else {
- expandedCategories.value.add(categoryId)
- }
- }
- /**
- * Toggles the expansion state of a subcategory.
- *
- * @param {string} subcategoryId - The unique identifier of the subcategory to be toggled.
- */
- const toggleSubcategory = (subcategoryId: string) => {
- if (expandedSubcategories.value.has(subcategoryId)) {
- expandedSubcategories.value.delete(subcategoryId)
- } else {
- expandedSubcategories.value.add(subcategoryId)
- }
- }
- /**
- * A function that toggles the inclusion of a specific value in
- * the selected items list.
- *
- * @param {string} value - The value to toggle in the selected items list.
- */
- const toggleItem = (value: string) => {
- const currentSelection = [...props.modelValue]
- const index = currentSelection.indexOf(value)
- if (index > -1) {
- currentSelection.splice(index, 1)
- } else {
- currentSelection.push(value)
- }
- emit('update:modelValue', currentSelection)
- }
- /**
- * Removes the specified item from the model value and emits an update event.
- *
- * @param {string} value - The item to be removed from the model value.
- * @emits update:modelValue - A custom event emitted with the updated model value
- * after the specified item has been removed.
- */
- const removeItem = (value: string) => {
- emit(
- 'update:modelValue',
- props.modelValue.filter((item) => item !== value),
- )
- }
- /**
- * Fonction appellée lorsque l'input de recherche textuelle est modifié
- */
- const onSearchInput = () => {
- // Réinitialiser les états d'expansion dans tous les cas
- expandedCategories.value.clear()
- expandedSubcategories.value.clear()
- if (searchText.value.trim()) {
- // Trouver tous les éléments qui correspondent à la recherche
- const matchingItems = normalizedItems.value.filter(
- (item) =>
- item.type === 'item' && item.level === 2 && itemMatchesSearch(item),
- )
- // Pour chaque élément correspondant, ajouter ses parents aux ensembles d'expansion
- for (const item of matchingItems) {
- // Trouver et ajouter la sous-catégorie parente
- const subcategory = normalizedItems.value.find((i) => i.id === item.parentId)
- if (subcategory) {
- expandedSubcategories.value.add(subcategory.id)
- // Trouver et ajouter la catégorie parente
- const category = normalizedItems.value.find((i) => i.id === subcategory.parentId)
- if (category) {
- expandedCategories.value.add(category.id)
- }
- }
- }
- }
- }
- const onSearchInputDebounced = _.debounce(onSearchInput, 200)
- const onSearchClear = () => {
- searchText.value = ''
- onSearchInput()
- }
- /**
- * Checks if any word in the normalized text starts with the normalized search term.
- *
- * @param {string} normalizedText - The normalized text to check.
- * @param {string} normalizedSearch - The normalized search term.
- * @returns {boolean} `true` if any word in the text starts with the search term; otherwise, `false`.
- */
- const anyWordStartsWith = (
- normalizedText: string,
- normalizedSearch: string,
- ): boolean => {
- if (normalizedText.indexOf(normalizedSearch) === 0) return true
- const spaceIndex = normalizedText.indexOf(' ')
- if (spaceIndex === -1) return false
- return normalizedText
- .split(' ')
- .some(word => word.startsWith(normalizedSearch))
- }
- /**
- * Determines if a given item matches the current search text by checking its normalized label
- * and, for certain items, the normalized labels of its parent elements.
- *
- * The search text is normalized using `StringUtils.normalize` before comparison.
- * If no search text is provided, the item matches by default.
- *
- * For items of type `item` at level 2, the function checks:
- * - The normalized label of the item itself
- * - The normalized label of its parent subcategory
- * - The normalized label of the grandparent category (if applicable)
- *
- * For all other item types, only the item's normalized label is checked.
- *
- * The matching is done by checking if any word in the normalized label starts with the normalized search text.
- *
- * @param {SelectItem} item - The item to evaluate against the search text.
- * @returns {boolean} `true` if the item or its relevant parent(s) match the search text; otherwise, `false`.
- */
- const itemMatchesSearch = (item: SelectItem): boolean => {
- if (!searchText.value) return true
- const normalizedSearch = StringUtils.normalize(searchText.value)
- // Find the item with normalized label from our computed property
- const itemWithNormalizedLabel = normalizedItems.value.find(i => i.id === item.id)
- if (!itemWithNormalizedLabel) return false
- // Si c'est un élément de niveau 2, vérifier son label et les labels de ses parents
- if (item.type === 'item' && item.level === 2) {
- // Vérifier le label de l'élément
- if (anyWordStartsWith(itemWithNormalizedLabel.normalizedLabel!, normalizedSearch))
- return true
- // Trouver et vérifier le label de la sous-catégorie parente
- const subcategory = normalizedItems.value.find((i) => i.id === item.parentId)
- if (
- subcategory &&
- anyWordStartsWith(
- subcategory.normalizedLabel!,
- normalizedSearch,
- )
- )
- return true
- // Trouver et vérifier le label de la catégorie parente
- if (subcategory && subcategory.parentId) {
- const category = normalizedItems.value.find((i) => i.id === subcategory.parentId)
- if (
- category &&
- anyWordStartsWith(
- category.normalizedLabel!,
- normalizedSearch,
- )
- )
- return true
- }
- return false
- }
- // Pour les autres types d'éléments, vérifier simplement leur label
- return anyWordStartsWith(itemWithNormalizedLabel.normalizedLabel!, normalizedSearch)
- }
- /**
- * Filtre les éléments de niveau 2 qui correspondent au texte de recherche.
- *
- * @returns {SelectItem[]} Les éléments de niveau 2 qui correspondent à la recherche.
- */
- const findMatchingLevel2Items = (): SelectItem[] => {
- return normalizedItems.value.filter(
- (item) =>
- item.type === 'item' && item.level === 2 && itemMatchesSearch(item),
- )
- }
- /**
- * Construit une liste hiérarchique d'éléments basée sur les résultats de recherche.
- * Pour chaque élément correspondant, ajoute sa hiérarchie complète (catégorie et sous-catégorie).
- *
- * @param {SelectItem[]} matchingItems - Les éléments correspondant à la recherche.
- * @returns {SelectItem[]} Liste hiérarchique incluant les éléments et leurs parents.
- */
- const buildSearchResultsList = (matchingItems: SelectItem[]): SelectItem[] => {
- const result: SelectItem[] = []
- const addedCategoryIds = new Set<string>()
- const addedSubcategoryIds = new Set<string>()
- for (const item of matchingItems) {
- // Trouver la sous-catégorie parente
- const subcategory = normalizedItems.value.find((i) => i.id === item.parentId)
- if (!subcategory) continue
- // Trouver la catégorie parente
- const category = normalizedItems.value.find((i) => i.id === subcategory.parentId)
- if (!category) continue
- // Ajouter la catégorie si elle n'est pas déjà présente
- if (!addedCategoryIds.has(category.id)) {
- result.push(category)
- addedCategoryIds.add(category.id)
- expandedCategories.value.add(category.id)
- }
- // Ajouter la sous-catégorie si elle n'est pas déjà présente
- if (!addedSubcategoryIds.has(subcategory.id)) {
- result.push(subcategory)
- addedSubcategoryIds.add(subcategory.id)
- expandedSubcategories.value.add(subcategory.id)
- }
- // Ajouter l'élément
- result.push(item)
- }
- return result
- }
- /**
- * Traite récursivement les éléments pour construire une liste hiérarchique
- * basée sur l'état d'expansion des catégories et sous-catégories.
- *
- * @param {SelectItem[]} items - Les éléments à traiter.
- * @param {SelectItem[]} result - Le tableau résultat à remplir.
- * @param {boolean} parentExpanded - Indique si le parent est développé.
- */
- const processItemsRecursively = (
- items: SelectItem[],
- result: SelectItem[],
- parentExpanded = true,
- ): void => {
- for (const item of items) {
- if (item.type === 'category') {
- result.push(item)
- if (expandedCategories.value.has(item.id)) {
- const subcategories = normalizedItems.value.filter(
- (i) => i.parentId === item.id && i.type === 'subcategory',
- )
- processItemsRecursively(subcategories, result, true)
- }
- } else if (item.type === 'subcategory') {
- if (parentExpanded) {
- result.push(item)
- if (expandedSubcategories.value.has(item.id)) {
- const subItems = normalizedItems.value.filter(
- (i) => i.parentId === item.id && i.type === 'item',
- )
- processItemsRecursively(subItems, result, true)
- }
- }
- } else if (item.type === 'item' && parentExpanded) {
- result.push(item)
- }
- }
- }
- /**
- * Construit une liste hiérarchique d'éléments en mode normal (sans recherche).
- *
- * @returns {SelectItem[]} Liste hiérarchique basée sur l'état d'expansion.
- */
- const buildNormalModeList = (): SelectItem[] => {
- const result: SelectItem[] = []
- const topLevelItems = normalizedItems.value.filter((item) => !item.parentId)
- processItemsRecursively(topLevelItems, result)
- return result
- }
- /**
- * A computed property that generates a flattened and organized list of items
- * from a hierarchical structure, based on the current search text and
- * expanded categories/subcategories.
- *
- * @returns {SelectItem[]} Flattened and organized list of items.
- */
- const flattenedItems = computed(() => {
- const hasSearch = !!searchText.value.trim()
- if (hasSearch) {
- const matchingItems = findMatchingLevel2Items()
- return buildSearchResultsList(matchingItems)
- }
- return buildNormalModeList()
- })
- /**
- * A computed property that maps selected values to their corresponding labels.
- * This is used to display the correct labels in the chips when the dropdown is closed.
- *
- * @returns {Record<string, string>} A map of selected values to their labels.
- */
- const selectedItemsMap = computed(() => {
- const map: Record<string, string> = {}
- // Find all selectable items (type 'item') in the items array with normalized labels
- const selectableItems = normalizedItems.value.filter(
- (item) => item.type === 'item' && item.value,
- )
- // Create a map of values to labels
- selectableItems.forEach((item) => {
- if (item.value) {
- map[item.value] = item.label
- }
- })
- return map
- })
- </script>
- <style scoped lang="scss">
- .v-list-item--active {
- background-color: rgba(var(--v-theme-primary), 0.1);
- }
- .v-list-item {
- contain: layout style paint;
- height: 40px !important; /* Ensure consistent height for virtual scrolling */
- min-height: 40px !important;
- max-height: 40px !important;
- padding-top: 0 !important;
- padding-bottom: 0 !important;
- }
- .search-icon {
- color: rgba(var(--v-theme-on-surface), var(--v-medium-emphasis-opacity));
- }
- :deep(.v-field__prepend-inner) {
- padding-top: 0;
- }
- :deep(.v-list) {
- padding-top: 0;
- contain: content;
- will-change: transform;
- transform-style: preserve-3d;
- }
- </style>
|