React Simple Table (RST) provides a way to simple define table component with basic functionality.
To install this package simply type:
npm install --save-dev @simple-gui/react-simple-table
Now you can use it in your project:
// somewhere in your components...
import Table from "@simple-gui/react-simple-table";
<Table {...props} />
Base class name. All components within table will inherit from that base class and add some sufix to it (according to the BEM methodology).
Suffixes:
-container
fortable
container_head
forthead
_head-row
for row inthead
_th
forth
_body
fortbody
_row
for row intbody
_cell
fortd
Extra class names:
drop-target-entered
fortable head cell
element while reordering column
List of objects. Every object is a definition of column in your table. Each column could have following properties:
The title of the column, displayed in <thead>
The name of the field from which column will display
data. field
could indicate name of the property from your data objects,
if your data is defined as list of objects, or index of the element if
your data is defined as list of lists
One of the following: ASC
, DESC
. Describes direction
of data sorting using on that column. This property only adds class on
columns cells, this will not sort your data!
For true
or undefined
it will render Sorter
with onSort
function.
If false
then sorting capability for given column will be disabled
Width of the column specified as a number (rendered with px
as the unit in style
property of the cell).
Function that should return header component.
Takes two arguments - renderTitle
and renderSorter
functions (you can use them to render title and sorter).
If you want to specify column unrelated to specified data, for example column with available actions of item, you can use this function. It accepts two parameters "
- data of current row
- toggler function - if
details
props is specified forTable
, then this function enables toggling details row
List of objects or lists. Every item represents one row of your data.
- If item is an array, then
columns[n].field
should point to the index of element in that item. - If item is an object, then
columns[n].filed
should point to the property name of the item.
Function that returns component. Takes one argument
- data from clicked row (in the form specified in
data
list).
If true
and semantic
equals false
, then rows details component will be rendered inside clicked row
as last child with css class with-inlined-details
.
Function that returns component rendered
in table header. Takes one argument - string specified as sorted
property
in columns definition. If you wan't to display sorter in header, you have to
specify sorted
property in column definition.
Valid css value for "max-height" property. If specified then entire table is wrapped in div with that "max-height" applied.
Function fired after user scrolled to the bottom of the table. Takes one argument - react event.
Function fired after click on th
element. Takes one argument - object from columns
of clicked column.
Default: true
. If false
, table will render with non semantic markup - divs
will be use instead of all
HTML table elements.
Function that returns component wrapping entire table body.
Takes one argument - body
which should be rendered inside your component.
Function that returns component wrapping every row.
Takes two arguments - row
which should be rendered inside your component and cells
- list of cells
that will be rendered in the row.
Function that is fired after columns reorder. If onReorder
property is provided, all columns are draggable
and user is able to reorder columns using pointer.
Takes two arguments - source column title
and target column title
.
Important - columns title should be defined as a string, not a function. If you need to define
custom components for columns header, please use columns.headerComponent
.
Function that is fired after click on the row cells.
Takes two arguments - clicked row
and event
.
<Table
data={[
{
id: 1,
name: "John"
},
{
id: 2,
name: "Bob"
}
]}
columns={[
{
title: "User name",
field: "name"
},
{
title: "User ID",
field: "id"
}
]}
/>
// result: Table with two columns:
// 1. User name -> from "name" field of data item
// 2. User ID -> from "id" field of data item
<Table
data={[
[
1,
"John"
],
[
2,
"Bob"
]
]}
columns={[
{
title: "User name",
field: 1
},
{
title: "User ID",
field: 0
}
]}
/>
// result: Table with two columns:
// 1. User name -> from [1] field of data item
// 2. User ID -> from [0] field of data item
Table with three columns and two rows, data defined as list of objects. Last column contains button.
<Table
data={[
{
id: 1,
name: "John"
},
{
id: 2,
name: "Bob"
}
]}
columns={[
{
title: "User name",
field: "name"
},
{
title: "User ID",
field: "id"
},
{
title: "Actions",
component: user => <button>Delete user: {user.name}</button>
}
]}
/>
// result: Table with three columns:
// 1. User name -> from "name" field of data item
// 2. User ID -> from "id" field of data item
// 3. Actions -> Button with "Delete user: John" in first row, and "Delete user: Bob" in second row.
<Table
data={...like in previous example...}
columns={[
{
title: "User name",
field: "name"
},
{
title: "User ID",
field: "id"
}
]}
details={user => <p className="details">User: {user.id} {user.name}</p>}
/>
// result: Table with two columns:
// 1. User name -> from "name" field of data item
// 2. User ID -> from "id" field of data item
// After click on any row details row expands with paragraph
// containing text "User: 1 John" for first row, and "User 2 Bob" for second row.
<Table
data={...like in previous example...}
columns={[
{
title: "User name",
field: "name",
sorted: "ASC"
},
{
title: "User ID",
field: "id"
}
]}
sorterComponent={sorted => <span>{sorted}</span>}
/>
// result: Table with two columns:
// 1. User name -> from "name" field of data item
// 2. User ID -> from "id" field of data item
// First column will contain span element with "ASC" text inside.
// Second column will not have any sorter indicator, because
// it doesn't have "sorted" specified.
Table with three columns and two rows, data defined as list of objects. Last column contains button.
<Table
data={[
{
id: 1,
name: "John"
},
{
id: 2,
name: "Bob"
}
]}
columns={[
{
title: "User name",
field: "name"
},
{
title: "User ID",
field: "id"
},
{
title: "Actions",
component: user => <button>Delete user: {user.name}</button>
}
]}
/>
// result: Table with three columns:
// 1. User name -> from "name" field of data item
// 2. User ID -> from "id" field of data item
// 3. Actions -> Button with "Delete user: John" in first row, and "Delete user: Bob" in second row.
<Table
data={...like in previous example...}
columns={[
{
title: "User name",
field: "name"
},
{
title: "User ID",
field: "id"
}
]}
details={user => <p className="details">User: {user.id} {user.name}</p>}
/>
// result: Table with two columns:
// 1. User name -> from "name" field of data item
// 2. User ID -> from "id" field of data item
// After click on any row details row expands with paragraph
// containing text "User: 1 John" for first row, and "User 2 Bob" for second row.
<Table
data={...like in previous example...}
columns={[
{
title: "User name",
field: "name",
sorted: "ASC"
},
{
title: "User ID",
field: "id"
}
]}
sorterComponent={sorted => <span>{sorted}</span>}
/>
// result: Table with two columns:
// 1. User name -> from "name" field of data item
// 2. User ID -> from "id" field of data item
// First column will contain span element with "ASC" text inside.
// Second column will not have any sorter indicator, because
// it doesn't have "sorted" specified.