-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
96dcad9
to
b5aae14
Compare
b5aae14
to
e095b94
Compare
e095b94
to
5c5e3d8
Compare
}: BaseComponentProps & CellContext<TData, TValue>): JSX.Element => { | ||
return ( | ||
<Typography {...TYPOGRAPHY_PROPS} className={className} component="div"> | ||
{cellContext.row.getRowPosition()} |
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
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>( |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 => |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
ff23a9a
to
33f370e
Compare
There was a problem hiding this 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 !
Closes #284.