import React, { useState, useMemo } from "react"; import Paper from "@mui/material/Paper"; import InputBase from "@mui/material/InputBase"; import IconButton from "@mui/material/IconButton"; import SearchIcon from "@mui/icons-material/Search"; import ClearIcon from "@mui/icons-material/Clear"; import { Box, debounce, FormControl, FormControlLabel, FormLabel, Grid, Radio, RadioGroup, Typography, } from "@mui/material"; import ChevronRightIcon from "@mui/icons-material/ChevronRight"; import { NixType, nixTypes } from "../../types/nix"; interface SelectOptionProps { label: string; handleChange: (value: string) => void; value: string; options: { value: string; label: string; }[]; } const SelectOption = (props: SelectOptionProps) => { const { label, handleChange, options, value } = props; const _handleChange = (event: React.ChangeEvent) => { const newVal = (event.target as HTMLInputElement).value as NixType; handleChange(newVal); }; return ( {label} {options.map(({ value, label }) => ( } label={label} /> ))} ); }; export interface SearchInputProps { handleSearch: (term: string) => void; handleFilter: (filter: { to: NixType; from: NixType }) => void; clearSearch: () => void; placeholder: string; } export function SearchInput(props: SearchInputProps) { const { handleSearch, clearSearch, placeholder, handleFilter } = props; const [term, setTerm] = useState(""); const [to, setTo] = useState("any"); const [from, setFrom] = useState("any"); const handleSubmit = React.useRef((input: string) => { handleSearch(input); }).current; const debouncedSubmit = useMemo( () => debounce(handleSubmit, 300), [handleSubmit] ); const _handleFilter = (t: NixType, mode: "from" | "to") => { console.log({ t, mode }); if (mode === "to") { setTo(t); handleFilter({ to: t, from }); } else { setFrom(t); handleFilter({ to, from: t }); } }; const handleClear = () => { setTerm(""); setFrom("any"); setTo("any"); clearSearch(); }; const handleChange = ( e: React.ChangeEvent ) => { setTerm(e.target.value); debouncedSubmit(e.target.value); }; return ( <> { e.preventDefault(); handleSubmit(term); }} > handleChange(e)} /> handleSubmit(term)} sx={{ p: 1, bgcolor: "primary.dark", color: "common.white", "&:hover": { backgroundColor: "primary.main", opacity: [0.9, 0.8, 0.7], }, }} aria-label="search-button" > { _handleFilter(value as NixType, "from"); }} options={nixTypes.map((v) => ({ value: v, label: v }))} /> { _handleFilter(value as NixType, "to"); }} options={nixTypes.map((v) => ({ value: v, label: v }))} /> ); }