UNPKG

data-table-material

Version:

A dynamic and customizable table component for React, built with Material UI & TypeScript.

205 lines (152 loc) โ€ข 5.59 kB
# ๐Ÿ“Š Data Table Material A highly customizable and reusable Material UI-based table component built with TypeScript and React. Supports features like dynamic columns, pagination, row actions, expandable rows, and more. ## ๐Ÿš€ Features - โœ… Customizable columns and rows - ๐Ÿ”ข Optional serial number column - ๐Ÿ“„ Expandable row support - โž• Additional action columns (switch, delete) - ๐Ÿ“œ Pagination with external control - ๐ŸŽฏ Row click handling - ๐ŸŽจ Theme-aware styling using MUI `sx` props - โšก Visible/hidden column control - ๐Ÿ“ Resizable headers --- ## ๐Ÿ“ฆ Installation ```bash npm install data-table-material # or yarn add data-table-material ``` --- ## โœจ Usage ```tsx import DataTable from "data-table-material"; const columns = [ { _key: "name", label: "Name" }, { _key: "email", label: "Email" }, ]; const rows = [ { name: "John Doe", email: "john@example.com" }, { name: "Jane Doe", email: "jane@example.com" }, ]; export default function App() { return <DataTable rows={rows} columns={columns} />; } ``` --- ## ๐Ÿงฉ Props ### `IDataTable.Props<T>` | Prop | Type | Description | Default | | -------------------------- | ------------------------------------- | ------------------------------------------------ | ------------ | | `rows` | `T[]` | Array of row data | **Required** | | `columns` | `Column<T>[]` | Array of column definitions | **Required** | | `serialNumber` | `boolean` | Show serial number column | `false` | | `pagination` | `boolean` | Enable pagination | `false` | | `paginationData` | `Pagination` | Current pagination state | - | | `onChangePaginationData` | `(data: Partial<Pagination>) => void` | Triggered when pagination changes | - | | `additionalColumns` | `AdditionalColumn<T>[]` | Additional action columns (e.g., delete, switch) | - | | `onRowClick` | `(row: T) => void` | Row click handler | - | | `containerStyle` | `SxProps` | Custom style for container | `{}` | | `paperStyle` | `SxProps` | Custom style for paper | `{}` | | `visibleColumns` | `string[]` | Filter which columns are visible | - | | `getExpandableTableConfig` | `(row: T) => ReactNode` | Expandable row renderer | - | | `size` | `"small" \| "medium"` | MUI Table size | `"small"` | | `...rest` | `TableProps` | All other native Table props | - | --- ## ๐Ÿ—๏ธ Column Definition ```ts interface Column<T> { _key: keyof T | string; label: string; renderCell?: (row: T) => ReactNode; width?: number | string; hidden?: boolean; sx?: SxProps; title?: string; } ``` --- ## โž• Additional Columns Use these for actions like toggles or delete buttons. ```ts interface AdditionalColumn<T> { _key: keyof T | string; label: string; onClick: (row: T) => void; type: "switch" | "deleteButton"; hidden?: boolean; width?: number; hasPermission?: (row: T) => boolean; } ``` --- ## ๐Ÿ”„ Pagination Structure ```ts interface Pagination { pageNo: number; pageSize: number; total: number; totalRecords: number; } ``` Use `paginationData` to pass current pagination info and `onChangePaginationData` to update it. --- ## ๐Ÿงฐ Utilities - โœ… `Resizable`: For resizable columns - ๐Ÿงฉ `RenderRow`: Custom row renderer - ๐Ÿ“Œ `AdditonalColumnHeaders`: Handles header rendering for additional columns - ๐Ÿ“š `CustomPagination`: External pagination component --- ## ๐Ÿ–ผ๏ธ Screenshots > _Coming soon!_ Feel free to contribute UI previews! --- ## ๐Ÿงช Example ```tsx <DataTable rows={data} columns={columns} serialNumber pagination paginationData={paginationState} onChangePaginationData={setPaginationState} additionalColumns={[ { _key: "delete", label: "Delete", type: "deleteButton", onClick: handleDelete, }, ]} onRowClick={(row) => console.log("Clicked row", row)} /> ``` --- ## ๐Ÿ“ Folder Structure ``` data-table-material/ โ”œโ”€โ”€ components/ โ”‚ โ”œโ”€โ”€ DataTable.tsx โ”‚ โ”œโ”€โ”€ RenderRow.tsx โ”‚ โ”œโ”€โ”€ Resizable.tsx โ”‚ โ”œโ”€โ”€ CustomPagination.tsx โ”‚ โ””โ”€โ”€ AdditonalColumnHeaders.tsx โ”œโ”€โ”€ types/ โ”‚ โ””โ”€โ”€ index.ts โ”œโ”€โ”€ style.css โ””โ”€โ”€ README.md ``` --- ## ๐Ÿงฑ Built With - [React](https://reactjs.org/) - [TypeScript](https://www.typescriptlang.org/) - [Material UI](https://mui.com/) --- ## ๐Ÿง‘โ€๐Ÿ’ป Contributing Contributions are welcome! Please open issues or submit PRs to help improve the component. --- ## ๐Ÿ“ License MIT License --- ## ๐Ÿ’ฌ Feedback Found a bug or want a feature? Feel free to [open an issue](https://github.com/meet503186/data-table-material/issues). ---