Skip to content

Commit

Permalink
feat: search by course abbreviation
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun969 committed Oct 22, 2024
1 parent 0586dfb commit e0cd4fc
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/components/SearchForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ export const SearchForm = () => {
})) ?? [];
const [selectedCourseId, setSelectedCourseId] = useState<Key | null>(null);

const courseSearchFilter = (text: string, input: string) => {
text = text.normalize('NFC');
const courseName = text.split(' - ')[1];
const courseAbbr = (
courseName.match(/[A-Z]/g)?.join('') ?? ''
).toLowerCase();
text = text.toLocaleLowerCase();
input = input.normalize('NFC').toLocaleLowerCase();
return text.includes(input) || courseAbbr.includes(input);
};

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const course = courses?.find((c) => c.id === selectedCourseId);
Expand Down Expand Up @@ -117,6 +128,7 @@ export const SearchForm = () => {
onSelectionChange={setSelectedCourseId}
disabledKeys={enrolledCourses.courses.map((c) => c.id)}
listboxProps={{ emptyContent: t('search.course-not-found') }}
defaultFilter={courseSearchFilter}
>
{(course) => (
<AutocompleteItem key={course.id} value={course.id}>
Expand Down

0 comments on commit e0cd4fc

Please sign in to comment.