import { Autocomplete, TextField } from "@mui/material"; import { useEffect, useState } from "react"; import { ajax } from "./functions"; export interface ISearchProps { library: string; onChange(library: string): void; } export default function Search(props: ISearchProps) { const [value, setValue] = useState(props.library); const [inputValue, setInputValue] = useState(""); const [options, setOptions] = useState([]); useEffect(() => { (async () => { if (inputValue.length > 2) { const libraries = await ajax("searchLibrary?term=" + encodeURIComponent(inputValue)); setOptions([...libraries]); } })(); }, [inputValue]); useEffect(() => { if (!open) { setOptions([]); } }, [open]); return ( x} fullWidth filterSelectedOptions noOptionsText={inputValue.length > 2 ? "Not found" : "Keep writing"} value={value} options={options} onChange={(_event: any, newValue: string | null) => { setOptions(newValue ? [newValue, ...options] : options); setValue(newValue); if (newValue) props.onChange(newValue); }} onInputChange={(_event, newInputValue) => { setInputValue(newInputValue); }} renderInput={(params) => ( )} /> ); }