3.2.17 • Published 3 months ago

nextjs-reusable-table v3.2.17

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

Next.js Reusable Table

A highly customizable and reusable table component for Next.js applications, built with TypeScript and TailwindCSS.

Installation

npm install nextjs-reusable-table@latest
# or
yarn add nextjs-reusable-table@latest
# or
pnpm add nextjs-reusable-table@latest

Prerequisites

  • Next.js 12 or later
  • React 16 or later
  • React DOM 16 or later
  • Tailwind CSS 3.0 or later
  • TypeScript (recommended)

Note: This is a Client Component ("use client"). Using it in purely SSR contexts may require additional handling to avoid hydration mismatches.

Basic Usage

"use client";
import React from "react";
import { TableComponent } from "nextjs-reusable-table";
import "nextjs-reusable-table/dist/index.css";

interface User {
  id: number;
  name: string;
}

export default function BasicTable() {
  const data: User[] = [
    { id: 1, name: "Alice" },
    { id: 2, name: "Bob" },
  ];

  return (
    <TableComponent<User>
      columns={["ID", "Name"]}
      data={data}
      props={["id", "name"]}
    />
  );
}

Introduction

Next.js Reusable Table is designed for easy integration into Next.js apps. It supports sorting, pagination, row actions, and flexible data rendering via custom formatters. Built-in helpers handle arrays, dates, and URLs gracefully.

Features

Column Management

  • Hide/Show columns: Each column header has a dropdown (⋮) to remove or unhide columns.
  • Sorting: Specify which columns can be sorted.
<TableComponent
  columns={["Name", "Email"]}
  data={yourData}
  props={["name", "email"]}
  sortableProps={["name"]}
/>

Smart Row Interactions

The table provides intelligent click handling:

  • Click anywhere on a row to trigger row action
  • Click on cell content to expand/interact without triggering row action
  • Expandable content with "show more" functionality

Built-In Data Handling

  • Dates automatically formatted.
  • Arrays displayed as chips with “+X more” for large arrays.
  • URLs automatically detected and rendered as links.

Action Dropdowns

  • Easily attach row actions via a dropdown button:
<TableComponent
  actions
  actionTexts={["Edit", "Delete"]}
  actionFunctions={[(item) => editItem(item), (item) => deleteItem(item)]}
/>

Search and Pagination

  • searchValue filters rows against all columns.
  • Built-in pagination. Provide page, setPage, and itemsPerPage.
<TableComponent
  searchValue={searchTerm}
  enablePagination
  page={page}
  setPage={setPage}
  itemsPerPage={10}
/>

Custom Styling

  • Styling with Tailwind. Override default classes or disable them entirely:
const customClassNames = {
  table: "my-custom-table-styles",
  thead: "bg-gray-200 text-gray-700",
  tbody: "divide-y divide-gray-200",
  pagination: {
    container: "flex justify-center mt-4",
    button: "px-2 py-1 border",
    pageInfo: "mx-2",
  },
};

<TableComponent
  customClassNames={customClassNames}
  disableDefaultStyles={false}
/>;

Dark Mode

  • Automatically respects system preference if enableDarkMode is true.

Loading Skeleton

  • Show a skeleton loader while data is loading.
<TableComponent loading />

Empty State

Pass noContentProps to customize text and icon:

<TableComponent
  noContentProps={{
    text: "No data found",
    icon: <MyCustomIcon />,
  }}
/>

Advanced Example

"use client";
import React, { useState } from "react";
import "nextjs-reusable-table/dist/index.css";
import { TableComponent } from "nextjs-reusable-table";

interface Project {
  id: number;
  title: string;
  tags: string[];
  deadline: string;
  active: boolean;
  link: string;
}

const ProjectTable = () => {
  const [page, setPage] = useState(1);
  const data: Project[] = [
    {
      id: 1,
      title: "Website Redesign",
      tags: ["UI/UX", "Frontend"],
      deadline: "2025-03-15T10:30:00Z",
      active: true,
      link: "https://example.com/project/1",
    },
  ];

  const formatValue = (value: string, prop: string, item: Project) => {
    if (prop === "active") {
      return item.active ? "Active" : "Archived";
    }
    return value;
  };

  return (
    <TableComponent<Project>
      columns={["ID", "Title", "Tags", "Deadline", "Status", "Link"]}
      data={data}
      props={["id", "title", "tags", "deadline", "active", "link"]}
      formatValue={formatValue}
      enablePagination
      page={page}
      setPage={setPage}
      itemsPerPage={5}
      sortableProps={["title", "deadline"]}
    />
  );
};

export default ProjectTable;

Prop Reference

PropTypeDefaultDescription
columnsstring[]Column headers
dataT[]Array of data objects
propsReadonlyArrayObject keys to display
actionsbooleanfalseEnable action dropdown
actionTextsstring[]Labels for dropdown actions
loadingbooleanfalseShow loading skeleton
actionFunctionsArrayHandlers for dropdown items
searchValuestringFilter rows by substring
rowOnClick(item: T) => voidCallback for row clicks
enablePaginationbooleanfalseEnable pagination
pagenumber1Current page index
setPage(page: number) => voidPage setter callback
itemsPerPagenumber10Rows per page
totalPagesnumberOverride total pages
sortablePropsArray[]Columns that can be sorted
formatValue(val, prop, item) => React.ReactNodeCustom cell formatter
enableDarkModebooleantrueRespect system dark mode
disableDefaultStylesbooleanfalseDisable built-in styling
customClassNamesobject{}Tailwind class overrides
noContentPropsobject{}Custom empty state

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details on how to get started.

Versioning

We use Semantic Versioning for versioning. For the versions available, see the tags on this repository.

To bump the version, update the version field in package.json and follow the guidelines in the CONTRIBUTING.md file.

License

This project is licensed under the ISC License - see the LICENSE file for details.

Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.

Acknowledgments

  • Inspired by common data table patterns in React and Next.js applications.
  • Thanks to all contributors and users for their support.
1.8.0

6 months ago

1.6.1

7 months ago

1.6.0

7 months ago

3.1.10

5 months ago

1.7.10

7 months ago

1.7.11

7 months ago

1.7.12

7 months ago

1.7.13

7 months ago

1.7.14

7 months ago

1.7.15

7 months ago

3.1.9

5 months ago

3.1.8

5 months ago

2.0.0

6 months ago

3.2.2

4 months ago

3.2.1

4 months ago

3.2.0

4 months ago

3.0.2

5 months ago

3.0.1

5 months ago

3.2.6

3 months ago

3.2.5

4 months ago

3.2.4

4 months ago

3.2.3

4 months ago

3.0.0

5 months ago

1.7.9

7 months ago

1.7.8

7 months ago

1.7.7

7 months ago

1.5.9

7 months ago

1.7.6

7 months ago

1.5.8

7 months ago

1.7.5

7 months ago

1.5.7

7 months ago

1.7.4

7 months ago

1.5.6

7 months ago

1.7.3

7 months ago

1.5.5

7 months ago

1.7.2

7 months ago

1.7.1

7 months ago

1.5.3

7 months ago

1.7.0

7 months ago

1.5.2

7 months ago

1.5.1

7 months ago

1.5.0

7 months ago

3.2.9

3 months ago

3.2.8

3 months ago

3.2.7

3 months ago

3.2.13

3 months ago

3.1.3

5 months ago

3.2.12

3 months ago

3.1.2

5 months ago

3.2.15

3 months ago

3.1.1

5 months ago

3.2.14

3 months ago

3.1.0

5 months ago

3.2.17

3 months ago

3.1.7

5 months ago

3.2.16

3 months ago

3.1.6

5 months ago

3.1.5

5 months ago

3.1.4

5 months ago

3.2.11

3 months ago

3.2.10

3 months ago

1.3.7

9 months ago

1.3.6

9 months ago

1.3.5

9 months ago

1.3.4

9 months ago

1.3.3

9 months ago

1.3.2

9 months ago

1.3.1

9 months ago

1.3.0

9 months ago

1.2.1

9 months ago

1.2.0

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago