|
|
@@ -176,6 +176,11 @@ const props = defineProps({
|
|
|
required: false,
|
|
|
default: null,
|
|
|
},
|
|
|
+ nbLevel: {
|
|
|
+ type: Number,
|
|
|
+ required: false,
|
|
|
+ default: 3,
|
|
|
+ },
|
|
|
/**
|
|
|
* Label du champ
|
|
|
* Si non défini, c'est le nom de propriété qui est utilisé
|
|
|
@@ -273,21 +278,27 @@ const expandParentsOfSelectedItems = () => {
|
|
|
const item = normalizedItems.value.find((i) => i.value === selectedId)
|
|
|
if (!item) continue
|
|
|
|
|
|
- // Trouver la sous-catégorie
|
|
|
- const subcategory = normalizedItems.value.find(
|
|
|
- (i) => i.id === item.parentId,
|
|
|
- )
|
|
|
- if (subcategory) {
|
|
|
- expandedSubcategories.value.add(subcategory.id)
|
|
|
+ let parentId = null
|
|
|
+ if(props.nbLevel === 3){
|
|
|
+ // Trouver la sous-catégorie
|
|
|
+ const subcategory = normalizedItems.value.find(
|
|
|
+ (i) => i.id === item.parentId,
|
|
|
+ )
|
|
|
+ if (subcategory) {
|
|
|
+ expandedSubcategories.value.add(subcategory.id)
|
|
|
+ parentId = subcategory.parentId
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ parentId = item.parentId
|
|
|
+ }
|
|
|
|
|
|
+ if (parentId) {
|
|
|
// Trouver la catégorie
|
|
|
- if (subcategory.parentId) {
|
|
|
- const category = normalizedItems.value.find(
|
|
|
- (i) => i.id === subcategory.parentId,
|
|
|
- )
|
|
|
- if (category) {
|
|
|
- expandedCategories.value.add(category.id)
|
|
|
- }
|
|
|
+ const category = normalizedItems.value.find(
|
|
|
+ (i) => i.id === parentId,
|
|
|
+ )
|
|
|
+ if (category) {
|
|
|
+ expandedCategories.value.add(category.id)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -387,25 +398,33 @@ const onSearchInput = () => {
|
|
|
// Trouver tous les éléments qui correspondent à la recherche
|
|
|
const matchingItems = normalizedItems.value.filter(
|
|
|
(item) =>
|
|
|
- item.type === 'item' && item.level === 2 && itemMatchesSearch(item),
|
|
|
+ item.type === 'item' && item.level === (props.nbLevel - 1) && 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)
|
|
|
+ let category
|
|
|
+ if(props.nbLevel === 3){
|
|
|
+ // 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
|
|
|
+ category = normalizedItems.value.find(
|
|
|
+ (i) => i.id === subcategory.parentId,
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }else{
|
|
|
// Trouver et ajouter la catégorie parente
|
|
|
- const category = normalizedItems.value.find(
|
|
|
- (i) => i.id === subcategory.parentId,
|
|
|
+ category = normalizedItems.value.find(
|
|
|
+ (i) => i.id === item.parentId,
|
|
|
)
|
|
|
- if (category) {
|
|
|
- expandedCategories.value.add(category.id)
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (category) {
|
|
|
+ expandedCategories.value.add(category.id)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -469,8 +488,8 @@ const itemMatchesSearch = (item: SelectItem): boolean => {
|
|
|
)
|
|
|
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) {
|
|
|
+ // Si c'est un élément de niveau nbLevel - 1, vérifier son label et les labels de ses parents
|
|
|
+ if (item.type === 'item' && item.level === (props.nbLevel - 1)) {
|
|
|
// Vérifier le label de l'élément
|
|
|
if (
|
|
|
anyWordStartsWith(
|
|
|
@@ -480,28 +499,35 @@ const itemMatchesSearch = (item: SelectItem): boolean => {
|
|
|
)
|
|
|
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,
|
|
|
+ let parentId = item.parentId
|
|
|
+ if(props.nbLevel === 3){
|
|
|
+ // Trouver et vérifier le label de la sous-catégorie parente
|
|
|
+ const subcategory = normalizedItems.value.find(
|
|
|
+ (i) => i.id === parentId,
|
|
|
)
|
|
|
if (
|
|
|
- category &&
|
|
|
- anyWordStartsWith(category.normalizedLabel!, normalizedSearch)
|
|
|
+ subcategory &&
|
|
|
+ anyWordStartsWith(subcategory.normalizedLabel!, normalizedSearch)
|
|
|
)
|
|
|
return true
|
|
|
+
|
|
|
+ // Trouver et vérifier le label de la catégorie parente
|
|
|
+ if (subcategory && subcategory.parentId) {
|
|
|
+ parentId = subcategory.parentId
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ // Trouver et vérifier le label de la catégorie parente
|
|
|
+ const category = normalizedItems.value.find(
|
|
|
+ (i) => i.id === parentId,
|
|
|
+ )
|
|
|
+ if (
|
|
|
+ category &&
|
|
|
+ anyWordStartsWith(category.normalizedLabel!, normalizedSearch)
|
|
|
+ )
|
|
|
+ return true
|
|
|
+
|
|
|
+
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
@@ -520,7 +546,7 @@ const itemMatchesSearch = (item: SelectItem): boolean => {
|
|
|
const findMatchingLevel2Items = (): SelectItem[] => {
|
|
|
return normalizedItems.value.filter(
|
|
|
(item) =>
|
|
|
- item.type === 'item' && item.level === 2 && itemMatchesSearch(item),
|
|
|
+ item.type === 'item' && item.level === (props.nbLevel - 1) && itemMatchesSearch(item),
|
|
|
)
|
|
|
}
|
|
|
|
|
|
@@ -537,15 +563,26 @@ const buildSearchResultsList = (matchingItems: SelectItem[]): SelectItem[] => {
|
|
|
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
|
|
|
+ let parentId = item.parentId
|
|
|
+ if(props.nbLevel === 3){
|
|
|
+ // Trouver la sous-catégorie parente
|
|
|
+ const subcategory = normalizedItems.value.find(
|
|
|
+ (i) => i.id === parentId,
|
|
|
+ )
|
|
|
+ if (!subcategory) continue
|
|
|
+
|
|
|
+ // 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)
|
|
|
+ }
|
|
|
+ parentId = subcategory.parentId
|
|
|
+ }
|
|
|
|
|
|
// Trouver la catégorie parente
|
|
|
const category = normalizedItems.value.find(
|
|
|
- (i) => i.id === subcategory.parentId,
|
|
|
+ (i) => i.id === parentId,
|
|
|
)
|
|
|
if (!category) continue
|
|
|
|
|
|
@@ -556,13 +593,6 @@ const buildSearchResultsList = (matchingItems: SelectItem[]): SelectItem[] => {
|
|
|
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)
|
|
|
}
|
|
|
@@ -588,7 +618,8 @@ const processItemsRecursively = (
|
|
|
result.push(item)
|
|
|
if (expandedCategories.value.has(item.id)) {
|
|
|
const subcategories = normalizedItems.value.filter(
|
|
|
- (i) => i.parentId === item.id && i.type === 'subcategory',
|
|
|
+ (i) => i.parentId === item.id
|
|
|
+ && ((props.nbLevel == 2 && i.type === 'item') || (props.nbLevel == 3 && i.type === 'subcategory')),
|
|
|
)
|
|
|
processItemsRecursively(subcategories, result, true)
|
|
|
}
|