TreeSelect.vue 17 KB

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