Last active
November 18, 2024 18:53
-
-
Save mxkaske/f0ebddefd02397dd37e4fa75b112b16c to your computer and use it in GitHub Desktop.
@tanstack/table filterFns example for date range (between days and same day)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use client"; | |
import type { ColumnDef } from "@tanstack/react-table"; | |
import type { ColumnSchema } from "./schema"; | |
import { format } from "date-fns"; | |
export const columns: ColumnDef<ColumnSchema>[] = [ | |
{ | |
accessorKey: "date", | |
header: "Date", | |
cell: ({ row }) => { | |
return ( | |
<div className="font-mono whitespace-nowrap"> | |
{format(new Date(row.getValue("date")), "LLL dd, y HH:mm")} | |
</div> | |
); | |
}, | |
filterFn: "inDateRange", | |
}, | |
// ... | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use client"; | |
import type { ColumnDef } from "@tanstack/react-table"; | |
import { useReactTable } from "@tanstack/react-table"; | |
import { inDateRange } from "@/lib/filterfns"; | |
export interface DataTableProps<TData, TValue> { | |
columns: ColumnDef<TData, TValue>[]; | |
data: TData[]; | |
} | |
export function DataTable<TData, TValue>({ | |
columns, | |
data, | |
}: DataTableProps<TData, TValue>) { | |
const table = useReactTable({ | |
data, | |
columns, | |
filterFns: { inDateRange }, | |
}); | |
return // ... see shadcn/ui | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FilterFn } from "@tanstack/react-table"; | |
import { isAfter, isBefore, isSameDay } from "date-fns"; | |
export const inDateRange: FilterFn<any> = (row, columnId, value) => { | |
const date = new Date(row.getValue(columnId)); | |
const [start, end] = value as Date[]; | |
if (isNaN(date.getTime())) return false; | |
// expection: if no end date, check if it's the same day | |
if (!end) return isSameDay(date, start); | |
return isAfter(date, start) && isBefore(date, end); | |
}; | |
inDateRange.autoRemove = (val: any) => | |
!Array.isArray(val) || | |
!val.length || | |
!val.every((item) => item instanceof Date); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "@tanstack/react-table"; | |
declare module "@tanstack/react-table" { | |
interface FilterFns { | |
inDateRange: FilterFn<any>; | |
} | |
// https://github.com/TanStack/table/discussions/4554 | |
interface ColumnFiltersOptions<TData extends RowData> { | |
filterFns?: Record<string, FilterFn<TData>>; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current implementation for logs.run/i.
Read more about how to leverage filterFns.