diff --git a/src/components/cards/FacultySelector.tsx b/src/components/cards/FacultySelector.tsx index a952e3e..80453e9 100644 --- a/src/components/cards/FacultySelector.tsx +++ b/src/components/cards/FacultySelector.tsx @@ -1,6 +1,6 @@ 'use client'; -import React, { useEffect, useRef, useState } from 'react'; +import React, { useEffect, useState } from 'react'; import Image from 'next/image'; import { ZButton } from '../ui/Buttons'; @@ -8,7 +8,7 @@ import { data } from '@/data/faculty'; import { fullCourseData } from '@/lib/type'; import AlertModal from '../ui/AlertModal'; import { course_type_map } from '@/lib/course_codes_map'; - +import ComboBox from '@/components/ui/ComboBox'; const schools = [ 'SCOPE', 'SCORE', @@ -33,83 +33,6 @@ const schools = [ // 'MTech (Fresher)', ]; -type SelectFieldProps = { - label: string; - value: string; - options: string[]; - onChange: (val: string) => void; - renderOption?: (option: string) => string; -}; - -function SelectField({ label, value, options, onChange, renderOption }: SelectFieldProps) { - const [isOpen, setIsOpen] = useState(false); - const ref = useRef(null); - - // Close dropdown on outside click - useEffect(() => { - const handleClickOutside = (event: MouseEvent) => { - if (ref.current && !ref.current.contains(event.target as Node)) { - setIsOpen(false); - } - }; - document.addEventListener('mousedown', handleClickOutside); - return () => document.removeEventListener('mousedown', handleClickOutside); - }, []); - - const selectedLabel = value ? (renderOption ? renderOption(value) : value) : `Select ${label}`; - - return ( -
- - - {isOpen && ( - - )} -
- ); -} - type SubjectEntry = { slot: string; faculty: string; @@ -692,14 +615,14 @@ export default function FacultySelector({
- - {getCourseType(selectedSubject.split(' - ')[0]) === 'P' && !selectedSubject.split(' - ')[0].startsWith('BSTS') ? ( - { @@ -722,7 +645,7 @@ export default function FacultySelector({ )} /> ) : ( - a.localeCompare(b))} diff --git a/src/components/ui/ComboBox.tsx b/src/components/ui/ComboBox.tsx index ef4e88c..b8d49fe 100644 --- a/src/components/ui/ComboBox.tsx +++ b/src/components/ui/ComboBox.tsx @@ -7,9 +7,10 @@ type ComboBoxProps = { value: string; options: string[]; onChange: (val: string) => void; + renderOption?: (val: string) => string; }; -export default function ComboBox({ label, value, options, onChange }: ComboBoxProps) { +export default function ComboBox({ label, value, options, onChange, renderOption }: ComboBoxProps) { const [isOpen, setIsOpen] = useState(false); const [inputValue, setInputValue] = useState(''); const ref = useRef(null); @@ -24,19 +25,21 @@ export default function ComboBox({ label, value, options, onChange }: ComboBoxPr return () => document.removeEventListener('mousedown', handleClickOutside); }, []); - const filteredOptions = options.filter(option => - option.toLowerCase().includes(inputValue.toLowerCase()) - ); + const filteredOptions = options.filter(option => { + const label = renderOption ? renderOption(option) : option; + return label.toLowerCase().includes(inputValue.toLowerCase()); + }); const handleSelect = (option: string) => { + const renderedOption = renderOption ? renderOption(option) : option; onChange(option); - setInputValue(option); + setInputValue(renderedOption); setIsOpen(false); }; useEffect(() => { - if (value) setInputValue(value); - }, [value]); + if (value) setInputValue(renderOption ? renderOption(value) : value); + }, [value, renderOption]); return (
@@ -82,7 +85,7 @@ export default function ComboBox({ label, value, options, onChange }: ComboBoxPr
{isOpen && ( -
    +
      {filteredOptions.map((option, index) => (
    • - {option} + {renderOption ? renderOption(option) : option}
    • ))}