feat: Add search functionality to the ListPane component which exposes it for
- Resources - Prompts - Tools
This commit is contained in:
parent
24ef31905e
commit
fbce75aba0
1 changed files with 104 additions and 33 deletions
|
|
@ -1,4 +1,7 @@
|
||||||
|
import { Search } from "lucide-react";
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
|
import { Input } from "./ui/input";
|
||||||
|
import { useState, useMemo, useRef } from "react";
|
||||||
|
|
||||||
type ListPaneProps<T> = {
|
type ListPaneProps<T> = {
|
||||||
items: T[];
|
items: T[];
|
||||||
|
|
@ -20,41 +23,109 @@ const ListPane = <T extends object>({
|
||||||
title,
|
title,
|
||||||
buttonText,
|
buttonText,
|
||||||
isButtonDisabled,
|
isButtonDisabled,
|
||||||
}: ListPaneProps<T>) => (
|
}: ListPaneProps<T>) => {
|
||||||
<div className="bg-card border border-border rounded-lg shadow">
|
const [searchQuery, setSearchQuery] = useState("");
|
||||||
<div className="p-4 border-b border-gray-200 dark:border-border">
|
const [isSearchExpanded, setIsSearchExpanded] = useState(false);
|
||||||
<h3 className="font-semibold dark:text-white">{title}</h3>
|
const searchInputRef = useRef<HTMLInputElement>(null);
|
||||||
</div>
|
|
||||||
<div className="p-4">
|
const filteredItems = useMemo(() => {
|
||||||
<Button
|
if (!searchQuery.trim()) return items;
|
||||||
variant="outline"
|
|
||||||
className="w-full mb-4"
|
return items.filter((item) => {
|
||||||
onClick={listItems}
|
const searchableText = JSON.stringify(item).toLowerCase();
|
||||||
disabled={isButtonDisabled}
|
return searchableText.includes(searchQuery.toLowerCase());
|
||||||
>
|
});
|
||||||
{buttonText}
|
}, [items, searchQuery]);
|
||||||
</Button>
|
|
||||||
<Button
|
const handleSearchClick = () => {
|
||||||
variant="outline"
|
setIsSearchExpanded(true);
|
||||||
className="w-full mb-4"
|
setTimeout(() => {
|
||||||
onClick={clearItems}
|
searchInputRef.current?.focus();
|
||||||
disabled={items.length === 0}
|
}, 100);
|
||||||
>
|
};
|
||||||
Clear
|
|
||||||
</Button>
|
const handleSearchBlur = () => {
|
||||||
<div className="space-y-2 overflow-y-auto max-h-96">
|
if (!searchQuery.trim()) {
|
||||||
{items.map((item, index) => (
|
setIsSearchExpanded(false);
|
||||||
<div
|
}
|
||||||
key={index}
|
};
|
||||||
className="flex items-center py-2 px-4 rounded hover:bg-gray-50 dark:hover:bg-secondary cursor-pointer"
|
|
||||||
onClick={() => setSelectedItem(item)}
|
return (
|
||||||
>
|
<div className="bg-card border border-border rounded-lg shadow">
|
||||||
{renderItem(item)}
|
<div className="p-4 border-b border-gray-200 dark:border-border">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h3 className="font-semibold dark:text-white">{title}</h3>
|
||||||
|
<div className="relative flex items-center">
|
||||||
|
<button
|
||||||
|
onClick={handleSearchClick}
|
||||||
|
className={`p-2 hover:bg-gray-100 dark:hover:bg-secondary rounded-md transition-all duration-300 ease-in-out ${
|
||||||
|
isSearchExpanded
|
||||||
|
? "opacity-0 scale-75 pointer-events-none"
|
||||||
|
: "opacity-100 scale-100"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<Search className="w-4 h-4 text-muted-foreground" />
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div
|
||||||
|
className={`absolute right-0 transition-all duration-300 ease-in-out ${
|
||||||
|
isSearchExpanded
|
||||||
|
? "opacity-100 translate-x-0 w-96"
|
||||||
|
: "opacity-0 translate-x-4 w-0"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div className="flex items-center">
|
||||||
|
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 w-4 h-4 text-muted-foreground pointer-events-none z-10" />
|
||||||
|
<Input
|
||||||
|
ref={searchInputRef}
|
||||||
|
type="text"
|
||||||
|
placeholder="Search..."
|
||||||
|
value={searchQuery}
|
||||||
|
onChange={(e) => setSearchQuery(e.target.value)}
|
||||||
|
onBlur={handleSearchBlur}
|
||||||
|
className="pl-10 w-full transition-all duration-300 ease-in-out"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="p-4">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="w-full mb-4"
|
||||||
|
onClick={listItems}
|
||||||
|
disabled={isButtonDisabled}
|
||||||
|
>
|
||||||
|
{buttonText}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="w-full mb-4"
|
||||||
|
onClick={clearItems}
|
||||||
|
disabled={items.length === 0}
|
||||||
|
>
|
||||||
|
Clear
|
||||||
|
</Button>
|
||||||
|
<div className="space-y-2 overflow-y-auto max-h-96">
|
||||||
|
{filteredItems.map((item, index) => (
|
||||||
|
<div
|
||||||
|
key={index}
|
||||||
|
className="flex items-center py-2 px-4 rounded hover:bg-gray-50 dark:hover:bg-secondary cursor-pointer"
|
||||||
|
onClick={() => setSelectedItem(item)}
|
||||||
|
>
|
||||||
|
{renderItem(item)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
{filteredItems.length === 0 && searchQuery && items.length > 0 && (
|
||||||
|
<div className="text-center py-4 text-muted-foreground">
|
||||||
|
No items found matching "{searchQuery}"
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
);
|
||||||
);
|
};
|
||||||
|
|
||||||
export default ListPane;
|
export default ListPane;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue