|
1 | 1 | import { type Table, flexRender } from "@tanstack/react-table"; |
2 | | -import { useState } from "react"; |
| 2 | +import { type JSX, useState } from "react"; |
3 | 3 | import { SearchInput } from "./SearchInput"; |
4 | 4 | import type { User } from "./userData"; |
5 | 5 |
|
| 6 | +const Cell = ({ className, ...props }: JSX.IntrinsicElements["div"]) => ( |
| 7 | + <div {...props} className={`table-cell py-1 px-2 text-center ${className}`} /> |
| 8 | +); |
| 9 | + |
6 | 10 | type Props = { |
7 | 11 | table: Table<User>; |
8 | 12 | }; |
@@ -39,11 +43,15 @@ export const UserTable = ({ table }: Props) => { |
39 | 43 | <div className="table border-2 border-gray-200 rounded-md "> |
40 | 44 | <div className="table-header-group bg-slate-200"> |
41 | 45 | <div className="table-row"> |
| 46 | + <Cell> |
| 47 | + <input |
| 48 | + type="checkbox" |
| 49 | + checked={table.getIsAllRowsSelected()} |
| 50 | + onChange={table.getToggleAllRowsSelectedHandler()} |
| 51 | + /> |
| 52 | + </Cell> |
42 | 53 | {table.getFlatHeaders().map((header) => ( |
43 | | - <div |
44 | | - key={header.id} |
45 | | - className="table-cell py-2 px-2 text-center space-y-1" |
46 | | - > |
| 54 | + <Cell key={header.id} className="space-y-1"> |
47 | 55 | <button |
48 | 56 | className="font-semibold" |
49 | 57 | type="button" |
@@ -115,13 +123,20 @@ export const UserTable = ({ table }: Props) => { |
115 | 123 | /> |
116 | 124 | )} |
117 | 125 | </div> |
118 | | - </div> |
| 126 | + </Cell> |
119 | 127 | ))} |
120 | 128 | </div> |
121 | 129 | </div> |
122 | 130 | <div className="table-row-group"> |
123 | 131 | {table.getRowModel().rows.map((row) => ( |
124 | 132 | <div key={row.id} className="table-row"> |
| 133 | + <div className="table-cell py-1 px-2 text-center"> |
| 134 | + <input |
| 135 | + type="checkbox" |
| 136 | + checked={row.getIsSelected()} |
| 137 | + onChange={row.getToggleSelectedHandler()} |
| 138 | + /> |
| 139 | + </div> |
125 | 140 | {row.getVisibleCells().map((cell) => ( |
126 | 141 | <div |
127 | 142 | key={cell.id} |
@@ -215,6 +230,7 @@ export const UserTable = ({ table }: Props) => { |
215 | 230 | pagination: table.getState().pagination, |
216 | 231 | columnFilters: table.getState().columnFilters, |
217 | 232 | columnOrder: table.getState().columnOrder, |
| 233 | + rowSelection: table.getState().rowSelection, |
218 | 234 | }, |
219 | 235 | null, |
220 | 236 | 2, |
|
0 commit comments