Skip to content

Commit

Permalink
fix: make select search insensitive to special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
abelflopes committed Jul 11, 2024
1 parent 5592dfb commit 11d6c65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/components/select/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Input } from "@react-ck/input";
import classNames from "classnames";
import { useNextRender } from "@react-ck/react-utils";
import { EmptyState } from "@react-ck/empty-state";
import { getChildrenData, valueAsArray } from "./utils";
import { getChildrenData, simplifyString, valueAsArray } from "./utils";
import { type SelectProps, type ChangeHandler, type SelectOptionProps } from "./types";
import { SelectContext, type SelectContextProps } from "./context";

Expand Down Expand Up @@ -54,7 +54,7 @@ const Select = ({
.filter(
(i) =>
!search.length ||
(i.textContent && i.textContent.toLowerCase().includes(search.toLowerCase())),
(i.textContent && simplifyString(i.textContent).includes(simplifyString(search))),
)
.map((i) => i.element),
[childrenData, search],
Expand Down
17 changes: 17 additions & 0 deletions packages/components/select/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,20 @@ export const getChildrenData = (children: React.ReactNode): SelectChildrenData[]
selectOptionProps: undefined,
};
});

export const simplifyString = (s: string): string => {
let r = s.toLowerCase().trim();
r = r.replace(new RegExp("\\s", "gu"), "");
r = r.replace(new RegExp("[àáâãäå]", "gu"), "a");
r = r.replace(new RegExp("æ", "gu"), "ae");
r = r.replace(new RegExp("ç", "gu"), "c");
r = r.replace(new RegExp("[èéêë]", "gu"), "e");
r = r.replace(new RegExp("[ìíîï]", "gu"), "i");
r = r.replace(new RegExp("ñ", "gu"), "n");
r = r.replace(new RegExp("[òóôõö]", "gu"), "o");
r = r.replace(new RegExp("œ", "gu"), "oe");
r = r.replace(new RegExp("[ùúûü]", "gu"), "u");
r = r.replace(new RegExp("[ýÿ]", "gu"), "y");
// r = r.replace(new RegExp("\\W", "gu"), ""); // Special chars
return r;
};

0 comments on commit 11d6c65

Please sign in to comment.