-
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
Showing
22 changed files
with
283 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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, | ||
header: "", | ||
id: ACCESSOR_KEYS.ROW_POSITION, | ||
meta: { align: TABLE_CELL_PROPS.ALIGN.RIGHT, header: "" }, | ||
}, | ||
}; |
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, | ||
}; |
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
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"; | ||
import { getRowPosition } from "./utils"; | ||
|
||
export const RowPositionCell = <TData extends RowData, TValue>({ | ||
className, | ||
...cellContext | ||
}: BaseComponentProps & CellContext<TData, TValue>): JSX.Element => { | ||
return ( | ||
<Typography {...TYPOGRAPHY_PROPS} className={className} component="div"> | ||
{getRowPosition(cellContext)} | ||
</Typography> | ||
); | ||
}; |
35 changes: 35 additions & 0 deletions
35
src/components/Table/components/TableCell/components/RowPositionCell/utils.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,35 @@ | ||
import { CellContext, RowData } from "@tanstack/react-table"; | ||
|
||
/** | ||
* Returns the index of the row in the table, for the current page. | ||
* @param cellContext - Cell context. | ||
* @returns row index. | ||
*/ | ||
export function getRowIndex<TData extends RowData, TValue>( | ||
cellContext: CellContext<TData, TValue> | ||
): number { | ||
const { | ||
row, | ||
table: { getRowModel }, | ||
} = cellContext; | ||
const { rows } = getRowModel(); | ||
return rows.findIndex(({ id }) => id === row.id); | ||
} | ||
|
||
/** | ||
* Returns the position of the row in the table. | ||
* @param cellContext - Cell context. | ||
* @returns row position. | ||
*/ | ||
export const getRowPosition = <TData extends RowData, TValue>( | ||
cellContext: CellContext<TData, TValue> | ||
): number => { | ||
const { | ||
table: { getState }, | ||
} = cellContext; | ||
const { | ||
pagination: { pageIndex, pageSize }, | ||
} = getState(); | ||
const rowIndex = getRowIndex(cellContext); | ||
return pageIndex * pageSize + (rowIndex + 1); | ||
}; |
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
Oops, something went wrong.