Skip to content

Commit

Permalink
Add filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
eswan18 committed Nov 26, 2023
1 parent 5c2a725 commit a91568a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 45 deletions.
106 changes: 63 additions & 43 deletions app/(protected)/account/ExerciseTypeTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import { useState } from "react";
import {
ColumnDef,
ColumnFiltersState,
flexRender,
getCoreRowModel,
useReactTable,
getFilteredRowModel,
getSortedRowModel,
SortingState,
useReactTable,
} from "@tanstack/react-table"
import {
Table,
Expand All @@ -17,6 +19,8 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table"
import { Input } from "@/components/ui/input";
import { Search } from "lucide-react";

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
Expand All @@ -28,57 +32,73 @@ export function DataTable<TData, TValue>({
data,
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = useState<SortingState>([])
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([])
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
state: { sorting },
onColumnFiltersChange: setColumnFilters,
getFilteredRowModel: getFilteredRowModel(),
state: { sorting, columnFilters },
})

return (
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
<div>
<div className="flex items-center pt-0 pb-4">
<Search size={24} className="mr-2 text-muted-foreground" />
<Input
placeholder="Filter by name..."
value={(table.getColumn("name")?.getFilterValue() as string) ?? ""}
onChange={(event) =>
table.getColumn("name")?.setFilterValue(event.target.value)
}
className="max-w-sm"
/>
</div>
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
)}
</TableBody>
</Table>
</div>
)
}
4 changes: 2 additions & 2 deletions app/(protected)/account/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export default async function AccountPage() {
<span className="text-muted-foreground">Account email:</span>
<span className="text-foreground">{me.email}</span>
</div>
<div className="flex flex-row justify-start items-center gap-2 mt-12 mb-4">
<div className="flex flex-row justify-start items-center gap-2 mt-14 mb-4">
<h2 className="text-2xl">Workout Types</h2>
<Button variant="secondary" size="sm" className="ml-2">
New <Plus size={18} className="ml-1" />
</Button>
</div>
<WorkoutTypeTable workoutTypes={workoutTypes} />
<div className="flex flex-row justify-start items-center gap-2 mt-12 mb-4">
<div className="flex flex-row justify-start items-center gap-2 mt-14 mb-4">
<h2 className="text-2xl">Exercise Types</h2>
<Button variant="secondary" size="sm" className="ml-2">
New <Plus size={18} className="ml-1" />
Expand Down

0 comments on commit a91568a

Please sign in to comment.