Skip to content

Commit

Permalink
Merge branch 'luke/majors-wip' of github.com:sandboxnu/graduatenu int…
Browse files Browse the repository at this point in the history
…o luke/majors-wip
  • Loading branch information
AlpacaFur committed Apr 14, 2024
2 parents 55c3d92 + 969a3ec commit 2596a87
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions packages/frontend/components/Form/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
FormHelperText,
} from "@chakra-ui/react";
import { OptionObject } from "@graduate/common";
import Fuse from "fuse.js";
import { Control, FieldError, useController } from "react-hook-form";
import Select from "react-select";
import { FilterOptionOption } from "react-select/dist/declarations/src/filters";
Expand Down Expand Up @@ -46,28 +45,40 @@ export const PlanSelect: React.FC<PlanSelectProps> = ({
isSearchable,
isDisabled,
placeholder,
useFuzzySearch,
// useFuzzySearch,
}) => {
const filterOptions = useFuzzySearch
? (option: FilterOptionOption<any>, inputValue: string) => {
if (inputValue.length !== 0) {
const list = new Fuse(options, {
keys: ["value"],
isCaseSensitive: false,
shouldSort: true,
ignoreLocation: true,
findAllMatches: true,
includeScore: true,
threshold: 0.4,
}).search(inputValue);
return list
.map((element) => element.item.value)
.includes(option.data.value);
} else {
return true;
}
}
: null;
const customFilterOption = (
option: FilterOptionOption<any>,
rawInput: string
) => {
const words = rawInput.split(" ");
return words.reduce(
(acc, cur) =>
acc && option.label.toLowerCase().includes(cur.toLowerCase()),
true
);
};
// TODO: Find a more efficient way to implement fuzzy search
// const filterOptions = useFuzzySearch
// ? (option: FilterOptionOption<any>, inputValue: string) => {
// if (inputValue.length !== 0) {
// const list = new Fuse(options, {
// keys: ["value"],
// isCaseSensitive: false,
// shouldSort: true,
// ignoreLocation: true,
// findAllMatches: true,
// includeScore: true,
// threshold: 0.4,
// }).search(inputValue);
// return list
// .map((element) => element.item.value)
// .includes(option.data.value);
// } else {
// return true;
// }
// }
// : null;

const {
field: { onChange: onChangeUpdateValue, value, ...fieldRest },
Expand Down Expand Up @@ -110,7 +121,7 @@ export const PlanSelect: React.FC<PlanSelectProps> = ({
isSearchable={isSearchable}
isDisabled={isDisabled}
placeholder={placeholder}
filterOption={filterOptions}
filterOption={customFilterOption}
{...fieldRest}
/>
{helperText && <FormHelperText>{helperText}</FormHelperText>}
Expand Down

0 comments on commit 2596a87

Please sign in to comment.