select checkboxes

This commit is contained in:
andrzej 2024-06-30 13:36:13 +02:00
parent 8ad3583c4e
commit 23584a0a50
4 changed files with 38 additions and 9 deletions

View File

@ -6,11 +6,13 @@ import { Button } from "@/components/ui/button"
import GenreBadges from "app/ui/genreBadges" import GenreBadges from "app/ui/genreBadges"
import { actions } from "app/ui/tables/actions" import { actions } from "app/ui/tables/actions"
import { TextInputCell } from "app/ui/inputs/textInput" import { TextInputCell } from "app/ui/inputs/textInput"
import { selectCol } from "app/ui/tables/selectColumn"
const columnHelper = createColumnHelper<StoryWithGenres>() const columnHelper = createColumnHelper<StoryWithGenres>()
export const columns: ColumnDef<StoryWithGenres>[] = [ export const columns: ColumnDef<StoryWithGenres>[] = [
selectCol,
{ {
accessorKey: "title", accessorKey: "title",
header: ({ column }) => { header: ({ column }) => {

View File

@ -1072,10 +1072,6 @@ body {
align-items: flex-start; align-items: flex-start;
} }
.items-end {
align-items: flex-end;
}
.items-center { .items-center {
align-items: center; align-items: center;
} }
@ -1100,6 +1096,10 @@ body {
justify-content: space-around; justify-content: space-around;
} }
.justify-items-center {
justify-items: center;
}
.gap-1 { .gap-1 {
gap: 0.25rem; gap: 0.25rem;
} }

View File

@ -46,7 +46,7 @@ import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, Di
import pluralize from "app/lib/pluralize" import pluralize from "app/lib/pluralize"
import { updateTextField } from "app/lib/update" import { updateTextField } from "app/lib/update"
interface DataTableProps<TData, TValue> { export interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[] columns: ColumnDef<TData, TValue>[]
data: TData[] data: TData[]
} }
@ -85,13 +85,12 @@ export function DataTable<TData, TValue>({
//this is where you put arbitrary functions etc to make them accessible via the table api //this is where you put arbitrary functions etc to make them accessible via the table api
meta: { meta: {
updateTextField, updateTextField,
//TODO - move select row action here so it can be accessed from within a cell
selectRow: () => { }
} }
}) })
const pathname: Pathname = usePathname() const pathname: Pathname = usePathname()
const [filterBy, setFilterBy] = useState(table.getAllColumns()[0]) const [filterBy, setFilterBy] = useState(table.getAllColumns()[0])
const [isContextMenuOpen, setIsContextMenuOpen] = useState(false) const [isContextMenuOpen, setIsContextMenuOpen] = useState(false)
@ -221,8 +220,6 @@ export function DataTable<TData, TValue>({
key={row.id} key={row.id}
data-state={row.getIsSelected() && "selected"} data-state={row.getIsSelected() && "selected"}
tabIndex={0} tabIndex={0}
//check if context menu is open, so as not to select the row on clicking a menu item
onClick={() => { if (!isContextMenuOpen) { row.toggleSelected() } }}
> >
{row.getVisibleCells().map((cell) => ( {row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}> <TableCell key={cell.id}>

View File

@ -0,0 +1,30 @@
import { Checkbox } from "@/components/ui/checkbox";
import { CellContext, Column, ColumnDef, ColumnMeta, Header, HeaderContext, RowSelectionTableState, Table, TableState } from "@tanstack/react-table";
export const selectCol = {
id: "select",
header: (props: HeaderContext<any, any>) => {
return (
<div className="flex items-center justify-center">
<Checkbox
checked={props.table.getIsAllRowsSelected()}
onCheckedChange={props.table.toggleAllRowsSelected}
aria-label="select/deselect all rows"
/>
</div>
)
},
cell: (props: CellContext<any, any>) => {
return (
<div className="flex items-center justify-center">
<Checkbox
checked={props.row.getIsSelected()}
onCheckedChange={props.row.toggleSelected}
aria-label="select/deselect row" />
</div>
)
}
}