"use client"; import * as React from "react"; import { useState, useRef } from "react"; interface SearchBarProps { value?: string; onChange?: (value: string) => void; size?: "small" | "large"; } export const SearchBar: React.FC = ({ value: propValue, onChange, size = "small", }) => { const [internalValue, setInternalValue] = useState(""); const inputRef = useRef(null); const [isFocused, setIsFocused] = useState(false); const value = propValue !== undefined ? propValue : internalValue; const handleInput = (e: React.ChangeEvent) => { const newValue = e.target.value; if (onChange) { onChange(newValue); } else { setInternalValue(newValue); } }; const clearSearch = () => { if (onChange) { onChange(""); } else { setInternalValue(""); } if (inputRef.current) { inputRef.current.focus(); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Escape") { clearSearch(); } }; return (
setIsFocused(true)} onBlur={() => setIsFocused(false)} aria-label="Search blog posts" /> {value && ( )}
); };