movie-web/src/components/SearchBar.tsx

75 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-02-13 18:49:03 +01:00
import { DropdownButton } from "./Buttons/DropdownButton";
2022-02-10 23:45:17 +01:00
import { Icons } from "./Icon";
import {
TextInputControl,
TextInputControlPropsNoLabel,
} from "./TextInputs/TextInputControl";
2022-02-07 23:22:35 +01:00
2022-02-13 18:49:03 +01:00
import { useState, useRef, useEffect } from "react";
2022-02-07 23:22:35 +01:00
export interface SearchBarProps extends TextInputControlPropsNoLabel {
buttonText?: string;
onClick?: () => void;
2022-02-10 23:45:17 +01:00
placeholder?: string;
2022-02-07 23:22:35 +01:00
}
export function SearchBarInput(props: SearchBarProps) {
2022-02-13 18:49:03 +01:00
const [dropdownOpen, setDropdownOpen] = useState(false);
const [dropdownSelected, setDropdownSelected] = useState("movie");
const dropdownRef = useRef<any>();
const handleClick = (e: MouseEvent) => {
if (dropdownRef.current?.contains(e.target as Node)) {
// inside click
return;
}
// outside click
closeDropdown();
};
const closeDropdown = () => {
setDropdownOpen(false);
};
2022-02-07 23:22:35 +01:00
return (
2022-02-13 18:49:03 +01:00
<div className="flex flex-col sm:flex-row items-center gap-4 px-4 py-4 sm:pl-8 sm:pr-2 sm:py-2 bg-denim-300 rounded-[28px] hover:bg-denim-400 focus-within:bg-denim-400 transition-colors">
2022-02-10 23:45:17 +01:00
<TextInputControl
onChange={props.onChange}
value={props.value}
2022-02-13 18:49:03 +01:00
className="placeholder-denim-700 w-full bg-transparent flex-1 focus:outline-none text-white"
2022-02-10 23:45:17 +01:00
placeholder={props.placeholder}
/>
2022-02-13 18:49:03 +01:00
<DropdownButton
icon={Icons.SEARCH}
open={dropdownOpen}
setOpen={setDropdownOpen}
selectedItem={dropdownSelected}
setSelectedItem={setDropdownSelected}
options={[
{
id: "movie",
name: "Movie",
icon: Icons.FILM,
},
{
id: "series",
name: "Series",
icon: Icons.CLAPPER_BOARD,
},
{
id: "anime",
name: "Anime",
icon: Icons.DRAGON,
},
]}
onClick={() => setDropdownOpen((old) => !old)}
ref={dropdownRef}
>
2022-02-10 23:45:17 +01:00
{props.buttonText || "Search"}
2022-02-13 18:49:03 +01:00
</DropdownButton>
2022-02-07 23:22:35 +01:00
</div>
2022-02-10 23:45:17 +01:00
);
2022-02-07 23:22:35 +01:00
}