1.0.12 • Published 10 months ago

duma-table v1.0.12

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

🏗️ BaseTable Documentation

This documentation describes how to use the BaseTable component, a flexible and highly customizable table component that supports features such as sorting, row selection, expandable rows, and context menus. It is built with React and TypeScript and includes support for dynamic data.

📖 Table of Contents

  1. Installation
  2. Props Overview
  3. Features
  4. Usage Examples
  5. Custom Styling

🚀 Installation

To install and use the BaseTable component, first install the necessary dependencies:

npm install duma-table

Import the components you need from duma-table:

import { BaseTable, TableColumn } from "duma-table";
import 'duma-table/dist/styles.css';

🧑‍💻 Props Overview

BaseTable Component

Prop NameTypeRequiredDescription
dataTData[]YesThe dataset that the table displays.
sortSortString<TData>YesThe initial sort configuration ('columnName asc' or 'columnName desc').
childrenReact.ReactElement<TableColumnProps<TData, unknown>>[]YesDefines the table columns, using TableColumn.
hasIndexColumnbooleanNoWhether to show an index column.
onRowClick(data: TData) => voidNoCallback function when a row is clicked.
onSortChange(sort: SortString<TData>) => voidNoCallback function when the sort order changes.
selectedRowsTData[]NoTracks selected rows.
onRowSelectionChange(selectedRows: TData[]) => voidNoCallback when the selected rows change.
expandableContent(rowData: TData) => React.ReactNodeNoRenders expandable row content.
rowActions(rowData: TData) => React.ReactNodeNoRenders actions for each row.

TableColumn Component

Prop NameTypeRequiredDescription
namestringYesUnique identifier for the column.
widthnumber or "*"YesSpecifies the column width.
labelReact.ReactNodeNoThe column header.
render(value?: TValue) => React.ReactNodeYesFunction to render cell content.
valueSelector(rowData: TDto) => TValue or undefinedYesFunction to extract the column value from a row.
sortablebooleanNoIf the column is sortable.
justifyTableColumnJustifyNoJustification for the column content.

🎛️ Features

Sorting

Define which columns are sortable by setting the sortable prop on the TableColumn. You can manage sorting by passing the sort and onSortChange props to BaseTable.

Row Selection

Enable row selection by providing the selectedRows and onRowSelectionChange props to BaseTable. This will display checkboxes for selecting rows.

Expandable Rows

To make rows expandable, pass a function to the expandableContent prop. This function should return the JSX for the expanded row content.

Context Menu

Right-clicking on a row can trigger a context menu. Define the menu content using the contextComponent prop.

Row Actions

You can define actions (e.g., edit, delete) for each row using the rowActions prop. This will display buttons or other interactive elements.


💡 Usage Examples

Table with Sorting and Row Selection

<BaseTable<TData>
  data={data}
  sort="name asc"
  onSortChange={(newSort) => setSort(newSort)}
  selectedRows={selectedRows}
  onRowSelectionChange={setSelectedRows}
>
  <TableColumn<TData, string>
    name="name"
    label="Name"
    sortable
    valueSelector={(rowData) => rowData.name}
    render={(value) => <span>{value}</span>}
  />
  <TableColumn<TData, number>
    name="age"
    label="Age"
    valueSelector={(rowData) => rowData.age}
    render={(value) => <span>{value}</span>}
  />
</BaseTable>

Table with Expandable Rows

<BaseTable<TData>
  data={data}
  expandableContent={(rowData) => (
    <div>
      <p>More details for {rowData.name}:</p>
      <ul>
        <li>Age: {rowData.age}</li>
        <li>Email: {rowData.email}</li>
      </ul>
    </div>
  )}
>
  <TableColumn<TData, string>
    name="name"
    label="Name"
    valueSelector={(rowData) => rowData.name}
    render={(value) => <span>{value}</span>}
  />
</BaseTable>

Table with Row Actions

<BaseTable<TData>
  data={data}
  rowActions={(rowData) => (
    <div>
      <button onClick={() => alert(`Edit ${rowData.name}`)}>Edit</button>
      <button onClick={() => alert(`Delete ${rowData.name}`)}>Delete</button>
    </div>
  )}
>
  <TableColumn<TData, string>
    name="name"
    label="Name"
    valueSelector={(rowData) => rowData.name}
    render={(value) => <span>{value}</span>}
  />
</BaseTable>

Table with Context Menu (No Positioning Props)

<BaseTable<TData>
  data={data}
  contextComponent={(rowData) => (
    <div>
      <button onClick={() => alert(`View details for ${rowData.name}`)}>View Details</button>
      <button onClick={() => alert(`Delete ${rowData.name}`)}>Delete</button>
    </div>
  )}
>
  <TableColumn<TData, string>
    name="name"
    label="Name"
    valueSelector={(rowData) => rowData.name}
    render={(value) => <span>{value}</span>}
  />
</BaseTable>

🎨 Custom Styling

You can easily apply custom styles to the table using the tableClassName prop on BaseTable. Additionally, each component supports className props for individual customization. This allows you to create entirely new styles as needed.

For example:

.custom-table {
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.custom-table th {
  color: #333;
}

.custom-table td {
  padding: 12px;
}
<BaseTable<TData>
  data={data}
  tableClassName="custom-table"
>
  <TableColumn<TData, string>
    name="name"
    label="Name"
    valueSelector={(rowData) => rowData.name}
    render={(value) => <span>{value}</span>}
  />
</BaseTable>

Enjoy styling your tables to suit your design! 🎉

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago