forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

data-table-search.tsx79 lines, 3.6 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 // Touch screens get a touch-sized box at 16px (index.css): below that
53 // iOS Safari zooms the page into a focused field. Compact otherwise.
54 // A pill, like every other toolbar control beside it.
55 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")}
56 />
57 <Show when={props.value}>
58 <button
59 type="button"
60 aria-label={t("common.clearSearch")}
61 onClick={() => props.onChange("")}
62 class="absolute right-1 top-1/2 flex h-8 w-8 -translate-y-1/2 items-center justify-center rounded-full text-muted-foreground outline-hidden transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring sm:h-6 sm:w-6 touch:h-8 touch:w-8"
63 >
64 <IconX class="h-4 w-4" />
65 </button>
66 </Show>
67 {/* Floated rather than inline: a static line would push every toolbar
68 that carries a search box taller, on every page. */}
69 <Show when={props.hint && focused()}>
70 <p
71 id={hintId}
72 class={cn("pointer-events-none absolute bottom-full left-0 z-30 mb-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 sm:bottom-auto sm:top-full sm:mb-0 sm:mt-1", props.hintClass)}
73 >
74 {props.hint}
75 </p>
76 </Show>
77 </div>
78 );
79}