Skip to main content

Table Component

The <Table> component is a versatile React component that provides a customizable table with support for different column types, styling options, and interactive features.

Basic Usage​

The Table component requires two main props: columns and data. The columns prop defines the structure of the table, while the data prop provides the content.

import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "text",
label: "Name",
keyName: "name",
},
{
type: "text",
label: "Age",
keyName: "age",
},
{
type: "text",
label: "Email",
keyName: "email",
},
]}
data={[
{ name: "John Doe", age: 28, email: "john@example.com" },
{ name: "Jane Smith", age: 32, email: "jane@example.com" },
{ name: "Mike Johnson", age: 45, email: "mike@example.com" },
]}
/>
);
}

Common Props​

The Table component accepts the following props:

  • columns - array of column definitions
  • data - array of data objects
  • isStriped - whether to display alternating row colors
  • isLoading - renders a loader instead of the rows
  • withStickyHeader - whether to make the header stick to the top when scrolling
  • noDataItemsMessage - message to display when there's no data (defaults to No data available)
  • getRowStyle - function that returns style props for a specific row
  • borderRadius - the border radius of the table in pixels (defaults to theme.styles.borderRadius * 2)
  • borderTopLeftRadius, borderTopRightRadius, borderBottomLeftRadius, borderBottomRightRadius - the border radius of a single corner in pixels (defaults to borderRadius)
  • containsOverflowComponents - set it when cells contain elements that need to overflow the table (dropdowns, tooltips, ...)
  • onClickRow - callback function when a row is clicked
  • onClickAllCheckboxes - callback function when the header checkbox is clicked. Receives the new checked state
  • pageSize - the number of items to display per page
  • pageCount - the total number of pages. Used for when the rest of the items in the table are not present.
  • onChangePage - callback function when the page changes. Used together with pageSize and pageCount.
  • onChangeFilter - callback function when the filter changes
  • onChangeFilterDataValue - callback function when the filter are applied and the data array is filtered.
  • defaultFilterData - the filters applied on mount. The keys are the indexes of the columns.
import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[{ type: "text", label: "Name", keyName: "name" }]}
data={[]}
isLoading
/>
);
}

Column Props & Types​

All column types support the following properties:

  • hidden - hides the column
  • label - the column header text
  • renderLabel - function to render a custom header. Receives the label
  • width - the width of the column (in pixels)
  • minWidth - the minimum width of the column (in pixels)
  • maxWidth - the maximum width of the column (in pixels)
  • align - the text alignment within the column
  • clickStopPropagation - whether to stop the click event from propagating to the parent element
  • filter - the filter type for the column. Learn more in the Filters section.

Column type-specific properties:

Text Column​

The text column type displays a text from the specified value in your data objects.

import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "text",
label: "Name",
keyName: "name"
},
{
type: "text",
label: "Age",
keyName: "age",
format: (item) => `${item.age} years old`,
getTextProps: {
color: "#ff0000"
}
}
]}
data={[
{ name: "John Doe", age: 28 },
{ name: "Jane Smith", age: 32 }
]}
/>
);
}

Text columns support the following props:

  • type - must be set to "text"
  • keyName - the key in your data object to display
  • format - optional function that receives the item and its index and returns the text to display
  • getTextProps - optional object or function to get props for the rendered text component inside the table cell. All props from the <Text> component are supported

Element Column​

The element column allows you to render custom React elements for each cell.

import { Table, Text } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "text",
label: "Name",
keyName: "name"
},
{
type: "element",
label: "Status",
render: (item, index) => (
<Text color={item.status === "Active" ? "green" : "red"} fontWeight={700}>
{item.status}
</Text>
)
}
]}
data={[
{ name: "John Doe", status: "Active" },
{ name: "Jane Smith", status: "Inactive" }
]}
/>
);
}

Element columns support the following props:

  • type - must be set to "element"
  • render - a function that returns a React element, receives the item and index as parameters

Image Column​

The image column displays images from URLs stored in your data.

import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "image",
label: "Avatar",
getImageProps: (item) => ({
src: item.avatarUrl,
width: 50,
height: 50,
borderRadius: 999
})
},
{
type: "text",
label: "Name",
keyName: "name"
}
]}
data={[
{ name: "John Doe", avatarUrl: "https://example.com/john.jpg" },
{ name: "Jane Smith", avatarUrl: "https://example.com/jane.jpg" }
]}
/>
);
}

Image columns support the following props:

  • type - must be set to "image"
  • getImageProps - optional object or function to get props for the rendered image component inside the table cell. All props from the <Image> component are supported

Checkbox Column​

The checkbox column displays checkboxes that can be toggled. The checked state is kept inside the table and can be read/changed with the onChange from getToggleInputProps and the setCheckedItems ref method.

import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "checkbox",
getToggleInputProps: {
onChange: (checked, item) => console.log(item.name, checked)
}
},
{
type: "text",
label: "Name",
keyName: "name"
}
]}
data={[{ name: "John Doe" }, { name: "Jane Smith" }]}
onClickAllCheckboxes={(checked) => console.log("All checked:", checked)}
/>
);
}

Checkbox columns support the following props:

  • type - must be set to "checkbox"
  • getToggleInputProps - optional object or function to get props for the rendered toggle input component inside the table cell. All props from the <ToggleInput.checkbox> component are supported
tip

The table prop onClickAllCheckboxes can be used to render a checkbox in the header that will toggle all checkboxes at ones.

Expand Column​

The expand column displays an arrow that can be toggled to show/hide additional content.

import { Table, Text } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "expand",
render: (item) => <Text>{item.isSelected ? "Selected" : "Not selected"}</Text>
},
{
type: "text",
label: "Name",
keyName: "name"
}
]}
data={[
{ name: "John Doe", isSelected: true },
{ name: "Jane Smith", isSelected: false }
]}
/>
);
}

Expand columns support the following props:

  • type - must be set to "expand"
  • onlyOneExpanded - whether to allow only one row to be expanded at a time
  • render - function to render the content inside the expanded row
  • onExpand - optional callback function to be called when a row is expanded
  • onCollapse - optional callback function to be called when a row is collapsed
warning

The table prop isInsideTableExpandRow must be set when rendering a <Table> component inside a Expand column. It is set to the inner <Table> component.

Column Filters​

Number filter​

import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "text",
label: "Name",
keyName: "name"
},
{
type: "text",
label: "Age",
keyName: "age",
filter: "number",
getValue: (item) => item.age
}
]}
data={[
{ name: "John Doe", age: 23 },
{ name: "Jane Smith", age: 28 }
]}
/>
);
}

Number filter support the following props:

  • filter - must be set to "number"
  • getValue - function to get the value for the filter check

Date and Date-Time filter​

import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "text",
label: "Name",
keyName: "name"
},
{
type: "text",
label: "Date born",
keyName: "dateBorn",
filter: "date",
presets: ["thisYear", "lastYear"],
getValue: (item) => new Date(item.dateBorn)
}
]}
data={[
{ name: "John Doe", dateBorn: "2002-01-18" },
{ name: "Jane Smith", dateBorn: "1997-07-02" }
]}
/>
);
}

Date and Date-Time filters support the following props:

  • filter - must be set to either "date" or "date-time"
  • getValue - function to get the value for the filter check
  • presets - optional array of presets to be displayed in the filter modal UI. Available presets: today, yesterday, tomorrow, thisWeek, thisMonth, thisYear, lastWeek, lastMonth, lastYear, nextWeek, nextMonth, nextYear

List filter​

import { Table } from "react-better-html";

function App() {
return (
<Table
columns={[
{
type: "text",
label: "Name",
keyName: "name"
},
{
type: "text",
label: "Age",
keyName: "age",
filter: "list",
list: [
{
value: 23,
label: "Age 23"
},
{
value: 28,
label: "Age 28"
}
]
}
]}
data={[
{ name: "John Doe", age: 23 },
{ name: "Jane Smith", age: 28 }
]}
/>
);
}

List filter support the following props:

  • filter - must be set to "list"
  • withTotalNumber - whether to show the total number of items in the filter modal
  • withSearch - whether to render a search input in the filter modal
  • list - the array of items to be displayed in the list as possible filter values
  • getItemValue - optional function to get the value of the item to compare against the list values (defaults to the value of keyName)

Memoized Filters​

When memoizeFilters is passed together with a name, the applied filters are saved and restored the next time the table is rendered. The memoizeFiltersLifespan prop sets how long (in milliseconds) the filters are kept (defaults to 2 hours). The time resets every time the filters change.

<Table
name="users"
memoizeFilters
columns={columns}
data={users}
/>

Ref API​

The table can be controlled using a ref:

type TableRef = {
currentPage: number;
setCurrentPage: React.Dispatch<React.SetStateAction<number>>;
pagesCount: number;
setCheckedItems: React.Dispatch<React.SetStateAction<boolean[]>>;
};
  • currentPage - the current page
  • setCurrentPage - changes the current page
  • pagesCount - the total number of pages
  • setCheckedItems - changes the checked state of the rows (used with the Checkbox Column)