data-table-material
Version:
A dynamic and customizable table component for React, built with Material UI & TypeScript.
205 lines (152 loc) โข 5.59 kB
Markdown
# ๐ 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).