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

react-lite-table

v1.0.0

Published

react-lite-table is a flexible, lightweight, and feature-rich React component for displaying tabular data. It includes functionalities such as sorting, searching, pagination, and row selection, making it suitable for a variety of use cases.

Downloads

4

Readme

react-lite-table

react-lite-table is a flexible, lightweight, and feature-rich React component for displaying tabular data. It includes functionalities such as sorting, searching, pagination, and row selection, making it suitable for a variety of use cases.

Features

  • Pagination: Divide your data into pages to handle large datasets efficiently.
  • Sorting: Organize your table data by sorting columns in ascending or descending order.
  • Row Selection: Select individual rows or all rows on the current page.
  • Search: Includes a search box to filter table data, with support for nested data and automatic delay to improve performance.
  • Custom No Data Message: Displays a custom message or component when there's no data to show.
  • Fully Customizable: Customize styles and behaviors with extensive props.
  • Responsive Design: Adapts to different screen sizes.

Installation

Install the package using npm:

npm install react-lite-table

Quick Start

Here’s a simple example of how to use Table:

import React, { useState } from "react";
import Table from "react-lite-table";

const App = () => {
  const [currentPage, setCurrentPage] = useState(1);
  const [rowsPerPage, setRowsPerPage] = useState(5);
  const [selectedRows, setSelectedRows] = useState([]);
  const [searchValue, setSearchValue] = useState("");

  // Note: To access values from nested objects, use double underscores "__" in the column id.
  const columns = [
    { id: "name", label: "Name" },
    { id: "age", label: "Age" },
    { id: "address__city", label: "City" },
  ];

  const rows = [
    { id: 1, name: "John Doe", age: 25, address: { city: "New York" } },
    { id: 2, name: "Smith", age: 30, address: { city: "San Francisco" } },
    { id: 3, name: "David", age: 31, address: { city: "San Francisco" } },
    { id: 4, name: "Miller", age: 32, address: { city: "San Francisco" } },
    { id: 5, name: "Parker", age: 27, address: { city: "San Francisco" } },
    { id: 6, name: "William", age: 24, address: { city: "San Francisco" } },
    { id: 7, name: "Johny", age: 32, address: { city: "San Francisco" } },
    { id: 8, name: "Peter", age: 30, address: { city: "San Francisco" } },
    { id: 9, name: "Jack", age: 19, address: { city: "San Francisco" } },
    { id: 10, name: "Johnson", age: 24, address: { city: "San Francisco" } },
    // More rows...
  ];

  const searchHandleChange = (value) => {
    setSearchValue(value);
  };

  return (
    <div
      style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        padding: "24px",
      }}
    >
      <Table
        columns={columns}
        rows={rows || []}
        tableWidth="100%"
        tableHeight="70vh"
        enableClientSidePagination={true}
        count={rows.length}
        currentPage={currentPage}
        setCurrentPage={setCurrentPage}
        rowsPerPage={rowsPerPage}
        setRowsPerPage={setRowsPerPage}
        selectedRows={selectedRows}
        setSelectedRows={setSelectedRows}
        searchKey="name"
        searchValue={searchValue}
        searchHandleChange={searchHandleChange}
        enableCustomNoDataComponent
        customNoDataComponent={() => {
          return (
            <img
              style={{ width: "240px", height: "240px", objectFit: "cover" }}
              src="https://static.vecteezy.com/system/resources/previews/009/007/126/non_2x/document-file-not-found-search-no-result-concept-illustration-flat-design-eps10-modern-graphic-element-for-landing-page-empty-state-ui-infographic-icon-vector.jpg"
            />
          );
        }}
      />
    </div>
  );
};

export default App;

Advanced Usage with API Integration

Here’s an example of how to integrate Table with an API to fetch data:

import React, { useEffect, useState } from "react";
import Table from "react-lite-table";

const App: React.FC = () => {
  const [list, setList] = useState({});
  const [currentPage, setCurrentPage] = useState(1);
  const [rowsPerPage, setRowsPerPage] = useState(20);
  const [selectedRow, setSelectedRow] = useState(null);
  const [selectedRows, setSelectedRows] = useState([]);
  const [searchValue, setSearchValue] = useState("");

  const actions = [
    {
      label: "Edit",
      onClick: (values: any) => {
        console.log("edit", values);
      },
    },
    {
      label: "Delete",
      onClick: (values: any) => {
        console.log("delete", values);
      },
    },
    {
      label: "View",
      onClick: (values: any) => {
        console.log("view", values);
      },
    },
  ];

  const columns = [
    { id: "id", label: "Id" },
    {
      id: "title",
      label: "Title",
      truncate: {
        enable: true,
        length: 40,
      },
    },
    {
      id: "thumbnail__alt_text",
      label: "Description",
      truncate: {
        enable: true,
        length: 40,
      },
    },
    {
      id: "api_model",
      label: "Model",
    },
    {
      id: "_score",
      label: "Score",
    },
    {
      id: "action",
      label: "Action",
      render: (values: any) => {
        const isOpen = selectedRow?.id === values.id;

        return (
          <div style={{ position: "relative" }}>
            <div
              onClick={() => {
                setSelectedRow(isOpen ? null : values);
              }}
              style={{ cursor: "pointer" }}
            >
              <svg
                xmlns="http://www.w3.org/2000/svg"
                height="20px" // Adjusting the height
                viewBox="0 96 960 960"
                width="20px" // Adjusting the width
              >
                <path d="M480 856q-25 0-42.5-17.5T420 796q0-25 17.5-42.5T480 736q25 0 42.5 17.5T540 796q0 25-17.5 42.5T480 856Zm0-240q-25 0-42.5-17.5T420 556q0-25 17.5-42.5T480 496q25 0 42.5 17.5T540 556q0 25-17.5 42.5T480 616Zm0-240q-25 0-42.5-17.5T420 316q0-25 17.5-42.5T480 256q25 0 42.5 17.5T540 316q0 25-17.5 42.5T480 376Z" />
              </svg>
            </div>

            {isOpen && (
              <div
                style={{
                  position: "absolute",
                  background: "#fff",
                  border: "1px solid #ccc",
                  boxShadow: "0 2px 8px rgba(0, 0, 0, 0.15)",
                  padding: "10px",
                  zIndex: 99,
                  marginTop: "5px",
                  borderRadius: "5px",
                  transition: "opacity 0.3s ease",
                }}
              >
                <div
                  style={{
                    display: "flex",
                    flexDirection: "column",
                    gap: "6px",
                  }}
                >
                  {actions.map((action) => (
                    <span
                      key={action.label}
                      onClick={() => action.onClick(values)}
                      style={{ fontSize: "12px", cursor: "pointer" }}
                    >
                      {action.label}
                    </span>
                  ))}
                </div>
              </div>
            )}
          </div>
        );
      },
    },
  ];

  useEffect(() => {
    fetch(
      `https://api.artic.edu/api/v1/artworks/search?q=&limit=${rowsPerPage}&page=${currentPage}&q=${searchValue}`
    )
      .then((response) => response.json())
      .then((json) =>
        setList({
          ...json,
          pagination: {
            ...json.pagination,
            total: 100,
          },
        })
      );
  }, [currentPage, rowsPerPage, searchValue]);

  const searchHandleChange = (value: string) => {
    setSearchValue(value);
  };

  return (
    <div
      style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        padding: "24px",
      }}
    >
      <Table
        columns={columns}
        rows={list?.data || []}
        tableWidth="100%"
        tableHeight="70vh"
        enableClientSidePagination={false}
        count={list?.pagination?.total}
        currentPage={currentPage}
        setCurrentPage={setCurrentPage}
        rowsPerPage={rowsPerPage}
        setRowsPerPage={setRowsPerPage}
        selectedRows={selectedRows}
        setSelectedRows={setSelectedRows}
        rowsPerPageOptions={[20]}
        searchKey="thumbnail__alt_text"
        searchValue={searchValue}
        searchHandleChange={searchHandleChange}
      />
    </div>
  );
};

export default App;

Table Properties

rows

  • Type: any[]
  • Default: []
  • Description: The data to display in the table. Each item is an object representing a row.

columns

  • Type: { id: string; label: string; truncate?: { enable: boolean; length: number; }; render?: (row: any) => any; }[]
  • Default: []
  • Description: Defines the columns of the table. Each column object should include id, label, truncate, and render properties.

tableWidth

  • Type: string
  • Default: 100%
  • Description: Width of the table container.

tableHeight

  • Type: string
  • Default: 70vh
  • Description: Height of the table container.

tableContainerStyle

  • Type: CSSProperties
  • Default: {}
  • Description: Custom styles for the table container.

tableHeaderStyle

  • Type: CSSProperties
  • Default: {}
  • Description: Custom styles for the table header.

tableFooterStyle

  • Type: CSSProperties
  • Default: {}
  • Description: Custom styles for the table footer.

enableStickyHeader

  • Type: boolean
  • Default: true
  • Description: Whether to keep the table header fixed while scrolling.

enablePagination

  • Type: boolean
  • Default: true
  • Description: Whether to enable pagination controls.

enableClientSidePagination

  • Type: boolean
  • Default: true
  • Description: Whether to handle pagination on the client side.

count

  • Type: number
  • Default: 0
  • Description: Total number of rows, used for server-side pagination.

currentPage

  • Type: number
  • Default: 1
  • Description: The current page number for pagination.

setCurrentPage

  • Type: Dispatch<number>
  • Description: Function to update the current page number.

enableRowsPerPage

  • Type: boolean
  • Default: true
  • Description: Whether to enable the option to select rows per page.

rowsPerPage

  • Type: number
  • Default: 20
  • Description: Number of rows per page.

setRowsPerPage

  • Type: React.Dispatch<React.SetStateAction<number>>
  • Description: Function to update the number of rows per page.

rowsPerPageOptions

  • Type: number[]
  • Default: []
  • Description: Options for selecting the number of rows per page.

enableSelectionRows

  • Type: boolean
  • Default: true
  • Description: Whether to enable row selection.

selectedRows

  • Type: Array<string | number>
  • Default: []
  • Description: Array of selected row IDs.

setSelectedRows

  • Type: React.Dispatch<React.SetStateAction<Array<string | number>>>
  • Default: () => {}
  • Description: Function to update the selected rows.

enableSorting

  • Type: boolean
  • Default: true
  • Description: Whether to enable column sorting.

enableSearch

  • Type: boolean
  • Default: true
  • Description: Whether to display the search box.

searchKey

  • Type: string
  • Default: ""
  • Description: Key for nested search (use __ for nested fields).

searchValue

  • Type: string
  • Default: ""
  • Description: The current value of the search input.

searchHandleChange

  • Type: (value: string) => void
  • Description: Function to handle changes in the search input.

enableCustomNoDataComponent

  • Type: boolean
  • Default: false
  • Description: Whether to use a custom component when there is no data.

customNoDataComponent

  • Type: () => ReactNode
  • Default: () => <></>
  • Description: Function to render a custom no data component.