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

Filter text isn't trimmed #697

Open
samal-rasmussen opened this issue Jul 10, 2024 · 2 comments
Open

Filter text isn't trimmed #697

samal-rasmussen opened this issue Jul 10, 2024 · 2 comments

Comments

@samal-rasmussen
Copy link

samal-rasmussen commented Jul 10, 2024

Steps to reproduce:
Open https://svelte-select-examples.vercel.app/examples/props/filter-text
Write " one" with a leading space character.

Expected result: The One option is shown.
Actual result: Nothing is shown.

@samal-rasmussen
Copy link
Author

patch-package patch:

@@ -0,0 +1,13 @@
diff --git a/node_modules/svelte-select/Select.svelte b/node_modules/svelte-select/Select.svelte
index 88f875a..e44a24b 100644
--- a/node_modules/svelte-select/Select.svelte
+++ b/node_modules/svelte-select/Select.svelte
@@ -31,7 +31,7 @@
     export let placeholderAlwaysShow = false;
     export let items = null;
     export let label = 'label';
-    export let itemFilter = (label, filterText, option) => `${label}`.toLowerCase().includes(filterText.toLowerCase());
+    export let itemFilter = (label, filterText, option) => `${label}`.toLowerCase().includes(filterText.toLowerCase().trim());
     export let groupBy = undefined;
     export let groupFilter = (groups) => groups;
     export let groupHeaderSelectable = false;

@samal-rasmussen
Copy link
Author

samal-rasmussen commented Jul 10, 2024

Also if you like to disregard accents in your match:

@@ -0,0 +1,24 @@
diff --git a/node_modules/svelte-select/Select.svelte b/node_modules/svelte-select/Select.svelte
index 88f875a..62ee7fa 100644
--- a/node_modules/svelte-select/Select.svelte
+++ b/node_modules/svelte-select/Select.svelte
@@ -31,7 +31,18 @@
     export let placeholderAlwaysShow = false;
     export let items = null;
     export let label = 'label';
-    export let itemFilter = (label, filterText, option) => `${label}`.toLowerCase().includes(filterText.toLowerCase());
+    export let itemFilter = (label, filterText, option) => {
+        const l = `${label}`.toLowerCase();
+        const f = filterText.toLowerCase().trim();
+        const unNormalizedMatch = l.includes(f);
+        const normalizedMatch =
+            l.normalize('NFD')
+            .replace(/\p{Diacritic}/gu, "")
+            .includes(
+                f.normalize('NFD').replace(/\p{Diacritic}/gu, "")
+            );
+        return unNormalizedMatch || normalizedMatch;
+    };
     export let groupBy = undefined;
     export let groupFilter = (groups) => groups;
     export let groupHeaderSelectable = false;

https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant