Skip to main content

Table

A responsive, sortable, and paginated table component designed to display tabular data with features like sorting, pagination, custom row rendering, and error handling. Perfect for dynamic data tables in React.

๐Ÿ“ฆ Importโ€‹

import { Table } from "@sahilphondekar/react-component-library";

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
dataArray<Object>[]Array of data objects to display in the table
columnsArray<Object>[]Array of column definitions
pageSizenumber10Number of rows per page
currentPagenumber1Current page number
totalPagesnumber1Total number of pages
totalItemsnumber0Total number of items in the data set
sortBystringnullColumn to sort by
sortOrderstring"asc"Sort order ("asc" or "desc")
isLoadingbooleanfalseWhether the table is in loading state
errorstringnullError message to display
onPageChangefunctionโ€” (required)Callback function when page changes
onSortChangefunctionโ€” (required)Callback function when sorting changes
classNamestring""Class name for the outer container
loadernode"Loading.."Loader text or component to display while loading
errorClassNamestring""Class name for error message
tableClassNamestring""Class name for the table
tableHeaderClassNamestring""Class name for table header
tableRowClassNamestring""Class name for table rows
stripedRowClassNameArray<string>["bg-gray-50", "bg-gray-100"]Class names for striped rows
emptyState`stringnode`"No Data Found"
emptyStateClassNamestring""Class name for the empty state message
paginationContainerClassNamestring""Class name for pagination container
paginationButtonClassNamestring""Class name for pagination buttons
paginationPagesClassNamestring""Class name for pagination page number container
paginationTextClassNamestring""Class name for pagination text
sortablebooleantrueWhether columns are sortable

๐Ÿง… Column Objectโ€‹

Each column in the columns array should follow this structure:

NameTypeDescription
headerstringThe header label for the column
accessorstringThe key in the data object to display in the column
isSortablebooleanIf true, the column is sortable
cellRendererfunctionCustom renderer for the cell

๐Ÿ’… Styling Notesโ€‹

  • Table: min-w-full border-collapse
  • Headers: p-1 font-medium text-gray-700 text-sm
  • Rows: border-b text-sm (with striped row colors alternating between bg-gray-50 and bg-gray-100)
  • Pagination: Customizable with classes for button and page elements.

๐Ÿงช Exampleโ€‹

<Table
data={tableData}
columns={[
{ header: "Name", accessor: "name", isSortable: true },
{ header: "Age", accessor: "age", isSortable: true },
{ header: "Email", accessor: "email" },
]}
pageSize={10}
currentPage={1}
totalPages={5}
totalItems={50}
sortBy="name"
sortOrder="asc"
isLoading={false}
error={null}
onPageChange={handlePageChange}
onSortChange={handleSortChange}
/>

๐Ÿง  Tipsโ€‹

  • The sortable prop enables column sorting. Clicking on sortable column headers toggles between ascending and descending order.
  • Use cellRenderer in column definitions to customize how data appears in each cell.
  • The pagination component is fully customizable and includes buttons for navigation.