-
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.
Browse files
Browse the repository at this point in the history
* feat: option to add row count on tables (#284) * feat!: option to add row count on tables (#284) * feat: removed row position from column configs (#284) --------- Co-authored-by: Fran McDade <[email protected]>
- Loading branch information
Showing
28 changed files
with
381 additions
and
71 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
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,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", | ||
}, | ||
}, | ||
}; |
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
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/components/Table/components/TableCell/components/RowPositionCell/constants.ts
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,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, | ||
}; |
16 changes: 16 additions & 0 deletions
16
src/components/Table/components/TableCell/components/RowPositionCell/rowPositionCell.tsx
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,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()} | ||
</Typography> | ||
); | ||
}; |
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
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
Oops, something went wrong.