@codecraftkit
Documentation
The Data Table is used to create very customizable and easy to use tables.
it can be imported from the core.
import { Table } from "@codecraftkit/core";
or alone.
import Table from "@codecraftkit/datatable";
const list = [{_id: '1',name: 'userName1',lastName: 'userLastName1',address: 'Maracaibo',age: '26',occupation: 'Developer',color: 'green'},{_id: '2',name: 'userName2',lastName: 'userLastName2',address: 'Maracaibo',age: '29',occupation: 'Developer'},{_id: '3',name: 'userName3',lastName: 'userLastName3',address: 'Maracaibo',age: '18',occupation: 'Designer',color: 'blue'},{_id: '4',name: 'userName4',lastName: 'userLastName4',address: 'Maracaibo',age: '22',occupation: 'Developer'},{_id: '5',name: 'userName5',lastName: 'userLastName5',address: 'Maracaibo',age: '33',occupation: 'Developer'}];
If the label and the field are the same just pass a string. Example: name
If the label and the field aren't the same pass and object. Example: {field:'lastName', label:'Apellido'}
const head = ['name',{field:'lastName', label:'Apellido'},'address',{field:'age', label:'Edad'},'color','occupation',];
The key label
in the head object is always required in their items, at least you are using the quotes method
function MyApp() {const head = ['name',{field:'lastName', label:'Apellido'},'address',{field:'age', label:'Edad'},'color','occupation',];return <Tablelist={list}head={head}/>}
function MyApp() {const head = ['name',{field:'lastName', label:'Apellido'},'address',{field:'age', label:'Edad'},'color','occupation',];const actions = [{color: 'teal', label: 'Details', icon: <FaEye/>, isLink: {route: 'example-query'}},{color: 'blue', label: 'Edit', icon: <FaRegEdit/>, handler: (user)=>alert('edit '+user.name)},{color: 'red', label: 'Remove', icon: <MdDelete/>, handler: (user)=>alert('remove ' + user.name)},]return <Tablelist={list}head={head}actions={actions}/>}
<Tablelist={list}head={head}leftActions={actions}actionsTitle="Left Actions"/>
How to group the actions
It use the same data structure as actions
<Tablelist={list}head={head}actions={actions}actionGroup/>
Customize the actions according the rows
function MyApp() {const head = ['name',{field:'lastName', label:'Apellido'},'address',{field:'age', label:'Edad'},'color','occupation',];const actionsWithRow = (row) => [{color: row.color,label: 'Edit',icon: <FaRegEdit/>,handler: (user)=>alert('edit '+user.name)},{color: 'red',label: 'Remove',icon:<MdDelete/>,handler: (user)=>alert('remove ' + user.name)},];const leftActionsWithRow = (row) => [{color: 'teal',label: row.name,icon: row.age > 25? <FaUser/>: <FaUserTie />,isLink: {route: 'example-query'}}];return <Tablelist={list}head={head}actionsWithRow={actionsWithRow}leftActionsWithRow={leftActionsWithRow}/>}
A wrapper head above the normal one, the key columns
handle the span of each item
Note: the number of columns in the subhead has to match with the number of columns in the head, counting their columns spans
function MyApp() {const subHead = [{ label:'Name', columns:2 },{ label:'Location', columns:3 },{ label:'Work'}]const footer = [{ label:'Total', columns:2 },{ label:'Location', columns:3 },]return <Tablelist={list}head={head}subHead={subHead}footer={footer}//headStyles={{bg:'gray.50'}}//subHeadStyles={{bg:'gray.50'}}//footerStyles={{bg:'gray.50'}}/>}
The sort will enable to sort the data by every Column
function MyApp() {const [sortBy, setSortBy] = useState('_id')const [orderBy, setOrderBy] = useState('desc')return <Tablelist={list}head={head}sortsortBy={sortBy}setSortBy={setSortBy}orderBy={orderBy}setOrderBy={setOrderBy}/>}
The Test component will receive the Item of the row as a prop, in this case we received it with the name data
function MyApp() {const Test = data => {return <Grid p={4} bg="gray.100">{data.name}</Grid>};const head = ['name',{field:'lastName', label:'Apellido'},'address',{field:'age', label:'Edad'},'color','occupation',];return <Tablelist={list}head={head}collapse={Test}/>}
Allow to select rows and get their values, those Values are store in the selected and selectedRows states
function MyApp() {const [selected, setSelected] = useState([])const [selectedRows, setSelectedRows] = useState([])return <Tablelist={list}head={head}selectionselected={selected}setSelected={setSelected}selectedRows={selectedRows}setSelectedRows={setSelectedRows}limit={5}/>}
A custom function that returns and object of styles and receive the row and its index
<Tablelist={list}head={head}limit={5}rowStyle={(row, index)=>({color: row.age > 25? 'blue.500' : undefined,bg: index === 3 ?'gray.200': undefined})}/>
it can receive a function that returns and object or a simple object
Custom styles for the last row of the table
<Tablelist={list}head={head}limit={5}lastRowStyles={{bg:'gray.200'}}// lastRowStyles={(row, index)=>({})}/>
it can receive a function that returns and object, or a simple object
A function called when a row is click
function MyApp() {const [rowSelected, setRowSelected] = useState({})return <Tablelist={list}head={head}limit={5}rowOnClick={(item)=>setRowSelected(item)}rowStyle={(row)=>({bg: row._id === rowSelected._id? 'gray.200' : undefined,})}/>}
rowOnClick
also receive a second parameter to set the styles of the row clicked
function MyApp() {const [page, setPage] = useState(1);const limit = 1const onPageChange = (pageSelected) =>setPage(pageSelected.selected + 1)return <Grid gap={4}><Box justifySelf="end"><Paginatetotal={5}limit={limit}onPageChange={onPageChange}/></Box><Tablelist={list}head={head}limit={limit}page={page}localPaginate/></Grid>}
list
array<object>
array<Strings>
yes
array of objects which will form the values of the Table
head
array<object>
yes
array of objects which will form the head of the Table
subHead
array<object>
no
Add another head above the normal one, to group the headers
footer
array<object>
no
Add a head at the bottom of the table
actions
array<object>
no
array of objects which will form the right actions button of the Table
actionsWithRow
Function (row) => array<actions>
no
Function that allows to create the actions buttons depending on the row, it must return the new actions array
leftActionsWithRow
Function (row) => array<actions>
no
Function that allows to create the actions buttons depending on the row in the left side of the table, it must return the new actions array
actionsTitle
String
no
Actions
The actions head label
leftActions
array<object>
no
array of objects which will form the left actions button of the Tablen
loading
Boolean
no
false
Show a loading animation
noDataComponent
Component
no
No data
Component that will be displayed as loading Screen
Sort
Boolean
no
false
Enable sort by each Column
sortBy
State
no, unless the prop “sort“ is used
false
Value of the table sort
setSortBy
SetState
no, unless the prop “sort“ is used
false
Set the sort in a state
orderBy
State
no, unless the prop “sort“ is used
false
Value of the table order
setOrderBy
SetSate
no, unless the prop “sort“ is used
Set the order in a state
collapse
Component
no
Render a component inside a collapse
collapseByField
String
no
_id || id
Unique item field from where the collapse will work
collapseOpened
Boolean
no
_false
Keep the collapses opened
localPaginate
Boolean
no
false
Enable Local Paginate
limit
Number
no, unless the prop “localPaginate“ is used
THe limit of items display in one page
selection
Boolean
no
false
enable or disable de selection
page
Number
no, unless the prop “localPaginate“ is used
The number of the actual Page, this is used for selection and Local Paginate
selected
State
no, unless the prop “selection“ is used
Array of Ids of the items selected
setSelected
SetState
no, unless the prop “selection“ is used
Set the Ids in a state
selectedRows
State
no, unless the prop “selection“ is used
Array of items of the items selected
setSelectedRows
SetState
no, unless the prop “selection“ is used
Set the items in a state
rowOnClick
Function (item,setStyles) => void>
no
Function that handles the row click
rowStyle
function
object
no
Styles applied to each rows
lastRowStyles
function
object
no
Styles applied to the last row of the table
headStyles
object
no
Custom Styles applied to the Head
subHeadStyles
object
no
Custom Styles applied to the SubHead
footerStyles
object
no
Custom Styles applied to the footer
noHead
Boolean
no
false
Remove the head component
noBorder
Boolean
no
false
Remove the Table Border
striped
Boolean
no
false
Add an striped color to odd number rows
stripeColor
String
no
Depends on chakra color mode
change stripe color
hovered
Boolean
no
false
Add a hover animation
paginate
Paginate<props>
no
Props to pass to the paginate component at the bottom of the datatable, pass them to show the paginate
paginateBottom
Boolean
no
true
Enable to show the paginate