diff --git a/src/hooks/use-media-query.ts b/src/hooks/use-media-query.ts new file mode 100644 index 0000000..5226760 --- /dev/null +++ b/src/hooks/use-media-query.ts @@ -0,0 +1,13 @@ +import * as React from "react" + +export function useMediaQuery(query: string) { + const subscribe = React.useCallback( + (onChange: () => void) => { + const mql = window.matchMedia(query) + mql.addEventListener("change", onChange) + return () => mql.removeEventListener("change", onChange) + }, + [query], + ) + return React.useSyncExternalStore(subscribe, () => window.matchMedia(query).matches) +} diff --git a/src/kit/index.ts b/src/kit/index.ts index a6a1cd6..af64a03 100644 --- a/src/kit/index.ts +++ b/src/kit/index.ts @@ -14,5 +14,6 @@ export * from "./pagination" export * from "./patient-avatar" export * from "./patient-panel" export * from "./person-chip" +export * from "./search-input" export * from "./stat-tile" export * from "./status-badge" diff --git a/src/kit/search-input.tsx b/src/kit/search-input.tsx new file mode 100644 index 0000000..f648cf5 --- /dev/null +++ b/src/kit/search-input.tsx @@ -0,0 +1,71 @@ +import * as React from "react" +import { SearchIcon, XIcon } from "lucide-react" + +import { cn } from "@/lib/utils" + +/** Поле поиска на мягкой подложке: иконка, крестик очистки. */ +export const SearchInput = React.forwardRef< + HTMLInputElement, + Omit, "value" | "onChange" | "size"> & { + value: string + onValueChange: (value: string) => void + size?: "sm" | "md" | "lg" + /** Что показать справа, пока поле пустое (подсказка клавиши). */ + trailing?: React.ReactNode + } +>(function SearchInput({ value, onValueChange, size = "md", trailing, className, ...props }, ref) { + const innerRef = React.useRef(null) + React.useImperativeHandle(ref, () => innerRef.current!) + + return ( +
+ + onValueChange(e.target.value)} + onKeyDown={(e) => { + if (e.key === "Escape" && value) { + e.stopPropagation() + onValueChange("") + } + props.onKeyDown?.(e) + }} + className={cn( + "w-full rounded-lg border border-transparent bg-muted/70 transition outline-none placeholder:text-muted-foreground/80 [&::-webkit-search-cancel-button]:hidden", + "focus:border-primary/40 focus:bg-card focus:ring-4 focus:ring-primary/10", + size === "sm" && "h-8 pr-8 pl-9 text-sm", + size === "md" && "h-9 pr-8 pl-9 text-sm", + size === "lg" && "h-12 rounded-xl pr-12 pl-12 text-base", + )} + {...props} + /> +
+ {value ? ( + + ) : ( + trailing + )} +
+
+ ) +})