File Search
files-sdk/file-searchFreeComponent
Search box that streams matching keys with switchable match modes.
Install it
npx shadcn@latest add https://files-sdk.dev/r/file-search.jsonSource
1"use client";23import type { SearchMatch, StoredFile } from "files-sdk";4import type { UseFilesResult } from "files-sdk/react";5import { FileIcon, Loader2Icon, SearchIcon } from "lucide-react";6import { useCallback, useId, useRef, useState } from "react";78import { Button } from "@/components/ui/button";9import { Input } from "@/components/ui/input";10import { cn } from "@/lib/utils";1112export interface FileSearchProps {13 /** A `useFiles()` instance — matches are streamed through `search()`. */14 files: UseFilesResult;15 /** Limit the search to keys under this prefix. */16 prefix?: string;17 /** Match mode shown first. Default `"substring"`. */18 defaultMatch?: SearchMatch;19 /** Cap on results collected per search. Default `100`. */20 maxResults?: number;21 /** Called when a result row is clicked. */22 onSelect?: (file: StoredFile) => void;23 className?: string;24}2526const MATCH_MODES: SearchMatch[] = ["substring", "glob", "regex", "exact"];2728const formatBytes = (bytes: number): string => {29 if (bytes === 0) {30 return "0 B";31 }32 const units = ["B", "KB", "MB", "GB", "TB"];33 const exponent = Math.min(34 Math.floor(Math.log(bytes) / Math.log(1024)),35 units.length - 136 );37 return `${(bytes / 1024 ** exponent).toFixed(exponent === 0 ? 0 : 1)} ${units[exponent]}`;38};3940/**41 * A search box for a `useFiles()` instance. Streams `search()` results into a42 * list and lets you switch match mode (substring, glob, regex, exact) and toggle43 * case sensitivity. The previous search is aborted when a new one starts.44 */45export const FileSearch = ({46 files,47 prefix,48 defaultMatch = "substring",49 maxResults = 100,50 onSelect,51 className,52}: FileSearchProps) => {53 const [query, setQuery] = useState("");54 const [match, setMatch] = useState<SearchMatch>(defaultMatch);55 const [caseInsensitive, setCaseInsensitive] = useState(true);56 const [results, setResults] = useState<StoredFile[]>([]);57 const [isSearching, setIsSearching] = useState(false);58 const [hasSearched, setHasSearched] = useState(false);5960 const filesRef = useRef(files);61 filesRef.current = files;62 // A per-search controller so a new query cancels the in-flight generator.63 const controllerRef = useRef<AbortController>(null);64 const caseId = useId();6566 const run = useCallback(67 async (event: React.FormEvent) => {68 event.preventDefault();69 if (!query.trim()) {70 return;71 }72 controllerRef.current?.abort();73 const controller = new AbortController();74// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from files-sdk
All 12 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Capabilities Badgescapabilities-badges | files-sdk | Components | Free | 2 deps | |
| Dropzonedropzone | files-sdk | Components | Free | 2 deps | |
| File Actionsfile-actions | files-sdk | Components | Free | 2 deps | |
| File Browserfile-browser | files-sdk | Components | Free | 2 deps | |
| File Listfile-list | files-sdk | Components | Free | 2 deps | |
| File Previewfile-preview | files-sdk | Components | Free | 2 deps | |
| Multipart Uploadermultipart-uploader | files-sdk | Components | Free | 2 deps | |
| Share Dialogshare-dialog | files-sdk | Components | Free | 2 deps |
