npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

duma-table

v1.0.12

Published

A flexible React table component with sorting, row selection, expandable rows, and context menu.

Downloads

853

Readme

🏗️ 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 Name | Type | Required | Description | |-------------------------|-----------------------------------------------------------------|----------|-------------| | data | TData[] | Yes | The dataset that the table displays. | | sort | SortString<TData> | Yes | The initial sort configuration ('columnName asc' or 'columnName desc'). | | children | React.ReactElement<TableColumnProps<TData, unknown>>[] | Yes | Defines the table columns, using TableColumn. | | hasIndexColumn | boolean | No | Whether to show an index column. | | onRowClick | (data: TData) => void | No | Callback function when a row is clicked. | | onSortChange | (sort: SortString<TData>) => void | No | Callback function when the sort order changes. | | selectedRows | TData[] | No | Tracks selected rows. | | onRowSelectionChange | (selectedRows: TData[]) => void | No | Callback when the selected rows change. | | expandableContent | (rowData: TData) => React.ReactNode | No | Renders expandable row content. | | rowActions | (rowData: TData) => React.ReactNode | No | Renders actions for each row. |

TableColumn Component

| Prop Name | Type | Required | Description | |---------------|---------------------------------------------------------|----------|-------------| | name | string | Yes | Unique identifier for the column. | | width | number or "*" | Yes | Specifies the column width. | | label | React.ReactNode | No | The column header. | | render | (value?: TValue) => React.ReactNode | Yes | Function to render cell content. | | valueSelector| (rowData: TDto) => TValue or undefined | Yes | Function to extract the column value from a row. | | sortable | boolean | No | If the column is sortable. | | justify | TableColumnJustify | No | Justification 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! 🎉