diff --git a/src/app/_components/tasks-table.tsx b/src/app/_components/tasks-table.tsx index 49c2459..d985238 100644 --- a/src/app/_components/tasks-table.tsx +++ b/src/app/_components/tasks-table.tsx @@ -82,11 +82,7 @@ export function TasksTable({ tasksPromise, viewsPromise }: TasksTableProps) { table={table} floatingBar={} > - + diff --git a/src/components/data-table/advanced/data-table-advanced-toolbar.tsx b/src/components/data-table/advanced/data-table-advanced-toolbar.tsx index a6777ba..4c975da 100644 --- a/src/components/data-table/advanced/data-table-advanced-toolbar.tsx +++ b/src/components/data-table/advanced/data-table-advanced-toolbar.tsx @@ -5,13 +5,13 @@ import { usePathname, useRouter, useSearchParams } from "next/navigation" import type { View } from "@/db/schema" import type { DataTableFilterField, DataTableFilterOption } from "@/types" import { CaretSortIcon, PlusIcon } from "@radix-ui/react-icons" -import type { Table } from "@tanstack/react-table" import isEqual from "lodash.isequal" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { DataTableFilterCombobox } from "@/components/data-table/advanced/data-table-filter-combobox" import { DataTableColumnsVisibility } from "@/components/data-table/data-table-columns-visibility" +import { useTableInstanceContext } from "@/app/_components/table-instance-provider" import type { SearchParams } from "@/app/_lib/validations" import { DataTableFilterItem } from "./data-table-filter-item" @@ -28,13 +28,11 @@ import { interface DataTableAdvancedToolbarProps extends React.HTMLAttributes { - table: Table filterFields?: DataTableFilterField[] views: Omit[] } export function DataTableAdvancedToolbar({ - table, filterFields = [], views, children, @@ -45,6 +43,8 @@ export function DataTableAdvancedToolbar({ const pathname = usePathname() const searchParams = useSearchParams() + const { tableInstance: table } = useTableInstanceContext() + const options = React.useMemo[]>(() => { return filterFields.map((field) => { return { @@ -218,7 +218,7 @@ export function DataTableAdvancedToolbar({ onSelect={onFilterComboboxItemSelect} /> )} - +
({ .map((selectedOption) => ( ({ ))} {selectedOptions.some((option) => option.isMulti) ? ( { - table: Table selectedOption: DataTableFilterOption setSelectedOptions: React.Dispatch< React.SetStateAction[]> @@ -35,7 +34,6 @@ interface DataTableFilterItemProps { } export function DataTableFilterItem({ - table, selectedOption, setSelectedOptions, defaultOpen, @@ -44,6 +42,8 @@ export function DataTableFilterItem({ const pathname = usePathname() const searchParams = useSearchParams() + const { tableInstance: table } = useTableInstanceContext() + const column = table.getColumn( selectedOption.value ? String(selectedOption.value) : "" ) diff --git a/src/components/data-table/advanced/data-table-multi-filter.tsx b/src/components/data-table/advanced/data-table-multi-filter.tsx index 497d261..33ae36a 100644 --- a/src/components/data-table/advanced/data-table-multi-filter.tsx +++ b/src/components/data-table/advanced/data-table-multi-filter.tsx @@ -7,7 +7,6 @@ import { TextAlignCenterIcon, TrashIcon, } from "@radix-ui/react-icons" -import type { Table } from "@tanstack/react-table" import { dataTableConfig, type DataTableConfig } from "@/config/data-table" import { createQueryString } from "@/lib/utils" @@ -34,11 +33,11 @@ import { SelectValue, } from "@/components/ui/select" import { Separator } from "@/components/ui/separator" +import { useTableInstanceContext } from "@/app/_components/table-instance-provider" import { DataTableFacetedFilter } from "../data-table-faceted-filter" interface DataTableMultiFilterProps { - table: Table allOptions: DataTableFilterOption[] options: DataTableFilterOption[] setSelectedOptions: React.Dispatch< @@ -48,7 +47,6 @@ interface DataTableMultiFilterProps { } export function DataTableMultiFilter({ - table, allOptions, options, setSelectedOptions, @@ -89,7 +87,6 @@ export function DataTableMultiFilter({ key={option.id ?? i} i={i} option={option} - table={table} allOptions={allOptions} setSelectedOptions={setSelectedOptions} operator={operator} @@ -129,7 +126,6 @@ export function DataTableMultiFilter({ interface MultiFilterRowProps { i: number - table: Table allOptions: DataTableFilterOption[] option: DataTableFilterOption setSelectedOptions: React.Dispatch< @@ -145,7 +141,6 @@ interface MultiFilterRowProps { export function MultiFilterRow({ i, - table, option, allOptions, setSelectedOptions, @@ -159,6 +154,8 @@ export function MultiFilterRow({ const value = option.filterValues?.[0] ?? "" const debounceValue = useDebounce(value, 500) + const { tableInstance: table } = useTableInstanceContext() + const column = table.getColumn(option.value ? String(option.value) : "") const comparisonOperators = diff --git a/src/components/data-table/data-table-columns-visibility.tsx b/src/components/data-table/data-table-columns-visibility.tsx index d1e2d2f..8875af1 100644 --- a/src/components/data-table/data-table-columns-visibility.tsx +++ b/src/components/data-table/data-table-columns-visibility.tsx @@ -1,7 +1,6 @@ "use client" import { LayoutIcon } from "@radix-ui/react-icons" -import type { Table } from "@tanstack/react-table" import { Button } from "@/components/ui/button" import { @@ -12,14 +11,11 @@ import { DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" +import { useTableInstanceContext } from "@/app/_components/table-instance-provider" -interface DataTableColumnsVisibility { - table: Table -} +export function DataTableColumnsVisibility() { + const { tableInstance: table } = useTableInstanceContext() -export function DataTableColumnsVisibility({ - table, -}: DataTableColumnsVisibility) { return ( diff --git a/src/components/data-table/data-table-pagination.tsx b/src/components/data-table/data-table-pagination.tsx index 03c5336..fca02ad 100644 --- a/src/components/data-table/data-table-pagination.tsx +++ b/src/components/data-table/data-table-pagination.tsx @@ -6,7 +6,6 @@ import { DoubleArrowLeftIcon, DoubleArrowRightIcon, } from "@radix-ui/react-icons" -import { type Table } from "@tanstack/react-table" import { Button } from "@/components/ui/button" import { @@ -16,28 +15,29 @@ import { SelectTrigger, SelectValue, } from "@/components/ui/select" +import { useTableInstanceContext } from "@/app/_components/table-instance-provider" -interface DataTablePaginationProps { - table: Table +interface DataTablePaginationProps { pageSizeOptions?: number[] } -export function DataTablePagination({ - table, +export function DataTablePagination({ pageSizeOptions = [10, 20, 30, 40, 50], -}: DataTablePaginationProps) { +}: DataTablePaginationProps) { const searchParams = useSearchParams() + const { tableInstance: table } = useTableInstanceContext() + + const page = searchParams.get("page") + const perPage = searchParams.get("per_page") + // Update pagination state when pagination params are changed useEffect(() => { - const page = searchParams.get("page") - const perPage = searchParams.get("per_page") - table.setPageIndex(page ? Number(page) - 1 : 0) table.setPageSize(perPage ? Number(perPage) : 10) // eslint-disable-next-line react-hooks/exhaustive-deps - }, [searchParams]) + }, [page, perPage]) return (
@@ -51,6 +51,7 @@ export function DataTablePagination({