From e0cd4fc1b580ab1bd0b3bc588d0ee10287050109 Mon Sep 17 00:00:00 2001 From: jsun969 Date: Tue, 22 Oct 2024 18:41:07 +1030 Subject: [PATCH] feat: search by course abbreviation --- src/components/SearchForm.tsx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/components/SearchForm.tsx b/src/components/SearchForm.tsx index 39adce2..ec0a1b3 100644 --- a/src/components/SearchForm.tsx +++ b/src/components/SearchForm.tsx @@ -59,6 +59,17 @@ export const SearchForm = () => { })) ?? []; const [selectedCourseId, setSelectedCourseId] = useState(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) => { e.preventDefault(); const course = courses?.find((c) => c.id === selectedCourseId); @@ -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) => (