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'; interface ProjectsSearchProps { projects: ProjectDetails[]; onChange?: (data: ProjectDetails) => void; } const ProjectSearchBar = ({ projects, onChange }: ProjectsSearchProps) => { const [items, setItems] = useState([]); const [selectedItem, setSelectedItem] = useState(null); const { isOpen, getMenuProps, getInputProps, getItemProps, highlightedIndex, inputValue, } = useCombobox({ onInputValueChange({ inputValue }) { setItems( inputValue ? projects.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;