1.0.2 • Published 1 year ago

@cyces-innovation-labs/lego-reactjs-table v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

lego-react-table

lego-react-table is a highly customizable and feature-rich React table component designed to handle large datasets efficiently. It includes various functionalities such as pagination, sorting, searching, row selection, and custom rendering.

Features

  • Sticky Header: Option to keep the header fixed while scrolling.
  • Pagination: Supports both client-side and server-side pagination.
  • Row Selection: Allows selecting multiple rows with checkboxes.
  • Sorting: Enable sorting on specified columns.
  • Search: Built-in search functionality.
  • Custom No Data Component: Render a custom component when there's no data.
  • Filtering: Integrate custom filter components.
  • Fully Customizable: Customize styles and behaviors with extensive props.

Installation

Install the package using npm:

npm install lego-react-table

Quick Start

Here's a simple example to get you started with Table:

import React, { useState } from 'react';
import Table from 'lego-react-table';

const App = () => {
  const [page, setPage] = useState(0);
  const [rowsPerPage, setRowsPerPage] = useState(10);
  const [selectedRows, setSelectedRows] = 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: "Jane Doe", age: 30, address: { city: "San Francisco" } },
    // More rows...
  ];

  return (
    <Table
      columns={columns}
      rows={rows}
      page={page}
      setPage={setPage}
      rowsPerPage={rowsPerPage}
      setRowsPerPage={setRowsPerPage}
      selectedRows={selectedRows}
      setSelectedRows={setSelectedRows}
      enableStickyHeader
      enablePagination
      enableRowSelection
      enableSorting
      enableSearch
       // Note: To search values from nested objects, use double underscores "__" in the searchkey.
      searchkey="address__city"
    />
  );
};

export default App;

Integration with External API Example

import { useEffect, useState } from "react";
import Table from 'lego-react-table';

function App() {
  const [list, setList] = useState({});
  const [page, setPage] = useState(0);
  const [rowsPerPage, setRowsPerPage] = useState(5);
  const [selectedRows, setSelectedRows] = useState([]);
  const [searchValue, setSearchValue] = useState();

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

  // Note: To access values from nested objects, use double underscores "__" in the column id.
  const columns = [
    { id: "id", label: "Id" },
    {
      id: "title",
      label: "Title",
      width: 300,
      truncate: {
        enable: true,
        length: 40,
      },
    },
    {
      id: "thumbnail__alt_text",
      label: "Description",
      width: 300,
      truncate: {
        enable: true,
        length: 40,
      },
    },
    {
      id: "api_model",
      label: "Model",
      render: (row: any) => {
        return (
          <span>
            {row?.api_model?.[0]?.toUpperCase() + row?.api_model?.slice(1)}
          </span>
        );
      },
    },
    {
      id: "_score",
      label: "Score",
    },
  ];

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

  return (
      <Table
        rows={list?.data || []}
        columns={columns}
        enableClientSidePagination={false}
        count={list?.pagination?.total}
        page={page}
        setPage={setPage}
        rowsPerPage={rowsPerPage}
        setRowsPerPage={setRowsPerPage}
        rowsOptions={[5, 10]}
        selectedRows={selectedRows}
        setSelectedRows={setSelectedRows}
        searchHandleChange={searchHandleChange}
      />
  );
}

export default App;

Column Configuration

Defines the configuration for each column in the table.

PropertyTypeDescription
idstringThe column identifier. It's used to map with row values.
labelstringDisplay name for the column.
widthnumberThe width of the column.
truncateobjectTruncation configuration for the column text.
  enablebooleanEnable or disable text truncation.
  lengthnumberNumber of characters to show before truncation.
render(row: any) => ReactNodeCustom render function for the column content.

Table Properties

Defines the properties for the table component.

PropertyTypeDescription
rowsany[]The data rows to be displayed in the table.
columnsColumn[]The configuration of the columns in the table.
tableWidthstringThe width of the table.
tableHeightstringThe height of the table.
containerSxCSSPropertiesStyles for the container.
tableContainerSxCSSPropertiesStyles for the table container.
tableHeaderSxCSSPropertiesStyles for the table header.
tableFooterSxCSSPropertiesStyles for the table footer.
checkboxSxCSSPropertiesStyles for the checkbox component.
tableCellSxCSSPropertiesStyles for the table cell.
enableStickyHeaderbooleanEnable sticky headers for the table.
enablePaginationbooleanShow pagination for the table.
enableClientSidePaginationbooleanEnable client-side pagination.
countnumberThe total count of rows.
pagenumberThe current page number.
setPageDispatch<number>Function to update the current page number state.
enableRowSelectionbooleanEnable row selection functionality.
selectedRowsArray<string \| number>The array of selected rows.
setSelectedRowsDispatch<Array<string \| number>>Function to update the selected rows state.Yes
enableSortingbooleanEnable column sorting functionality.
enableRowsPerPagebooleanEnable selection of rows per page.
rowsPerPagenumberThe number of rows per page.
setRowsPerPageDispatch<number>Function to update the number of rows per page state.
rowsOptionsnumber[]The options for the number of rows per page.
enableCustomNoDataComponentbooleanEnable custom "No Data" component.
customNoDataComponent() => ReactNodeA custom component to display when there is no data.
enableFilterbooleanEnable filtering functionality.
filterComponent() => ReactNodeA custom component for filtering the data.
filterContainerSxCSSPropertiesStyles for the filter container.
cancelButtonSxCSSPropertiesStyles for the cancel button in the filter.
submitButtonSxCSSPropertiesStyles for the submit button in the filter.
filterCancelHandler() => voidFunction to handle filter cancel.
filterSubmitHandler() => voidFunction to handle filter submit.
enableSearchbooleanEnable search functionality.
formControlSxCSSPropertiesStyles for the form control.
inputSxCSSPropertiesStyles for the input component.
searchKeystringThe search key for filtering rows.
searchHandleChange(value: string) => voidFunction to handle changes in the search input.