import React, { useState } from 'react'; import { useCombobox } from 'downshift'; import { List, ListItem, ListItemPrefix, Card, Typography, } from '@material-tailwind/react'; import SearchBar from '../SearchBar'; import { ProjectDetails } from '../../types/project'; import projectsData from '../../assets/projects.json'; interface ProjectsSearchProps { onChange?: (data: ProjectDetails) => void; } const ProjectSearchBar = ({ onChange }: ProjectsSearchProps) => { const [items, setItems] = useState([]); const [selectedItem, setSelectedItem] = useState(null); const { isOpen, getMenuProps, getInputProps, getItemProps, highlightedIndex, inputValue, } = useCombobox({ onInputValueChange({ inputValue }) { setItems( inputValue ? projectsData.filter((project) => project.title.toLowerCase().includes(inputValue.toLowerCase()), ) : [], ); }, items, itemToString(item) { return item ? item.title : ''; }, selectedItem, onSelectedItemChange: ({ selectedItem: newSelectedItem }) => { if (newSelectedItem) { setSelectedItem(newSelectedItem); if (onChange) { onChange(newSelectedItem); } } }, }); return (
{items.length ? ( <>
Suggestions
{items.map((item, index) => ( ^
{item.title} {item.organization}
))} ) : (
^ No projects matching this name
)}
); }; export default ProjectSearchBar;