-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: option to add row count on tables (#284)
- Loading branch information
Fran McDade
authored and
Fran McDade
committed
Nov 25, 2024
1 parent
b5aae14
commit 5c5e3d8
Showing
13 changed files
with
157 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
35 changes: 0 additions & 35 deletions
35
src/components/Table/components/TableCell/components/RowPositionCell/utils.ts
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
Cell, | ||
Column, | ||
Row, | ||
RowData, | ||
RowModel, | ||
Table, | ||
TableFeature, | ||
TableState, | ||
} from "@tanstack/react-table"; | ||
import { InitialTableState } from "@tanstack/table-core/src/types"; | ||
import { getRowModel, getRowPosition, initInitialState } from "./utils"; | ||
|
||
export const ROW_POSITION: TableFeature = { | ||
createCell: <T extends RowData, TValue>( | ||
cell: Cell<T, TValue>, | ||
column: Column<T>, | ||
row: Row<T>, | ||
table: Table<T> | ||
): void => { | ||
row.getRowPosition = (): number => { | ||
return getRowPosition(row.id, table); | ||
}; | ||
}, | ||
createTable: <T extends RowData>(table: Table<T>): void => { | ||
const originalGetRowModel = table.getRowModel.bind(table); | ||
table.getRowModel = (): RowModel<T> => { | ||
return getRowModel(table, originalGetRowModel); | ||
}; | ||
}, | ||
getInitialState: (initialState?: InitialTableState): Partial<TableState> => { | ||
return initInitialState(initialState); | ||
}, | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface RowPositionRow { | ||
getRowPosition: () => number; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
InitialTableState, | ||
RowData, | ||
RowModel, | ||
Table, | ||
TableState, | ||
} from "@tanstack/react-table"; | ||
import { ACCESSOR_KEYS } from "../../../TableCreator/common/constants"; | ||
import { DEFAULT_PAGINATION } from "../constants"; | ||
|
||
/** | ||
* Returns row model, with getter for row position. | ||
* @param table - Table. | ||
* @param getRowModel - Table getRowModel function. | ||
* @returns row model. | ||
*/ | ||
export function getRowModel<T extends RowData>( | ||
table: Table<T>, | ||
getRowModel: Table<T>[`getRowModel`] | ||
): RowModel<T> { | ||
const rowModel = getRowModel(); | ||
rowModel.rows.forEach(({ id }, i) => { | ||
rowModel.rowsById[id].getRowPosition = (): number => | ||
calculateRowPosition(table, i); | ||
}); | ||
return rowModel; | ||
} | ||
|
||
/** | ||
* Returns the position of the row in the table. | ||
* @param rowId - Row ID. | ||
* @param table - Table. | ||
* @returns row position. | ||
*/ | ||
export function getRowPosition<T extends RowData>( | ||
rowId: string, | ||
table: Table<T> | ||
): number { | ||
const { getRowModel } = table; | ||
const { rowsById } = getRowModel(); | ||
return rowsById[rowId].getRowPosition(); | ||
} | ||
|
||
/** | ||
* Calculates the position of the row in the table. | ||
* @param table - Table. | ||
* @param index - Row index. | ||
* @returns row position. | ||
*/ | ||
function calculateRowPosition<T extends RowData>( | ||
table: Table<T>, | ||
index: number | ||
): number { | ||
const { getState } = table; | ||
const { | ||
pagination: { pageIndex, pageSize }, | ||
} = getState(); | ||
return pageIndex * pageSize + (index + 1); | ||
} | ||
|
||
/** | ||
* Returns the initial table state. | ||
* @param initialState - Initial state. | ||
* @returns initial state. | ||
*/ | ||
export function initInitialState( | ||
initialState?: InitialTableState | ||
): Partial<TableState> { | ||
return { | ||
...initialState, | ||
columnVisibility: { | ||
[ACCESSOR_KEYS.ROW_POSITION]: false, | ||
...initialState?.columnVisibility, | ||
}, | ||
pagination: { | ||
...DEFAULT_PAGINATION, | ||
...initialState?.pagination, | ||
}, | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { PaginationState } from "@tanstack/react-table"; | ||
|
||
/** | ||
* Default TanStack pagination state. | ||
* See https://tanstack.com/table/latest/docs/guide/custom-features#getinitialstate. | ||
*/ | ||
export const DEFAULT_PAGINATION: PaginationState = { | ||
pageIndex: 0, | ||
pageSize: 10, | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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