Spaces:
Sleeping

MicProtocol / packages /ui /src /components /SearchInput.tsx
Benaah's picture
Upload folder using huggingface_hub
43842af verified
Raw History Blame Contribute Delete
3.58 kB
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<HTMLInputElement>) => {
let clean = sanitizeText(e.target.value, {
maxLength,
trim: false,
allowNewlines: false,
});
onChange(clean);
};
const handlePaste = (e: React.ClipboardEvent<HTMLInputElement>) => {
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 (
<div
className="micprotocol-search-input"
style={{
display: "flex",
alignItems: "center",
gap: "var(--space-2)",
padding: "var(--space-2) var(--space-3)",
background: "var(--bg-elevated)",
borderRadius: "var(--radius-lg)",
border: "1px solid var(--border-color-subtle)",
fontSize: "var(--text-sm)",
...style,
}}
>
<svg
width="14"
height="14"
viewBox="0 0 24 24"
fill="none"
stroke="var(--text-tertiary)"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
style={{ flexShrink: 0 }}
>
<circle cx="11" cy="11" r="8" />
<path d="m21 21-4.3-4.3" />
</svg>
<input
type="search"
value={value}
maxLength={maxLength}
onChange={handleChange}
onPaste={handlePaste}
onKeyDown={(e) => {
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 && (
<button
type="button"
onClick={() => onChange("")}
style={{
background: "none",
border: "none",
cursor: "pointer",
color: "var(--text-tertiary)",
padding: 0,
display: "flex",
alignItems: "center",
flexShrink: 0,
}}
aria-label="Clear search"
>
<svg
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M18 6 6 18" />
<path d="m6 6 12 12" />
</svg>
</button>
)}
</div>
);
}