Skip to content

Commit 43446de

Browse files
committed
add search
1 parent b292550 commit 43446de

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

components/list-table.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import { Search } from "lucide-react";
23
import {
34
Table,
45
TableBody,
@@ -10,13 +11,17 @@ import {
1011

1112
import {
1213
ColumnDef,
14+
ColumnFiltersState,
1315
SortingState,
1416
flexRender,
1517
getCoreRowModel,
18+
getFilteredRowModel,
1619
getSortedRowModel,
1720
useReactTable,
1821
} from "@tanstack/react-table";
1922

23+
import { Input } from "@/components/ui/input";
24+
2025
interface DataTableProps<TData, TValue> {
2126
columns: ColumnDef<TData, TValue>[];
2227
data: TData[];
@@ -27,20 +32,42 @@ export default function ListTable<TData, TValue>({
2732
data,
2833
}: DataTableProps<TData, TValue>) {
2934
const [sorting, setSorting] = React.useState<SortingState>([]);
35+
const [columnFilters, setColumnFilters] =
36+
React.useState<ColumnFiltersState>([]);
3037
const table = useReactTable({
3138
data,
3239
columns,
3340
getCoreRowModel: getCoreRowModel(),
3441
onSortingChange: setSorting,
3542
getSortedRowModel: getSortedRowModel(),
43+
onColumnFiltersChange: setColumnFilters,
44+
getFilteredRowModel: getFilteredRowModel(),
3645
state: {
3746
sorting,
47+
columnFilters,
3848
},
3949
});
4050
return (
41-
<div className="w-full flex justify-center px-1 sm:px-5 md:px-10 lg:px-20 pt-5">
42-
<div className="mx-auto max-w-7xl overflow-x-auto border rounded-lg">
43-
<Table>
51+
<div className="w-full flex flex-col justify-center px-2 sm:px-5 md:px-10 lg:px-20 pt-5">
52+
<div className="mx-auto max-w-7xl w-full flex flex-row items-center justify-start py-4">
53+
<Search className="w-5 h-5 mr-3" />
54+
<Input
55+
placeholder="Search products..."
56+
value={
57+
(table
58+
.getColumn("product")
59+
?.getFilterValue() as string) ?? ""
60+
}
61+
onChange={(event) =>
62+
table
63+
.getColumn("product")
64+
?.setFilterValue(event.target.value)
65+
}
66+
className="max-w-md"
67+
/>
68+
</div>
69+
<div className="mx-auto max-w-7xl w-full overflow-x-auto border rounded-lg">
70+
<Table className="w-full">
4471
<TableHeader className="bg-zinc-100">
4572
{table.getHeaderGroups().map((headerGroup) => (
4673
<TableRow key={headerGroup.id}>

components/ui/input.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as React from "react"
2+
3+
import { cn } from "@/lib/utils"
4+
5+
const Input = React.forwardRef<HTMLInputElement, React.ComponentProps<"input">>(
6+
({ className, type, ...props }, ref) => {
7+
return (
8+
<input
9+
type={type}
10+
className={cn(
11+
"flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
12+
className
13+
)}
14+
ref={ref}
15+
{...props}
16+
/>
17+
)
18+
}
19+
)
20+
Input.displayName = "Input"
21+
22+
export { Input }

0 commit comments

Comments
 (0)