import { Show, createSignal, createUniqueId } from "solid-js";
import { IconSearch, IconX } from "@/components/ui/icons";
import { Input } from "@/components/ui/input";
import { cn } from "@/lib/cn";
import { useT } from "@/stores/preferences-context";
export type DataTableSearchProps = {
value: string;
onChange: (value: string) => void;
placeholder?: string;
/**
* What this particular box actually matches on, in the user's words. Shown
* under the field while it has focus — a placeholder alone never says
* whether a name, a code or a description is the thing to type. Keep it
* honest: it must list what the filter really reads, nothing more.
*/
hint?: string;
/** Optional styling for the focused hint, e.g. a wider panel search hint. */
hintClass?: string;
onKeyDown?: (event: KeyboardEvent) => void;
class?: string;
};
/** Rounded search field: leading icon, conditional clear (X) button, Escape to
* empty it, and an optional on-focus hint naming the fields it searches. */
export function DataTableSearch(props: DataTableSearchProps) {
const t = useT();
const [focused, setFocused] = createSignal(false);
const hintId = createUniqueId();
return (
setFocused(true)}
onFocusOut={() => setFocused(false)}
>
props.onChange(event.currentTarget.value)}
// Escape empties the box instead of only blurring it: a filter the
// keyboard cannot undo leaves the list silently narrowed.
onKeyDown={(event: KeyboardEvent) => {
props.onKeyDown?.(event);
if (event.defaultPrevented) return;
if (event.key !== "Escape" || !props.value) return;
event.preventDefault();
event.stopPropagation();
props.onChange("");
}}
placeholder={props.placeholder ?? t("common.search")}
aria-describedby={props.hint ? hintId : undefined}
// Touch screens get a touch-sized box at 16px (index.css): below that
// iOS Safari zooms the page into a focused field. Compact otherwise.
// A pill, like every other toolbar control beside it.
class={cn("h-10 rounded-full bg-muted/40 text-base shadow-none sm:h-8 sm:text-[13px] md:text-[13px] touch:h-10", props.value ? "pl-9 pr-10 sm:pr-8" : "pl-9")}
/>
{/* Floated rather than inline: a static line would push every toolbar
that carries a search box taller, on every page. */}