forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

data-table-search.tsx76 lines, 3.1 KiBHistoryRawDownload
1import { Show, createSignal, createUniqueId } from "solid-js";
2import { IconSearch, IconX } from "@/components/ui/icons";
3import { Input } from "@/components/ui/input";
4import { cn } from "@/lib/cn";
5import { useT } from "@/stores/preferences-context";
6
7export type DataTableSearchProps = {
8 value: string;
9 onChange: (value: string) => void;
10 placeholder?: string;
11 /**
12 * What this particular box actually matches on, in the user's words. Shown
13 * under the field while it has focus — a placeholder alone never says
14 * whether a name, a code or a description is the thing to type. Keep it
15 * honest: it must list what the filter really reads, nothing more.
16 */
17 hint?: string;
18 /** Optional styling for the focused hint, e.g. a wider panel search hint. */
19 hintClass?: string;
20 onKeyDown?: (event: KeyboardEvent) => void;
21 class?: string;
22};
23
24/** Rounded search field: leading icon, conditional clear (X) button, Escape to
25 * empty it, and an optional on-focus hint naming the fields it searches. */
26export function DataTableSearch(props: DataTableSearchProps) {
27 const t = useT();
28 const [focused, setFocused] = createSignal(false);
29 const hintId = createUniqueId();
30 return (
31 <div
32 class={cn("relative w-full sm:max-w-xs", props.class)}
33 onFocusIn={() => setFocused(true)}
34 onFocusOut={() => setFocused(false)}
35 >
36 <IconSearch class="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
37 <Input
38 value={props.value}
39 onInput={(event) => props.onChange(event.currentTarget.value)}
40 // Escape empties the box instead of only blurring it: a filter the
41 // keyboard cannot undo leaves the list silently narrowed.
42 onKeyDown={(event: KeyboardEvent) => {
43 props.onKeyDown?.(event);
44 if (event.defaultPrevented) return;
45 if (event.key !== "Escape" || !props.value) return;
46 event.preventDefault();
47 event.stopPropagation();
48 props.onChange("");
49 }}
50 placeholder={props.placeholder ?? t("common.search")}
51 aria-describedby={props.hint ? hintId : undefined}
52 class={cn("h-8 rounded-lg bg-muted/40 text-[13px] md:text-[13px]", props.value ? "pl-9 pr-8" : "pl-9")}
53 />
54 <Show when={props.value}>
55 <button
56 type="button"
57 aria-label={t("common.clearSearch")}
58 onClick={() => props.onChange("")}
59 class="absolute right-2.5 top-1/2 -translate-y-1/2 text-muted-foreground transition-colors hover:text-foreground"
60 >
61 <IconX class="h-4 w-4" />
62 </button>
63 </Show>
64 {/* Floated rather than inline: a static line would push every toolbar
65 that carries a search box taller, on every page. */}
66 <Show when={props.hint && focused()}>
67 <p
68 id={hintId}
69 class={cn("absolute left-0 top-full z-30 mt-1 w-full min-w-max max-w-[22rem] rounded-lg border border-border/80 bg-popover px-2.5 py-1.5 text-[11px] leading-snug text-muted-foreground shadow-md", props.hintClass)}
70 >
71 {props.hint}
72 </p>
73 </Show>
74 </div>
75 );
76}