Files
DevToysWeb/components/sidebar/search-bar.tsx
rusconn f1c3bd2971 renewal
recreate project by using https://github.com/shadcn/next-template

App:
- support dark mode
- add toggle theme button
- add clear search button
- add search button
- add current page indicator
- add tool group pages
- add settings tool
- add 1 tab format option to Json format tool
- add paste button to some tools
- add file button to some tools
- add copy button to some tools
- add clear button to some tools
- change favicon
- change search hit rate
- change each page title
- change icons from Material Icons to Lucide
- change sidebar scroll area
- change editor from Ace to Monaco
- change parsable separators of number base converter
- change default value of format option of number base converter
- change default values of some tool forms
- change some styles
- remove disabled tools
- remove real-time search
- fix uri encoding tool

Dev:
- MUI + Emotion -> Radix UI + Tailwind CSS
- Next.js 12 Pages -> Next.js 13 App Router
- React 17 -> React 18
- many other packages upgraded
- use useState instead of recoil
- use Next.js typedRoutes instead of pathpida
- clean npm scripts
- format import statements by Prettier
- no component separations between container and presenter
- effective component memoizations
- add vscode settings
- many refactors
2023-05-28 23:56:43 +09:00

65 lines
1.8 KiB
TypeScript

"use client";
import { useMemo, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import { cn } from "@/lib/style";
import { Button, ButtonProps } from "@/components/ui/button";
import { Input, InputProps } from "@/components/ui/input";
import { icons } from "@/components/icons";
export function SearchBar() {
const router = useRouter();
const inputRef = useRef<HTMLInputElement>(null);
const [text, setText] = useState("");
const search = () => {
if (text.trim()) {
router.push(`/search?q=${text.trim()}`);
}
};
const changeText: InputProps["onChange"] = ({ currentTarget }) => setText(currentTarget.value);
const searchIfEnter: InputProps["onKeyDown"] = ({ code }) => {
if (code === "Enter") {
search();
}
};
const clearText: ButtonProps["onClick"] = () => {
setText("");
inputRef.current?.focus();
};
const clearIcon = useMemo(() => <icons.X className="p-1 text-muted-foreground" />, []);
const searchIcon = useMemo(
() => <icons.Search className="-scale-x-100 p-1 text-muted-foreground" />,
[]
);
return (
<div className="relative flex w-full items-center">
<Input
ref={inputRef}
className="flex-1 pr-16 font-sans"
value={text}
onChange={changeText}
onKeyDown={searchIfEnter}
placeholder="Type to search for tools…"
/>
<div className="absolute right-1 flex gap-1">
<Button className={cn("h-6 p-0", !text && "hidden")} variant="ghost" onClick={clearText}>
{clearIcon}
<span className="sr-only">Clear search text</span>
</Button>
<Button className="h-6 p-0" variant="ghost" onClick={search} aria-label="search">
{searchIcon}
<span className="sr-only">Search tools</span>
</Button>
</div>
</div>
);
}