Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: option to add row count on tables (#284) #285

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

frano-m
Copy link
Contributor

@frano-m frano-m commented Nov 22, 2024

Closes #284.

image

@frano-m frano-m requested a review from NoopDog November 22, 2024 02:20
@frano-m frano-m force-pushed the fran/415-table-row-count branch from 96dcad9 to b5aae14 Compare November 22, 2024 02:25
@frano-m frano-m changed the title feat: option to add row count on tables (#415) feat: option to add row count on tables (#284) Nov 22, 2024
@frano-m frano-m force-pushed the fran/415-table-row-count branch from b5aae14 to e095b94 Compare November 22, 2024 03:03
@NoopDog NoopDog changed the title feat: option to add row count on tables (#284) feat!: option to add row count on tables (#284) Nov 22, 2024
@frano-m frano-m force-pushed the fran/415-table-row-count branch from e095b94 to 5c5e3d8 Compare November 25, 2024 04:17
}: BaseComponentProps & CellContext<TData, TValue>): JSX.Element => {
return (
<Typography {...TYPOGRAPHY_PROPS} className={className} component="div">
{cellContext.row.getRowPosition()}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RowPosition is now a custom feature, and we now have getRowPostiion on the cellContext.row.

@@ -56,8 +58,11 @@ export const Table = <T extends RowData>({
const { stickyHeader = false } = table || {};
const { sx: tableContainerSx } = tableContainer || {};
const tableInstance = useReactTable({
_features: [ROW_PREVIEW],
columns: generateColumnDefinitions(columns),
_features: [ROW_POSITION, ROW_PREVIEW],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New custom feature ROW_POSITION.

import { getRowModel, getRowPosition, initInitialState } from "./utils";

export const ROW_POSITION: TableFeature = {
createCell: <T extends RowData, TValue>(
Copy link
Contributor Author

@frano-m frano-m Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We extend cellContext.row with the function getRowPosition. We will call this function from the RowPositionCell component. The value returned will be the row position.

createTable: <T extends RowData>(table: Table<T>): void => {
const originalGetRowModel = table.getRowModel.bind(table);
table.getRowModel = (): RowModel<T> => {
return getRowModel(table, originalGetRowModel);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We update the getRowModel; each row in the getRowModel.rowsById will be updated to include the getRowPosition function. Doing this in the feature, allows us to efficiently retrieve the getRowPosition function for the given row.

};
},
getInitialState: (initialState?: InitialTableState): Partial<TableState> => {
return initInitialState(initialState);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default initial state will be to hide the row position column.

): RowModel<T> {
const rowModel = getRowModel();
rowModel.rows.forEach(({ id }, i) => {
rowModel.rowsById[id].getRowPosition = (): number =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the function getRowPosition onto rowModel.rowsById[id] where rowsById is part of TanStack's rowModel API, which maps each row to its id.

...initialState?.columnVisibility,
},
pagination: {
...DEFAULT_PAGINATION,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Custom feature initial state expects pagination to be defined pageIndex and pageSize. These values are initial state only, and will always be updated by table state as per usual.

@frano-m frano-m force-pushed the fran/415-table-row-count branch from ff23a9a to 33f370e Compare November 25, 2024 04:34
Copy link
Collaborator

@NoopDog NoopDog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks @frano-m !

@NoopDog NoopDog merged commit 1643efb into main Nov 25, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to add row count on tables
2 participants