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.
- Basic
- Striped
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" },
]}
/>
);
}
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" },
]}
isStriped
/>
);
}
Common Props
The Table component accepts the following props:
columns- array of column definitionsdata- array of data objectsisStriped- whether to display alternating row colorsisLoading- renders a loader instead of the rowswithStickyHeader- whether to make the header stick to the top when scrollingnoDataItemsMessage- message to display when there's no data (defaults toNo data available)getRowStyle- function that returns style props for a specific rowborderRadius- the border radius of the table in pixels (defaults totheme.styles.borderRadius * 2)borderTopLeftRadius,borderTopRightRadius,borderBottomLeftRadius,borderBottomRightRadius- the border radius of a single corner in pixels (defaults toborderRadius)containsOverflowComponents- set it when cells contain elements that need to overflow the table (dropdowns, tooltips, ...)onClickRow- callback function when a row is clickedonClickAllCheckboxes- callback function when the header checkbox is clicked. Receives the newcheckedstatepageSize- the number of items to display per pagepageCount- 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 withpageSizeandpageCount.onChangeFilter- callback function when the filter changesonChangeFilterDataValue- 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.
- Loading
- Row Style
- Pagination
import { Table } from "react-better-html";
function App() {
return (
<Table
columns={[{ type: "text", label: "Name", keyName: "name" }]}
data={[]}
isLoading
/>
);
}
import { Table } from "react-better-html";
function App() {
return (
<Table
columns={[
{ type: "text", label: "Name", keyName: "name" },
{ type: "text", label: "Status", keyName: "status" },
]}
data={[
{ name: "John Doe", status: "Active" },
{ name: "Jane Smith", status: "Blocked" },
]}
getRowStyle={(item) => ({
opacity: item.status === "Blocked" ? 0.5 : undefined,
})}
/>
);
}
import { Table } from "react-better-html";
function App() {
return (
<Table
columns={[{ type: "text", label: "Name", keyName: "name" }]}
data={users}
pageSize={10}
onChangePage={(page) => console.log("Current page:", page)}
/>
);
}
For server side pagination pass pageCount and load the data for the page in onChangePage.
Column Props & Types
All column types support the following properties:
hidden- hides the columnlabel- the column header textrenderLabel- function to render a custom header. Receives thelabelwidth- 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 columnclickStopPropagation- whether to stop the click event from propagating to the parent elementfilter- 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 displayformat- optional function that receives the item and its index and returns the text to displaygetTextProps- 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
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 timerender- function to render the content inside the expanded rowonExpand- optional callback function to be called when a row is expandedonCollapse- optional callback function to be called when a row is collapsed
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 checkpresets- 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 modalwithSearch- whether to render a search input in the filter modallist- the array of items to be displayed in the list as possible filter valuesgetItemValue- optional function to get the value of the item to compare against the list values (defaults to the value ofkeyName)
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 pagesetCurrentPage- changes the current pagepagesCount- the total number of pagessetCheckedItems- changes the checked state of the rows (used with the Checkbox Column)