0.2.6 • Published 6 months ago

@earnest-data/earnest-table v0.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

earnest-table

A customizable, responsive data table component for React with sorting, filtering, and pagination.

Table of Contents

Prop Definitions

PropTypeDescription
columnsColumnDef<any, any>[]An array of column definitions. Each column must have a header (column title) and accessorKey (key to access the column data).
dataRecord<string, any>[]An array of data objects to be displayed in the table.
onRowSelect(selectedIndices: string[] \| number[]) => voidCallback function to handle row selection.
filterOptionsany[]List of filter options available for use in the table filtering.
clearFilters() => voidFunction to clear all filters applied to the table.
onClickNext() => voidCallback function to handle 'Next' button click in pagination.
onClickPrevious() => voidCallback function to handle 'Previous' button click in pagination.
totalCountnumberTotal number of items in the table.
pageSizenumberNumber of items per page for pagination.
pagenumberCurrent page number for pagination.
onRowClick(rowData: Record<string, any>, event: MouseEvent<HTMLTableRowElement>) => voidCallback function triggered when a row is clicked, receiving the row's data and the click event.
tabsITabsList of tabs to display for segmenting the data (e.g., for filtering or categorizing).
onTabChange(index: number, tab: any) => voidCallback when a tab is selected. Receives the index of the tab and the tab object.
onDownload(event: any) => voidCallback for downloading the data in the table.
headerConfigHeaderConfigConfiguration for the table header, which includes heading, description, and label.
headerActions (New ✨)ReactNodeOptional content that can be added to the header wrapper (e.g., a button).
classNamesIClassNameCustom class names for styling different parts of the table, header, filter, etc.
disablePaginationbooleanDetermines whether pagination is disabled for the table.
disableDownloadbooleanControls whether the "Download" button should be displayed.
filterContentReactNodeOptional content that can be added to the filter toolbar (e.g., a button).
errorMessagestringAn optional error message to display when there's no data.
showClearFiltersButtonbooleanControls whether the "Clear Filters" button should be displayed.
showSearchInputbooleanControls whether the search input box should be displayed in the toolbar.
enableCheckboxSelectionbooleanControls whether the checkbox column should be displayed.

Customization (classNames)

SectionCustomization (className)Description
Filter ChipfilterChipClass for customizing the appearance of the filter chips.
Filter Chip IconfilterChipIconClass for customizing the icon inside the filter chip.
Filter Chip LabelfilterChipLabelClass for customizing the label inside the filter chip.
Filter Chip Dropdown (New ✨)filterChipDropdownClass for customizing the dropdown of the filter chip.
Table WrappertableWrapperClass for customizing the wrapper around the table.
DataTabledataTableClass for customizing the main data table container.
Table Header CelltableHeaderCellClass for customizing the header cells.
Table RowtableRowClass for customizing the individual table rows.
Table Row CelltableRowCellClass for customizing the individual row cells.
Sorting Arrow IconsortingArrowIconClass for customizing the sorting arrow icon in the header cells.
Header WrapperheaderWrapperClass for customizing the wrapper around the table header.
Header Actions Wrapper (New ✨)headerActionsWrapperClass for customizing the container that holds additional actions in the table header.
HeadingheadingClass for customizing the main heading (title).
SubheadingsubHeadingClass for customizing the subheading under the title.
BadgebadgeClass for customizing the badges, if used.
Download ButtondownloadButtonClass for customizing the download button.
Download Button IcondownloadButtonIconClass for customizing the icon inside the download button.
ToolbartoolbarClass for customizing the toolbar under the header wrapper.
Tabs WrappertabsWrapperClass for customizing the wrapper around the tabs.
Tabs PaneltabsPanelClass for customizing the panel of tabs.
Tab ItemtabItemClass for customizing the individual tab items.
Selected TabselectedTabClass for customizing the selected tab.
Pagination WrapperpaginationWrapperClass for customizing the pagination wrapper.
Pagination InfopaginationInfoClass for customizing the pagination information display. (1-10 of 100 items)
Pagination Button WrapperpaginationButtonWrapperClass for customizing the pagination button container.
Pagination Button PreviouspaginationButtonPreviousClass for customizing the "Previous" button in pagination.
Pagination Button NextpaginationButtonNextClass for customizing the "Next" button in pagination.
Pagination Button PreviouspaginationButtonPreviousClass for customizing the "Previous" button in pagination.
SpinnerspinnerClass for customizing the loading spinner.

Example Usage

import React, { useState, useEffect, useCallback } from "react";
import { DataTable, DataTableColumn, ITabs } from "@earnest-data/earnest-table";
import styles from "./index.module.scss";

const pageSize = 10;

export interface OrderRecord {
  id: string;
  orderAmount: number;
  orderStatus: string;
  city: string;
  state: string;
  processed: boolean;
}

export default function Home() {
  const [data, setData] = useState<OrderRecord[]>([]);
  const [loading, setLoading] = useState(true);
  const [count, setCount] = useState(0);
  const [page, setPage] = useState(1);
  const [count, setCount] = useState(0);
  const [page, setPage] = useState(1);
  const [filters, setFilters] = useState<any>({});
  const [selectedRows, setSelectedRows] = useState<number[]>([]);

  const fetchData = useCallback(async () => {
    // API call to fetch data here
  }, []);

  useEffect(() => {
    fetchData();
  }, [fetchData]);

  const columns: DataTableColumn[] = [
    { header: "ID", accessorKey: "id" },
    { header: "Order Status", accessorKey: "orderStatus" },
    { header: "City", accessorKey: "city", size: 20 },
    { header: "Processed", accessorFn: (row) => (row.processed ? "Yes" : "No") },
  ];

  const orderData = useMemo(() => data, [data]);

  const handleApiDownload = async () => {
    // Make API call for downloading or use default download functionality
  };

  const filterOptions = useMemo(() => [
    {
      label: "Order Status",
      options: ["success", "inprogress", "pending", "failed", "paid"],
      id: "orderStatus",
      value: filters.orderStatus,
      onOptionChange: (value: any) => 
          setFilters((prev: any) => ({ ...prev, orderStatus: value })),,
    },
    {
      label: "Processed",
      id: "processed",
      options: ["true", "false"],
      value: filters.processed,
     onOptionChange: (value: any) =>
          setFilters((prev: any) => ({ ...prev, processed: value === "true" })),
    },
    {
      label: "City",
      id: "city",
      options: ["Delhi", "BOM", "Patna"],
      value: filters.city,
        onOptionChange: (value) =>
          setFilters((prev: any) => ({ ...prev, city: value })),
    },
  ], [filters]);

  const tabs: ITabs = [
    { label: "All", value: undefined },
    { label: "Processed", value: "true" },
    { label: "Not Processed", value: "false" },
  ];

  const onTabChange = (index: number, tab: any) => {
    setFilters((p: any) => ({ ...p, processed: tab.value }));
  };

  const handleRowSelect = (selectedIndices: any) => {
    setSelectedRows(selectedIndices);
  };

  const handleNext = () => {
    setPage((p) => p + 1);
  };

  const handlePrevious = () => {
    setPage((p) => Math.max(p - 1, 1));
  };

  const clearFilters = () => setFilters({});

   const handleDelete = () => {
    console.log("Deleting selected rows:", selectedRows);
  };

  const headerConfig: HeaderConfig = {
    headline: "My Data Table",
    label: "Company Orders",
    description: "Default description text comes here.",
  };

  return (
    <div>
      <DataTable
        columns={columns}
        data={orderData}
        onRowSelect={handleRowSelect}
        filterOptions={filterOptions}
        tabs={tabs}
        onDownload={handleApiDownload}
        onRowClick={(rowData, event) => {
          console.log("Row clicked:", rowData);
        }}
        clearFilters={clearFilters}
        onTabChange={onTabChange}
        onClickNext={handleNext}
        onClickPrevious={handlePrevious}
        page={page}
        totalCount={count}
        pageSize={pageSize}
        headerConfig={headerConfig}
        headerActions={<button>Add New</button>}
        classNames={{
          filterChip: styles.filterChip,
          filterChipIcon: styles.filterChipIcon,
          filterChipLabel: styles.filterChipLabel,
          tableWrapper: styles.tableWrapper,
          dataTable: styles.dataTable,
          tableHeaderCell: styles.tableHeaderCell,
          tableRow: styles.tableRow,
          tableRowCell: styles.tableRowCell,
          headerWrapper: styles.headerWrapper,
          headerActionsWrapper: styles.headerActionsWrapper,
          heading: styles.heading,
          subHeading: styles.subHeading,
          badge: styles.badge,
          downloadButton: styles.downloadButton,
          downloadButtonIcon: styles.downloadButtonIcon,
          toolbar: styles.toolbar,
          tabsWrapper: styles.tabsWrapper,
          tabsPanel: styles.tabsPanel,
          tabItem: styles.tabItem,
          selectedTab: styles.selectedTab,
          paginationWrapper: styles.paginationWrapper,
          paginationInfo: styles.paginationInfo,
          paginationButtonWrapper: styles.pagintaionButtonWrapper,
          paginationButtonPrevious: styles.paginationButtonPrevious,
          paginationButtonNext: styles.paginationButtonNext,
          filterChipDropdown: styles.filterChipDropdown,
          spinner: styles.spinner
        }}
        filterContent={
          selectedRows.length > 0 && (
            <button onClick={handleDelete} style={{ marginLeft: "auto" }}>
              Delete Selected
            </button>
          )
        }
        disablePagination={false}
        disableDownload={true}
        errorMessage="No data to display"
        showClearFiltersButton={false}
        showSearchInput={true}
        enableCheckboxSelection={true}
      />
    </div>
  );
}
0.2.6

6 months ago

0.2.5

6 months ago

0.2.4

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.8

8 months ago

0.1.7

8 months ago

0.1.6

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.27

10 months ago

0.0.26

11 months ago

0.0.25

11 months ago

0.0.24

11 months ago

0.0.23

11 months ago

0.0.22

11 months ago

0.0.21

11 months ago

0.0.20

11 months ago

0.0.19

11 months ago

0.0.18

11 months ago

0.0.17

11 months ago

0.0.16

11 months ago

0.0.15

11 months ago

0.0.14

11 months ago

0.0.13

11 months ago

0.0.12

11 months ago

0.0.11

11 months ago

0.0.10

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago