import React from "react"; import { SAFE_MAX_LENGTHS, sanitizeText } from "../security"; export interface SearchInputProps { /** Current value */ value: string; /** Change handler */ onChange: (value: string) => void; /** Placeholder text */ placeholder?: string; /** Optional submit handler (adds Enter key support) */ onSubmit?: () => void; /** Max character length (default: 120) */ maxLength?: number; /** Additional inline styles */ style?: React.CSSProperties; } /** * Hardened SearchInput with boundary enforcement, null byte stripping, and XSS defense. */ export function SearchInput({ value, onChange, placeholder = "Search...", onSubmit, maxLength = SAFE_MAX_LENGTHS.search, style, }: SearchInputProps) { const handleChange = (e: React.ChangeEvent) => { let clean = sanitizeText(e.target.value, { maxLength, trim: false, allowNewlines: false, }); onChange(clean); }; const handlePaste = (e: React.ClipboardEvent) => { const text = e.clipboardData.getData("text"); const clean = sanitizeText(text, { maxLength, trim: false, allowNewlines: false, }); if (clean !== text) { e.preventDefault(); document.execCommand("insertText", false, clean); } }; return (
{ if (e.key === "Enter" && onSubmit) onSubmit(); }} placeholder={placeholder} aria-label={placeholder} autoComplete="off" spellCheck={false} style={{ border: "none", background: "transparent", outline: "none", fontSize: "var(--text-sm)", width: "100%", color: "var(--text-primary)", fontFamily: "var(--font-sans)", }} /> {value && ( )}
); }