TreeSelect.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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: 400 }"
  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="onSearchInputDebounced"
  41. @click:clear.stop="onSearchClear"
  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. <!-- Always prioritize the mapping for consistent labels, fall back to item label if available -->
  53. {{
  54. selectedItemsMap[item.raw.value] ||
  55. (item.raw.label && item.raw.label !== item.raw.value ? item.raw.label : item.raw.value)
  56. }}
  57. </v-chip>
  58. <span
  59. v-if="
  60. maxVisibleChips &&
  61. index === maxVisibleChips &&
  62. modelValue.length > maxVisibleChips
  63. "
  64. class="text-grey text-caption align-self-center"
  65. >
  66. (+{{ modelValue.length - maxVisibleChips }} {{ $t('others') }})
  67. </span>
  68. </template>
  69. <template #item="{ item }">
  70. <template v-if="item.raw.type === 'category'">
  71. <v-list-item
  72. :ripple="false"
  73. :class="{
  74. 'v-list-item--active': expandedCategories.has(item.raw.id),
  75. }"
  76. @click.stop="toggleCategory(item.raw.id)"
  77. >
  78. <template #prepend>
  79. <v-icon
  80. :icon="
  81. 'fas ' +
  82. (expandedCategories.has(item.raw.id)
  83. ? 'fa-angle-down'
  84. : 'fa-angle-right')
  85. "
  86. size="small"
  87. />
  88. </template>
  89. <v-list-item-title class="font-weight-medium">
  90. {{ item.raw.label }}
  91. </v-list-item-title>
  92. </v-list-item>
  93. </template>
  94. <template v-else-if="item.raw.type === 'subcategory'">
  95. <v-list-item
  96. :ripple="false"
  97. :class="{
  98. 'v-list-item--active': expandedSubcategories.has(item.raw.id),
  99. 'pl-8': true,
  100. }"
  101. @click.stop="toggleSubcategory(item.raw.id)"
  102. >
  103. <template #prepend>
  104. <v-icon
  105. :icon="
  106. 'fas ' +
  107. (expandedSubcategories.has(item.raw.id)
  108. ? 'fa-angle-down'
  109. : 'fa-angle-right')
  110. "
  111. size="small"
  112. />
  113. </template>
  114. <v-list-item-title>
  115. {{ item.raw.label }}
  116. </v-list-item-title>
  117. </v-list-item>
  118. </template>
  119. <template v-else>
  120. <v-list-item
  121. :active="modelValue.includes(item.raw.value!)"
  122. :class="{
  123. 'd-flex': true,
  124. 'pl-12': item.raw.level === 2,
  125. 'pl-8': item.raw.level === 1,
  126. }"
  127. @click="toggleItem(item.raw.value!)"
  128. >
  129. <template #prepend>
  130. <v-checkbox
  131. :model-value="modelValue.includes(item.raw.value!)"
  132. color="primary"
  133. :hide-details="true"
  134. @click.stop="toggleItem(item.raw.value!)"
  135. />
  136. </template>
  137. <v-list-item-title>
  138. {{ item.raw.label }}
  139. </v-list-item-title>
  140. </v-list-item>
  141. </template>
  142. </template>
  143. </v-select>
  144. </template>
  145. <script setup lang="ts">
  146. import StringUtils from '~/services/utils/stringUtils'
  147. import _ from 'lodash'
  148. interface SelectItem {
  149. id: string
  150. label: string
  151. normalizedLabel?: string
  152. value?: string
  153. type: 'category' | 'subcategory' | 'item'
  154. parentId?: string
  155. level: number
  156. }
  157. const props = defineProps({
  158. modelValue: {
  159. type: Array as PropType<string[]>,
  160. required: true,
  161. },
  162. items: {
  163. type: Array as PropType<SelectItem[]>,
  164. required: true,
  165. },
  166. maxVisibleChips: {
  167. type: Number,
  168. required: false,
  169. default: null,
  170. },
  171. })
  172. /**
  173. * A computed property that normalizes the labels of all items upfront.
  174. * This avoids having to normalize labels during each search operation.
  175. */
  176. const normalizedItems = computed(() => {
  177. return props.items.map(item => ({
  178. ...item,
  179. normalizedLabel: StringUtils.normalize(item.label)
  180. }))
  181. })
  182. const emit = defineEmits(['update:modelValue'])
  183. const expandedCategories: Ref<Set<string>> = ref(new Set())
  184. const expandedSubcategories: Ref<Set<string>> = ref(new Set())
  185. const searchText: Ref<string> = ref('')
  186. /**
  187. * A callback function that is triggered when the menu's open state is updated.
  188. */
  189. const onMenuUpdate = (isOpen: boolean) => {
  190. // Réinitialiser la recherche quand le menu se ferme
  191. if (!isOpen && searchText.value) {
  192. searchText.value = ''
  193. onSearchInput()
  194. }
  195. }
  196. /**
  197. * Toggles the expanded state of a given category. If the category is currently
  198. * expanded, it will collapse the category and also collapse its subcategories.
  199. * If the category is not expanded, it will expand the category.
  200. *
  201. * @param {string} categoryId - The unique identifier of the category to toggle.
  202. */
  203. const toggleCategory = (categoryId: string) => {
  204. if (expandedCategories.value.has(categoryId)) {
  205. expandedCategories.value.delete(categoryId)
  206. // Fermer aussi les sous-catégories
  207. const subcategories = normalizedItems.value.filter(
  208. (i) => i.parentId === categoryId && i.type === 'subcategory',
  209. )
  210. subcategories.forEach((sub) => {
  211. expandedSubcategories.value.delete(sub.id)
  212. })
  213. } else {
  214. expandedCategories.value.add(categoryId)
  215. }
  216. }
  217. /**
  218. * Toggles the expansion state of a subcategory.
  219. *
  220. * @param {string} subcategoryId - The unique identifier of the subcategory to be toggled.
  221. */
  222. const toggleSubcategory = (subcategoryId: string) => {
  223. if (expandedSubcategories.value.has(subcategoryId)) {
  224. expandedSubcategories.value.delete(subcategoryId)
  225. } else {
  226. expandedSubcategories.value.add(subcategoryId)
  227. }
  228. }
  229. /**
  230. * A function that toggles the inclusion of a specific value in
  231. * the selected items list.
  232. *
  233. * @param {string} value - The value to toggle in the selected items list.
  234. */
  235. const toggleItem = (value: string) => {
  236. const currentSelection = [...props.modelValue]
  237. const index = currentSelection.indexOf(value)
  238. if (index > -1) {
  239. currentSelection.splice(index, 1)
  240. } else {
  241. currentSelection.push(value)
  242. }
  243. emit('update:modelValue', currentSelection)
  244. }
  245. /**
  246. * Removes the specified item from the model value and emits an update event.
  247. *
  248. * @param {string} value - The item to be removed from the model value.
  249. * @emits update:modelValue - A custom event emitted with the updated model value
  250. * after the specified item has been removed.
  251. */
  252. const removeItem = (value: string) => {
  253. emit(
  254. 'update:modelValue',
  255. props.modelValue.filter((item) => item !== value),
  256. )
  257. }
  258. /**
  259. * Fonction appellée lorsque l'input de recherche textuelle est modifié
  260. */
  261. const onSearchInput = () => {
  262. // Réinitialiser les états d'expansion dans tous les cas
  263. expandedCategories.value.clear()
  264. expandedSubcategories.value.clear()
  265. if (searchText.value.trim()) {
  266. // Trouver tous les éléments qui correspondent à la recherche
  267. const matchingItems = normalizedItems.value.filter(
  268. (item) =>
  269. item.type === 'item' && item.level === 2 && itemMatchesSearch(item),
  270. )
  271. // Pour chaque élément correspondant, ajouter ses parents aux ensembles d'expansion
  272. for (const item of matchingItems) {
  273. // Trouver et ajouter la sous-catégorie parente
  274. const subcategory = normalizedItems.value.find((i) => i.id === item.parentId)
  275. if (subcategory) {
  276. expandedSubcategories.value.add(subcategory.id)
  277. // Trouver et ajouter la catégorie parente
  278. const category = normalizedItems.value.find((i) => i.id === subcategory.parentId)
  279. if (category) {
  280. expandedCategories.value.add(category.id)
  281. }
  282. }
  283. }
  284. }
  285. }
  286. const onSearchInputDebounced = _.debounce(onSearchInput, 200)
  287. const onSearchClear = () => {
  288. searchText.value = ''
  289. onSearchInput()
  290. }
  291. /**
  292. * Checks if any word in the normalized text starts with the normalized search term.
  293. *
  294. * @param {string} normalizedText - The normalized text to check.
  295. * @param {string} normalizedSearch - The normalized search term.
  296. * @returns {boolean} `true` if any word in the text starts with the search term; otherwise, `false`.
  297. */
  298. const anyWordStartsWith = (
  299. normalizedText: string,
  300. normalizedSearch: string,
  301. ): boolean => {
  302. if (normalizedText.indexOf(normalizedSearch) === 0) return true
  303. const spaceIndex = normalizedText.indexOf(' ')
  304. if (spaceIndex === -1) return false
  305. return normalizedText
  306. .split(' ')
  307. .some(word => word.startsWith(normalizedSearch))
  308. }
  309. /**
  310. * Determines if a given item matches the current search text by checking its normalized label
  311. * and, for certain items, the normalized labels of its parent elements.
  312. *
  313. * The search text is normalized using `StringUtils.normalize` before comparison.
  314. * If no search text is provided, the item matches by default.
  315. *
  316. * For items of type `item` at level 2, the function checks:
  317. * - The normalized label of the item itself
  318. * - The normalized label of its parent subcategory
  319. * - The normalized label of the grandparent category (if applicable)
  320. *
  321. * For all other item types, only the item's normalized label is checked.
  322. *
  323. * The matching is done by checking if any word in the normalized label starts with the normalized search text.
  324. *
  325. * @param {SelectItem} item - The item to evaluate against the search text.
  326. * @returns {boolean} `true` if the item or its relevant parent(s) match the search text; otherwise, `false`.
  327. */
  328. const itemMatchesSearch = (item: SelectItem): boolean => {
  329. if (!searchText.value) return true
  330. const normalizedSearch = StringUtils.normalize(searchText.value)
  331. // Find the item with normalized label from our computed property
  332. const itemWithNormalizedLabel = normalizedItems.value.find(i => i.id === item.id)
  333. if (!itemWithNormalizedLabel) return false
  334. // Si c'est un élément de niveau 2, vérifier son label et les labels de ses parents
  335. if (item.type === 'item' && item.level === 2) {
  336. // Vérifier le label de l'élément
  337. if (anyWordStartsWith(itemWithNormalizedLabel.normalizedLabel!, normalizedSearch))
  338. return true
  339. // Trouver et vérifier le label de la sous-catégorie parente
  340. const subcategory = normalizedItems.value.find((i) => i.id === item.parentId)
  341. if (
  342. subcategory &&
  343. anyWordStartsWith(
  344. subcategory.normalizedLabel!,
  345. normalizedSearch,
  346. )
  347. )
  348. return true
  349. // Trouver et vérifier le label de la catégorie parente
  350. if (subcategory && subcategory.parentId) {
  351. const category = normalizedItems.value.find((i) => i.id === subcategory.parentId)
  352. if (
  353. category &&
  354. anyWordStartsWith(
  355. category.normalizedLabel!,
  356. normalizedSearch,
  357. )
  358. )
  359. return true
  360. }
  361. return false
  362. }
  363. // Pour les autres types d'éléments, vérifier simplement leur label
  364. return anyWordStartsWith(itemWithNormalizedLabel.normalizedLabel!, normalizedSearch)
  365. }
  366. /**
  367. * Filtre les éléments de niveau 2 qui correspondent au texte de recherche.
  368. *
  369. * @returns {SelectItem[]} Les éléments de niveau 2 qui correspondent à la recherche.
  370. */
  371. const findMatchingLevel2Items = (): SelectItem[] => {
  372. return normalizedItems.value.filter(
  373. (item) =>
  374. item.type === 'item' && item.level === 2 && itemMatchesSearch(item),
  375. )
  376. }
  377. /**
  378. * Construit une liste hiérarchique d'éléments basée sur les résultats de recherche.
  379. * Pour chaque élément correspondant, ajoute sa hiérarchie complète (catégorie et sous-catégorie).
  380. *
  381. * @param {SelectItem[]} matchingItems - Les éléments correspondant à la recherche.
  382. * @returns {SelectItem[]} Liste hiérarchique incluant les éléments et leurs parents.
  383. */
  384. const buildSearchResultsList = (matchingItems: SelectItem[]): SelectItem[] => {
  385. const result: SelectItem[] = []
  386. const addedCategoryIds = new Set<string>()
  387. const addedSubcategoryIds = new Set<string>()
  388. for (const item of matchingItems) {
  389. // Trouver la sous-catégorie parente
  390. const subcategory = normalizedItems.value.find((i) => i.id === item.parentId)
  391. if (!subcategory) continue
  392. // Trouver la catégorie parente
  393. const category = normalizedItems.value.find((i) => i.id === subcategory.parentId)
  394. if (!category) continue
  395. // Ajouter la catégorie si elle n'est pas déjà présente
  396. if (!addedCategoryIds.has(category.id)) {
  397. result.push(category)
  398. addedCategoryIds.add(category.id)
  399. expandedCategories.value.add(category.id)
  400. }
  401. // Ajouter la sous-catégorie si elle n'est pas déjà présente
  402. if (!addedSubcategoryIds.has(subcategory.id)) {
  403. result.push(subcategory)
  404. addedSubcategoryIds.add(subcategory.id)
  405. expandedSubcategories.value.add(subcategory.id)
  406. }
  407. // Ajouter l'élément
  408. result.push(item)
  409. }
  410. return result
  411. }
  412. /**
  413. * Traite récursivement les éléments pour construire une liste hiérarchique
  414. * basée sur l'état d'expansion des catégories et sous-catégories.
  415. *
  416. * @param {SelectItem[]} items - Les éléments à traiter.
  417. * @param {SelectItem[]} result - Le tableau résultat à remplir.
  418. * @param {boolean} parentExpanded - Indique si le parent est développé.
  419. */
  420. const processItemsRecursively = (
  421. items: SelectItem[],
  422. result: SelectItem[],
  423. parentExpanded = true,
  424. ): void => {
  425. for (const item of items) {
  426. if (item.type === 'category') {
  427. result.push(item)
  428. if (expandedCategories.value.has(item.id)) {
  429. const subcategories = normalizedItems.value.filter(
  430. (i) => i.parentId === item.id && i.type === 'subcategory',
  431. )
  432. processItemsRecursively(subcategories, result, true)
  433. }
  434. } else if (item.type === 'subcategory') {
  435. if (parentExpanded) {
  436. result.push(item)
  437. if (expandedSubcategories.value.has(item.id)) {
  438. const subItems = normalizedItems.value.filter(
  439. (i) => i.parentId === item.id && i.type === 'item',
  440. )
  441. processItemsRecursively(subItems, result, true)
  442. }
  443. }
  444. } else if (item.type === 'item' && parentExpanded) {
  445. result.push(item)
  446. }
  447. }
  448. }
  449. /**
  450. * Construit une liste hiérarchique d'éléments en mode normal (sans recherche).
  451. *
  452. * @returns {SelectItem[]} Liste hiérarchique basée sur l'état d'expansion.
  453. */
  454. const buildNormalModeList = (): SelectItem[] => {
  455. const result: SelectItem[] = []
  456. const topLevelItems = normalizedItems.value.filter((item) => !item.parentId)
  457. processItemsRecursively(topLevelItems, result)
  458. return result
  459. }
  460. /**
  461. * A computed property that generates a flattened and organized list of items
  462. * from a hierarchical structure, based on the current search text and
  463. * expanded categories/subcategories.
  464. *
  465. * @returns {SelectItem[]} Flattened and organized list of items.
  466. */
  467. const flattenedItems = computed(() => {
  468. const hasSearch = !!searchText.value.trim()
  469. if (hasSearch) {
  470. const matchingItems = findMatchingLevel2Items()
  471. return buildSearchResultsList(matchingItems)
  472. }
  473. return buildNormalModeList()
  474. })
  475. /**
  476. * A computed property that maps selected values to their corresponding labels.
  477. * This is used to display the correct labels in the chips when the dropdown is closed.
  478. *
  479. * @returns {Record<string, string>} A map of selected values to their labels.
  480. */
  481. const selectedItemsMap = computed(() => {
  482. const map: Record<string, string> = {}
  483. // Find all selectable items (type 'item') in the items array with normalized labels
  484. const selectableItems = normalizedItems.value.filter(
  485. (item) => item.type === 'item' && item.value,
  486. )
  487. // Create a map of values to labels
  488. selectableItems.forEach((item) => {
  489. if (item.value) {
  490. map[item.value] = item.label
  491. }
  492. })
  493. return map
  494. })
  495. </script>
  496. <style scoped lang="scss">
  497. .v-list-item--active {
  498. background-color: rgba(var(--v-theme-primary), 0.1);
  499. }
  500. .v-list-item {
  501. contain: layout style paint;
  502. height: 40px !important; /* Ensure consistent height for virtual scrolling */
  503. min-height: 40px !important;
  504. max-height: 40px !important;
  505. padding-top: 0 !important;
  506. padding-bottom: 0 !important;
  507. }
  508. .search-icon {
  509. color: rgba(var(--v-theme-on-surface), var(--v-medium-emphasis-opacity));
  510. }
  511. :deep(.v-field__prepend-inner) {
  512. padding-top: 0;
  513. }
  514. :deep(.v-list) {
  515. padding-top: 0;
  516. contain: content;
  517. will-change: transform;
  518. transform-style: preserve-3d;
  519. }
  520. </style>