TreeSelect.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <!--
  2. Composant de sélection hiérarchique à plusieurs niveaux permettant de naviguer
  3. et sélectionner des éléments organisés en catégories et sous-catégories.
  4. ## Exemple d'utilisation
  5. ```vue
  6. <TreeSelect
  7. v-model="selectedValues"
  8. :items="hierarchicalItems"
  9. :max-visible-chips="3"
  10. label="Sélectionner des éléments"
  11. />
  12. ```
  13. -->
  14. <template>
  15. <v-select
  16. :model-value="modelValue"
  17. v-bind="$attrs"
  18. :items="flattenedItems"
  19. item-title="label"
  20. item-value="value"
  21. multiple
  22. chips
  23. closable-chips
  24. :menu-props="{ maxHeight: 500 }"
  25. @update:menu="onMenuUpdate"
  26. @update:model-value="$emit('update:modelValue', $event)"
  27. >
  28. <template #prepend-item>
  29. <!-- Champs de recherche textuelle -->
  30. <v-text-field
  31. v-model="searchText"
  32. density="compact"
  33. hide-details
  34. :placeholder="$t('search') + '...'"
  35. prepend-inner-icon="fas fa-magnifying-glass"
  36. variant="outlined"
  37. clearable
  38. class="mx-2 my-2"
  39. @click.stop
  40. @input="onSearchInput"
  41. @click:clear="onSearchInput"
  42. />
  43. <v-divider class="mt-2"/>
  44. </template>
  45. <template #selection="{ item, index }">
  46. <v-chip
  47. v-if="maxVisibleChips && index < maxVisibleChips"
  48. :key="item.raw.value"
  49. closable
  50. @click:close="removeItem(item.raw.value!)"
  51. >
  52. {{ item.raw.label }}
  53. </v-chip>
  54. <span
  55. v-if="maxVisibleChips && index === maxVisibleChips && modelValue.length > maxVisibleChips"
  56. class="text-grey text-caption align-self-center"
  57. >
  58. (+{{ modelValue.length - maxVisibleChips }} {{ $t('others') }})
  59. </span>
  60. </template>
  61. <template #item="{ item }">
  62. <template v-if="item.raw.type === 'category'">
  63. <v-list-item
  64. @click.stop="toggleCategory(item.raw.id)"
  65. :ripple="false"
  66. :class="{ 'v-list-item--active': expandedCategories.has(item.raw.id) }"
  67. >
  68. <template #prepend>
  69. <v-icon
  70. :icon="'fas ' + (expandedCategories.has(item.raw.id) ? 'fa-angle-down' : 'fa-angle-right')"
  71. size="small"
  72. />
  73. </template>
  74. <v-list-item-title class="font-weight-medium">
  75. {{ item.raw.label }}
  76. </v-list-item-title>
  77. </v-list-item>
  78. </template>
  79. <template v-else-if="item.raw.type === 'subcategory'">
  80. <v-list-item
  81. @click.stop="toggleSubcategory(item.raw.id)"
  82. :ripple="false"
  83. :class="{
  84. 'v-list-item--active': expandedSubcategories.has(item.raw.id),
  85. 'pl-8': true
  86. }"
  87. >
  88. <template #prepend>
  89. <v-icon
  90. :icon="'fas ' + (expandedSubcategories.has(item.raw.id) ? 'fa-angle-down' : 'fa-angle-right')"
  91. size="small"
  92. />
  93. </template>
  94. <v-list-item-title>
  95. {{ item.raw.label }}
  96. </v-list-item-title>
  97. </v-list-item>
  98. </template>
  99. <template v-else>
  100. <v-list-item
  101. @click="toggleItem(item.raw.value!)"
  102. :active="modelValue.includes(item.raw.value!)"
  103. :class="{ 'pl-12': item.raw.level === 2, 'pl-8': item.raw.level === 1 }"
  104. >
  105. <template #prepend>
  106. <v-checkbox
  107. :model-value="modelValue.includes(item.raw.value!)"
  108. @click.stop="toggleItem(item.raw.value!)"
  109. color="primary"
  110. :hide-details="true"
  111. />
  112. </template>
  113. <v-list-item-title>
  114. {{ item.raw.label }}
  115. </v-list-item-title>
  116. </v-list-item>
  117. </template>
  118. </template>
  119. </v-select>
  120. </template>
  121. <script setup lang="ts">
  122. import StringUtils from '~/services/utils/stringUtils'
  123. interface SelectItem {
  124. id: string
  125. label: string
  126. value?: string
  127. type: 'category' | 'subcategory' | 'item'
  128. parentId?: string
  129. level: number
  130. }
  131. const props = defineProps({
  132. modelValue: {
  133. type: Array as PropType<string[]>,
  134. required: true
  135. },
  136. items: {
  137. type: Array as PropType<SelectItem[]>,
  138. required: true
  139. },
  140. maxVisibleChips: {
  141. type: Number,
  142. required: false,
  143. default: null
  144. }
  145. })
  146. const emit = defineEmits(['update:modelValue'])
  147. const expandedCategories: Ref<Set<string>> = ref(new Set())
  148. const expandedSubcategories: Ref<Set<string>> = ref(new Set())
  149. const searchText: Ref<string> = ref('')
  150. /**
  151. * A callback function that is triggered when the menu's open state is updated.
  152. */
  153. const onMenuUpdate = (isOpen: boolean) => {
  154. // Réinitialiser la recherche quand le menu se ferme
  155. if (!isOpen && searchText.value) {
  156. searchText.value = ''
  157. onSearchInput()
  158. }
  159. }
  160. /**
  161. * A computed property that generates a flattened and organized list of items
  162. * from a hierarchical structure, based on the current search text and
  163. * expanded categories/subcategories.
  164. *
  165. * Logic:
  166. * - If there is a search text:
  167. * - Filters items to include only level 2 items matching the search text.
  168. * - Ensures parent categories and subcategories are added to the result.
  169. * - Expands categories and subcategories relevant to search results.
  170. * - Without a search text:
  171. * - Recursively processes items to include all relevant categories,
  172. * subcategories, and individual items based on the expanded states.
  173. *
  174. * @returns {SelectItem[]} Flattened and organized list of items.
  175. */
  176. const flattenedItems = computed(() => {
  177. const result: SelectItem[] = []
  178. const hasSearch = !!searchText.value.trim()
  179. // TODO: simplifier et découper
  180. // Si une recherche est active, afficher uniquement les éléments de niveau 2 qui correspondent
  181. if (hasSearch) {
  182. // Trouver tous les éléments de niveau 2 qui correspondent à la recherche
  183. const matchingItems = props.items.filter(item =>
  184. item.type === 'item' &&
  185. item.level === 2 &&
  186. itemMatchesSearch(item)
  187. )
  188. // Ensemble pour suivre les catégories et sous-catégories déjà ajoutées
  189. const addedCategoryIds = new Set<string>()
  190. const addedSubcategoryIds = new Set<string>()
  191. // Pour chaque élément correspondant, ajouter sa hiérarchie complète
  192. for (const item of matchingItems) {
  193. // Trouver la sous-catégorie parente
  194. const subcategory = props.items.find(i => i.id === item.parentId)
  195. if (!subcategory) continue
  196. // Trouver la catégorie parente
  197. const category = props.items.find(i => i.id === subcategory.parentId)
  198. if (!category) continue
  199. // Ajouter la catégorie si elle n'est pas déjà présente
  200. if (!addedCategoryIds.has(category.id)) {
  201. result.push(category)
  202. addedCategoryIds.add(category.id)
  203. // S'assurer que la catégorie est considérée comme "expanded" pendant la recherche
  204. expandedCategories.value.add(category.id)
  205. }
  206. // Ajouter la sous-catégorie si elle n'est pas déjà présente
  207. if (!addedSubcategoryIds.has(subcategory.id)) {
  208. result.push(subcategory)
  209. addedSubcategoryIds.add(subcategory.id)
  210. // S'assurer que la sous-catégorie est considérée comme "expanded" pendant la recherche
  211. expandedSubcategories.value.add(subcategory.id)
  212. }
  213. // Ajouter l'élément
  214. result.push(item)
  215. }
  216. return result
  217. }
  218. // Comportement normal sans recherche
  219. const processItems = (items: SelectItem[], parentExpanded = true) => {
  220. for (const item of items) {
  221. if (item.type === 'category') {
  222. result.push(item)
  223. if (expandedCategories.value.has(item.id)) {
  224. const subcategories = props.items.filter(i =>
  225. i.parentId === item.id && i.type === 'subcategory'
  226. )
  227. processItems(subcategories, true)
  228. }
  229. } else if (item.type === 'subcategory') {
  230. if (parentExpanded) {
  231. result.push(item)
  232. if (expandedSubcategories.value.has(item.id)) {
  233. const subItems = props.items.filter(i =>
  234. i.parentId === item.id && i.type === 'item'
  235. )
  236. processItems(subItems, true)
  237. }
  238. }
  239. } else if (item.type === 'item' && parentExpanded) {
  240. result.push(item)
  241. }
  242. }
  243. }
  244. const topLevelItems = props.items.filter(item => !item.parentId)
  245. processItems(topLevelItems)
  246. return result
  247. })
  248. /**
  249. * Toggles the expanded state of a given category. If the category is currently
  250. * expanded, it will collapse the category and also collapse its subcategories.
  251. * If the category is not expanded, it will expand the category.
  252. *
  253. * @param {string} categoryId - The unique identifier of the category to toggle.
  254. */
  255. const toggleCategory = (categoryId: string) => {
  256. if (expandedCategories.value.has(categoryId)) {
  257. expandedCategories.value.delete(categoryId)
  258. // Fermer aussi les sous-catégories
  259. const subcategories = props.items.filter(i =>
  260. i.parentId === categoryId && i.type === 'subcategory'
  261. )
  262. subcategories.forEach(sub => {
  263. expandedSubcategories.value.delete(sub.id)
  264. })
  265. } else {
  266. expandedCategories.value.add(categoryId)
  267. }
  268. }
  269. /**
  270. * Toggles the expansion state of a subcategory.
  271. *
  272. * @param {string} subcategoryId - The unique identifier of the subcategory to be toggled.
  273. */
  274. const toggleSubcategory = (subcategoryId: string) => {
  275. if (expandedSubcategories.value.has(subcategoryId)) {
  276. expandedSubcategories.value.delete(subcategoryId)
  277. } else {
  278. expandedSubcategories.value.add(subcategoryId)
  279. }
  280. }
  281. /**
  282. * A function that toggles the inclusion of a specific value in
  283. * the selected items list.
  284. *
  285. * @param {string} value - The value to toggle in the selected items list.
  286. */
  287. const toggleItem = (value: string) => {
  288. const currentSelection = [...props.modelValue]
  289. const index = currentSelection.indexOf(value)
  290. if (index > -1) {
  291. currentSelection.splice(index, 1)
  292. } else {
  293. currentSelection.push(value)
  294. }
  295. emit('update:modelValue', currentSelection)
  296. }
  297. /**
  298. * Removes the specified item from the model value and emits an update event.
  299. *
  300. * @param {string} value - The item to be removed from the model value.
  301. * @emits update:modelValue - A custom event emitted with the updated model value
  302. * after the specified item has been removed.
  303. */
  304. const removeItem = (value: string) => {
  305. emit('update:modelValue', props.modelValue.filter(item => item !== value))
  306. }
  307. /**
  308. * Fonction appellée lorsque l'input de recherche textuelle est modifié
  309. */
  310. const onSearchInput = () => {
  311. // Réinitialiser les états d'expansion dans tous les cas
  312. expandedCategories.value.clear()
  313. expandedSubcategories.value.clear()
  314. if (searchText.value.trim()) {
  315. // Trouver tous les éléments qui correspondent à la recherche
  316. const matchingItems = props.items.filter(item =>
  317. item.type === 'item' &&
  318. item.level === 2 &&
  319. itemMatchesSearch(item)
  320. )
  321. // Pour chaque élément correspondant, ajouter ses parents aux ensembles d'expansion
  322. for (const item of matchingItems) {
  323. // Trouver et ajouter la sous-catégorie parente
  324. const subcategory = props.items.find(i => i.id === item.parentId)
  325. if (subcategory) {
  326. expandedSubcategories.value.add(subcategory.id)
  327. // Trouver et ajouter la catégorie parente
  328. const category = props.items.find(i => i.id === subcategory.parentId)
  329. if (category) {
  330. expandedCategories.value.add(category.id)
  331. }
  332. }
  333. }
  334. }
  335. }
  336. /**
  337. * Determines if a given item matches the current search text by checking its label
  338. * and, for certain items, the labels of its parent elements.
  339. *
  340. * The search text is normalized using `StringUtils.normalize` before comparison.
  341. * If no search text is provided, the item matches by default.
  342. *
  343. * For items of type `item` at level 2, the function checks:
  344. * - The label of the item itself
  345. * - The label of its parent subcategory
  346. * - The label of the grandparent category (if applicable)
  347. *
  348. * For all other item types, only the item's label is checked.
  349. *
  350. * @param {SelectItem} item - The item to evaluate against the search text.
  351. * @returns {boolean} `true` if the item or its relevant parent(s) match the search text; otherwise, `false`.
  352. */
  353. const itemMatchesSearch = (item: SelectItem): boolean => {
  354. if (!searchText.value) return true
  355. const normalizedSearch = StringUtils.normalize(searchText.value)
  356. // Si c'est un élément de niveau 2, vérifier son label et les labels de ses parents
  357. if (item.type === 'item' && item.level === 2) {
  358. // Vérifier le label de l'élément
  359. if (StringUtils.normalize(item.label).includes(normalizedSearch)) return true
  360. // Trouver et vérifier le label de la sous-catégorie parente
  361. const subcategory = props.items.find(i => i.id === item.parentId)
  362. if (subcategory && StringUtils.normalize(subcategory.label).includes(normalizedSearch)) return true
  363. // Trouver et vérifier le label de la catégorie parente
  364. if (subcategory && subcategory.parentId) {
  365. const category = props.items.find(i => i.id === subcategory.parentId)
  366. if (category && StringUtils.normalize(category.label).includes(normalizedSearch)) return true
  367. }
  368. return false
  369. }
  370. // Pour les autres types d'éléments, vérifier simplement leur label
  371. return StringUtils.normalize(item.label).includes(normalizedSearch)
  372. }
  373. </script>
  374. <style scoped lang="scss">
  375. .v-list-item--active {
  376. background-color: rgba(var(--v-theme-primary), 0.1);
  377. }
  378. .search-icon {
  379. color: rgba(var(--v-theme-on-surface), var(--v-medium-emphasis-opacity));
  380. }
  381. :deep(.v-field__prepend-inner) {
  382. padding-top: 0;
  383. }
  384. :deep(.v-list) {
  385. padding-top: 0;
  386. }
  387. </style>