-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ColumnDef, RowData } from "@tanstack/react-table"; | ||
import { TABLE_CELL_PROPS } from "../../../styles/common/mui/tableCell"; | ||
import { ACCESSOR_KEYS } from "../../TableCreator/common/constants"; | ||
import { RowPositionCell } from "../components/TableCell/components/RowPositionCell/rowPositionCell"; | ||
|
||
export const COLUMN_DEF: Record<string, ColumnDef<RowData>> = { | ||
ROW_POSITION: { | ||
cell: RowPositionCell, | ||
enableHiding: false, | ||
enableSorting: false, | ||
header: "", | ||
id: ACCESSOR_KEYS.ROW_POSITION, | ||
meta: { | ||
align: TABLE_CELL_PROPS.ALIGN.RIGHT, | ||
header: "", | ||
width: "max-content", | ||
}, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
import { TableCellProps as MTableCellProps } from "@mui/material"; | ||
import { CoreCell, CoreHeader, RowData } from "@tanstack/react-table"; | ||
import { TableCellProps } from "@mui/material"; | ||
import { Column, CoreCell, CoreHeader, RowData } from "@tanstack/react-table"; | ||
import { TABLE_CELL_PROPS } from "../../../../../styles/common/mui/tableCell"; | ||
import { ACCESSOR_KEYS } from "../../../../TableCreator/common/constants"; | ||
|
||
/** | ||
* Returns table cell alignment based on the cell. | ||
* @param column - Column. | ||
* @returns table cell alignment. | ||
*/ | ||
export function getTableCellAlign<T extends RowData, TValue>( | ||
column: Column<T, TValue> | ||
): TableCellProps["align"] { | ||
return column.columnDef.meta?.align; | ||
} | ||
|
||
/** | ||
* Returns table cell padding based on the cell ID. | ||
* @param id - Cell ID. | ||
* @returns table cell padding. | ||
*/ | ||
export function getTableCellPadding<T extends RowData, TValue>( | ||
id: CoreHeader<T, TValue>["id"] | CoreCell<T, TValue>["id"] | ||
): MTableCellProps["padding"] { | ||
if (id === ACCESSOR_KEYS.SELECT) { | ||
return "checkbox"; | ||
): TableCellProps["padding"] { | ||
switch (id) { | ||
case ACCESSOR_KEYS.ROW_POSITION: | ||
return TABLE_CELL_PROPS.PADDING.NORMAL; | ||
case ACCESSOR_KEYS.SELECT: | ||
return TABLE_CELL_PROPS.PADDING.CHECKBOX; | ||
default: | ||
return TABLE_CELL_PROPS.PADDING.NORMAL; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { TypographyOwnProps } from "@mui/material"; | ||
import { VARIANT } from "../../../../../../styles/common/mui/typography"; | ||
|
||
export const TYPOGRAPHY_PROPS: Partial<TypographyOwnProps> = { | ||
sx: { marginRight: -2, paddingLeft: 2 }, | ||
variant: VARIANT.INHERIT, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Typography } from "@mui/material"; | ||
import { CellContext, RowData } from "@tanstack/react-table"; | ||
import React from "react"; | ||
import { BaseComponentProps } from "../../../../../types"; | ||
import { TYPOGRAPHY_PROPS } from "./constants"; | ||
|
||
export const RowPositionCell = <TData extends RowData, TValue>({ | ||
className, | ||
...cellContext | ||
}: 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
</Typography> | ||
); | ||
}; |
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>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We extend |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We update the |
||
}; | ||
}, | ||
getInitialState: (initialState?: InitialTableState): Partial<TableState> => { | ||
return initInitialState(initialState); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default initial state will be to hide the row position column. |
||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface RowPositionRow { | ||
getRowPosition: () => number; | ||
} |
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 => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding the function |
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Custom feature initial state expects pagination to be defined |
||
...initialState?.pagination, | ||
}, | ||
}; | ||
} |
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, | ||
}; |
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
.