Skip to content

Commit

Permalink
fix some types for fns internally
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Oct 1, 2024
1 parent a1219e2 commit 99df25a
Show file tree
Hide file tree
Showing 32 changed files with 82 additions and 65 deletions.
1 change: 0 additions & 1 deletion examples/react/custom-features/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ function App() {
const table = useTable({
_features: { DensityFeature }, //pass our custom feature to the table to be instantiated upon creation
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
Sorted: createSortedRowModel(),
Expand Down
1 change: 0 additions & 1 deletion examples/react/expanding/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ function App() {
const table = useTable({
_features,
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
Sorted: createSortedRowModel(),
Expand Down
29 changes: 20 additions & 9 deletions examples/react/filters-faceted/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import {
createFilteredRowModel,
createPaginatedRowModel,
createSortedRowModel,
filterFns,
flexRender,
sortingFns,
tableFeatures,
tableFns,
useTable,
} from '@tanstack/react-table'
import { makeData } from './makeData'
import type { Fns } from '../../../../packages/table-core/dist/esm/types/Fns'
import type {
CellData,
Column,
Expand All @@ -38,8 +42,13 @@ const _features = tableFeatures({
RowSorting,
})

const _fns = tableFns(_features, {
filterFns,
sortingFns,
})

declare module '@tanstack/react-table' {
//allows us to define custom properties for our columns
// allows us to define custom properties for our columns
interface ColumnMeta<
TFeatures extends TableFeatures,
TFns extends Fns<TFeatures, TFns, TData>,
Expand All @@ -55,7 +64,9 @@ function App() {
[],
)

const columns = React.useMemo<Array<ColumnDef<typeof _features, {}, Person>>>(
const columns = React.useMemo<
Array<ColumnDef<typeof _features, typeof _fns, Person>>
>(
() => [
{
accessorKey: 'firstName',
Expand Down Expand Up @@ -100,19 +111,19 @@ function App() {
)

const [data, setData] = React.useState<Array<Person>>(() => makeData(5_000))
const refreshData = () => setData((_old) => makeData(100_000)) //stress test
const refreshData = () => setData((_old) => makeData(100_000)) // stress test
const rerender = React.useReducer(() => ({}), {})[1]

const table = useTable({
_features,
_fns,
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(), //client-side filtering
Filtered: createFilteredRowModel(), // client-side filtering
Paginated: createPaginatedRowModel(),
Sorted: createSortedRowModel(),
Faceted: createFacetedRowModel(), //client-side faceting
FacetedMinMax: createFacetedMinMaxValues(), //generate min/max values for range filter
FacetedUnique: createFacetedUniqueValues(), //generate unique values for select filter/autocomplete
Faceted: createFacetedRowModel(), // client-side faceting
FacetedMinMax: createFacetedMinMaxValues(), // generate min/max values for range filter
FacetedUnique: createFacetedUniqueValues(), // generate unique values for select filter/autocomplete
},
columns,
data,
Expand Down Expand Up @@ -319,7 +330,7 @@ function Filter({ column }: { column: Column<any, Person, unknown> }) {
>
<option value="">All</option>
{sortedUniqueValues.map((value) => (
//dynamically generated select options from faceted values feature
// dynamically generated select options from faceted values feature
<option value={value} key={value}>
{value}
</option>
Expand Down
1 change: 0 additions & 1 deletion examples/react/filters-fuzzy/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ function App() {
const table = useTable<typeof _features, Person>({
_features,
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
Sorted: createSortedRowModel(),
Expand Down
6 changes: 5 additions & 1 deletion examples/react/filters/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ function App() {
)
}

function Filter({ column }: { column: Column<any, any, Person, unknown> }) {
function Filter({
column,
}: {
column: Column<typeof _features, typeof _fns, Person, unknown>
}) {
const columnFilterValue = column.getFilterValue()
const { filterVariant } = column.columnDef.meta ?? {}

Expand Down
4 changes: 1 addition & 3 deletions examples/react/full-width-resizable-table/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ function App() {
const data = React.useMemo(() => makeData(20), [])

const table = useTable({
_rowModels: {
Core: createCoreRowModel(),
},
_rowModels: {},
columns,
data,
enableColumnResizing: true,
Expand Down
1 change: 0 additions & 1 deletion examples/react/full-width-table/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ function App() {

const table = useTable({
_rowModels: {
Core: createCoreRowModel(),
Paginated: createPaginatedRowModel(),
},
columns,
Expand Down
1 change: 0 additions & 1 deletion examples/react/fully-controlled/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ function App() {
// Create the table and pass your options
const table = useTable({
_rowModels: {
Core: createCoreRowModel(),
Paginated: createPaginatedRowModel(),
},
columns,
Expand Down
1 change: 0 additions & 1 deletion examples/react/grouping/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const tableHelper = createTableHelper({
RowExpanding,
},
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
Sorted: createSortedRowModel(),
Expand Down
1 change: 0 additions & 1 deletion examples/react/kitchen-sink/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export const App = () => {

const table = useTable({
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
Sorted: createSortedRowModel(),
Expand Down
1 change: 0 additions & 1 deletion examples/react/material-ui-pagination/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ function LocalTable({
}) {
const table = useTable({
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
},
Expand Down
4 changes: 1 addition & 3 deletions examples/react/pagination-controlled/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ function App() {

const table = useTable({
_features: {},
_rowModels: {
Core: createCoreRowModel(),
},
_rowModels: {},
columns,
data: dataQuery.data?.rows ?? defaultData,
rowCount: dataQuery.data?.rowCount, //alternatively, just pass in `pageCount` directly
Expand Down
1 change: 0 additions & 1 deletion examples/react/pagination/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ function MyTable({

const table = useTable({
_rowModels: {
Core: createCoreRowModel(),
Sorted: createSortedRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function Table<T extends Record<string, string | number>>({
sorting,
}: Props<T>) {
const table = useTable({
_rowModels: { Core: createCoreRowModel() },
_rowModels: {},
columns,
data,
manualFiltering: true,
Expand Down
4 changes: 1 addition & 3 deletions examples/react/row-dnd/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ function App() {

const table = useTable({
_features: {},
_rowModels: {
Core: createCoreRowModel(),
},
_rowModels: {},
columns,
data,
getRowId: (row) => row.userId, //required because row indexes will change
Expand Down
1 change: 0 additions & 1 deletion examples/react/row-pinning/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ function App() {
const table = useTable({
_features,
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Expanded: createExpandedRowModel(),
Paginated: createPaginatedRowModel(),
Expand Down
1 change: 0 additions & 1 deletion examples/react/row-selection/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function App() {
const table = useTable({
_features,
_rowModels: {
Core: createCoreRowModel(),
Filtered: createFilteredRowModel(),
Paginated: createPaginatedRowModel(),
},
Expand Down
1 change: 0 additions & 1 deletion examples/react/sorting/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ function App() {
const table = useTable({
_features: { RowSorting },
_rowModels: {
Core: createCoreRowModel(),
Sorted: createSortedRowModel(), //client-side sorting
},
columns,
Expand Down
1 change: 0 additions & 1 deletion examples/react/sub-components/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ function Table({
const table = useTable({
_features: { RowExpanding },
_rowModels: {
Core: createCoreRowModel(),
Expanded: createExpandedRowModel(),
},
columns,
Expand Down
2 changes: 1 addition & 1 deletion examples/react/virtualized-columns/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function App() {

const table = useTable({
_features: { ColumnSizing, RowSorting },
_rowModels: { Core: createCoreRowModel(), Sorted: createSortedRowModel() },
_rowModels: { Sorted: createSortedRowModel() },
columns,
data,
debugTable: true,
Expand Down
2 changes: 1 addition & 1 deletion examples/react/virtualized-infinite-scrolling/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function App() {

const table = useTable({
_features: { ColumnSizing, RowSorting },
_rowModels: { Core: createCoreRowModel(), Sorted: createSortedRowModel() },
_rowModels: { Sorted: createSortedRowModel() },
data: flatData,
columns,
state: {
Expand Down
2 changes: 1 addition & 1 deletion examples/react/virtualized-rows/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function App() {

const table = useTable({
_features: { ColumnSizing, RowSorting },
_rowModels: { Core: createCoreRowModel(), Sorted: createSortedRowModel() },
_rowModels: { Sorted: createSortedRowModel() },
columns,
data,
debugTable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface TableFns_ColumnFiltering<
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
> {
filterFns?: Record<keyof FilterFns, FilterFn<TFeatures, TFns, TData>>
filterFns: Record<keyof FilterFns, FilterFn<TFeatures, TFns, TData>>
}

export interface FilterFn<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export function column_getAutoFilterFn<
column: Column<TFeatures, TFns, TData, TValue>,
table: Table_Internal<TFeatures, TFns, TData>,
) {
const { filterFns } = table._fns
const filterFns = table._fns.filterFns as
| Record<string, FilterFn<TFeatures, TFns, TData>>
| undefined

const firstRow = table_getCoreRowModel(table).flatRows[0]

const value = firstRow ? row_getValue(firstRow, table, column.id) : undefined
Expand Down Expand Up @@ -64,11 +67,14 @@ export function column_getFilterFn<
},
table: Table_Internal<TFeatures, TFns, TData>,
): FilterFn<TFeatures, TFns, TData> | undefined {
const filterFns = table._fns.filterFns as
| Record<string, FilterFn<TFeatures, TFns, TData>>
| undefined
return isFunction(column.columnDef.filterFn)
? column.columnDef.filterFn
: column.columnDef.filterFn === 'auto'
? column_getAutoFilterFn(column, table)
: table._fns.filterFns?.[column.columnDef.filterFn as string]
: filterFns?.[column.columnDef.filterFn as string]
}

export function column_getCanFilter<
Expand Down Expand Up @@ -258,7 +264,10 @@ export function shouldAutoRemoveFilter<
) {
return (
(filterFn && filterFn.autoRemove
? filterFn.autoRemove(value, column)
? filterFn.autoRemove(
value,
column as Column<TFeatures, TFns, TData, unknown>,
)
: false) ||
typeof value === 'undefined' ||
(typeof value === 'string' && !value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface TableFns_ColumnGrouping<
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
> {
aggregationFns?: Record<
aggregationFns: Record<
keyof AggregationFns,
AggregationFn<TFeatures, TFns, TData>
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { Row } from '../../types/Row'
import type { Cell } from '../../types/Cell'
import type { Column } from '../../types/Column'
import type {
AggregationFn,
ColumnDef_ColumnGrouping,
GroupingState,
Row_ColumnGrouping,
Expand Down Expand Up @@ -114,7 +115,10 @@ export function column_getAutoAggregationFn<
},
table: Table_Internal<TFeatures, TFns, TData>,
) {
const { aggregationFns } = table._fns
const aggregationFns = table._fns.aggregationFns as
| Record<string, AggregationFn<TFeatures, TFns, TData>>
| undefined

const firstRow = table.getCoreRowModel().flatRows[0]

const value = firstRow?.getValue(column.id)
Expand All @@ -139,11 +143,15 @@ export function column_getAggregationFn<
},
table: Table_Internal<TFeatures, TFns, TData>,
) {
const aggregationFns = table._fns.aggregationFns as
| Record<string, AggregationFn<TFeatures, TFns, TData>>
| undefined

return isFunction(column.columnDef.aggregationFn)
? column.columnDef.aggregationFn
: column.columnDef.aggregationFn === 'auto'
? column_getAutoAggregationFn(column, table)
: table._fns.aggregationFns?.[column.columnDef.aggregationFn as string]
: aggregationFns?.[column.columnDef.aggregationFn as string]
}

export function table_setGrouping<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface TableFns_RowSorting<
TFns extends Fns<TFeatures, TFns, TData>,
TData extends RowData,
> {
sortingFns?: Record<keyof SortingFns, SortingFn<TFeatures, TFns, TData>>
sortingFns: Record<keyof SortingFns, SortingFn<TFeatures, TFns, TData>>
}

export interface SortingFns {}
Expand Down
Loading

0 comments on commit 99df25a

Please sign in to comment.