Skip to content
Open
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
8 changes: 6 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": ["eslint:recommended", "plugin:react/recommended", "prettier"],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"prettier"
],
"env": {
"browser": true,
"node": true,
Expand All @@ -15,4 +19,4 @@
"rules": {
"react/react-in-jsx-scope": "off"
}
}
}
21 changes: 15 additions & 6 deletions src/lib/fuzzySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ function search(q, text) {
return true;
}

export default function fuzzySearch(options, query) {
export function fuzzySearch(options, query) {
return !query.length
? options
: options.filter((o) =>
search(
query.toLowerCase(),
`${o.name} ${o.group || ''}`.trim().toLowerCase(),
),
);
search(
query.toLowerCase(),
`${o.name} ${o.group || ''}`.trim().toLowerCase(),
),
);
}

export function fulltextSearch(options, query) {
return !query.length
? options
: options?.filter((o) =>
`${o?.name}`.trim().toLowerCase().indexOf(query.toLowerCase()) == 0
);
}

4 changes: 2 additions & 2 deletions src/useSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import updateOption from './lib/updateOption';
import getDisplayValue from './lib/getDisplayValue';
import getValue from './lib/getValue';
import groupOptions from './lib/groupOptions';
import fuzzySearch from './lib/fuzzySearch';
import {fuzzySearch,fulltextSearch} from './lib/fuzzySearch';
import reduce from './lib/reduce';
import useOptions from './useOptions';
import useHighlight from './useHighlight';
Expand Down Expand Up @@ -59,7 +59,7 @@ export default function useSelect({
};

const middleware = [
useFuzzySearch ? fuzzySearch : null,
useFuzzySearch ? fuzzySearch : fulltextSearch,
...(filterOptions ? filterOptions : []),
];
const filteredOptions = groupOptions(reduce(middleware, options, q));
Expand Down