Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajoute fuse dans la recherche des Select du design system #3498

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const SelectBase = (props: SelectProps) => {
? options
: filterOptions(options, inputValue);

/** Compare la valeur de l'input de recherche avec la première optin de la liste
/** Compare la valeur de l'input de recherche avec la première option de la liste
* pour afficher le bouton de création d'une option */
const isNotSimilar =
inputValue.toLowerCase().trim() !==
Expand Down
58 changes: 36 additions & 22 deletions packages/ui/src/design-system/Select/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Fuse from 'fuse.js';

import {
ITEM_ALL,
itemAllOption,
Expand Down Expand Up @@ -91,32 +93,44 @@ export const sortOptionByAlphabet = (
export const filterOptions = (
options: SelectOption[],
filterValue: string
): SelectOption[] =>
options.reduce((acc: SelectOption[], currentOption) => {
if (isSingleOption(currentOption)) {
): SelectOption[] => {
if (filterValue.trim().length > 0) {
const flatOptions = getFlatOptions(options);

const fuse = new Fuse(flatOptions, {
keys: ['label'],
threshold: 0.5,
shouldSort: false,
});

const fusedOptions = fuse.search(filterValue).map((r) => r.item);

return options.reduce((acc: SelectOption[], currentOption) => {
if (
currentOption.label.toLowerCase().includes(filterValue.toLowerCase())
isSingleOption(currentOption) &&
fusedOptions.includes(currentOption)
) {
return [...acc, currentOption];
}
}

if (isOptionSection(currentOption)) {
const filteredOptions = currentOption.options.filter((option) =>
option.label.toLowerCase().includes(filterValue.toLowerCase())
);
if (filteredOptions.length > 0) {
return [
...acc,
{
title: currentOption.title,
options: filteredOptions,
},
];
} else {
return acc;
if (isOptionSection(currentOption)) {
const filteredOptions = currentOption.options.filter((option) =>
fusedOptions.includes(option)
);
return filteredOptions.length > 0
? [
...acc,
{
title: currentOption.title,
options: filteredOptions,
},
]
: acc;
}
}

return acc;
}, []);
return acc;
}, []);
} else {
return options;
}
};
Loading