Thanks to Tanner Linsley have developed a great open source data-grid named react-table for ReacJs especially the react-table component. When working on a big project for my company and had been required to add-in a feature react-table that allows folding the columns.
So, I had developed a HOC for react-table for that feature and today, by writing this post I would like to share it with all the ReacJs developers.
FoldableTable HOC
FoldableTable is a HOC that make the columns are foldable. The reason I developed this HOC because when working on the real project related to the financial which display so many columns for validation and comparison.
So foldable columns allow users to temporary hidden the unwanted to columns so that they can focus on the data that they want to see.
How it works
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import ReactTable from 'react-table' import FoldableTableHOC from 'react-table/lib/hoc/foldableTable' const FoldableTable = FoldableTableHOC(ReactTable); class App extends Component { render() { return <FoldableTable data={/*you data*/} columns={[/*your coluumns defination*/]}/> } } export default App; |
It will scan all the columns which foldable is true and apply the foldable column feature. This feature will work for both normal columns and header columns as samples below.
-
With Header Columns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
render(){ return <FoldableTable columns={[{ Header: "Name", foldable: true, columns: [{ Header: "First Name", accessor: "first_name" },{ Header: "Last Name", accessor: "last_name" }] },{ Header: "Info", foldable: true, columns: [{ Header: "Email", accessor: "email" },{ Header: "Gender", accessor: "gender" }] }] }/> } |
-
With Normal Columns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
render() { return <FoldableTable columns={[{ Header: "First Name", accessor: "first_name" }, { Header: "Last Name", accessor: "last_name", foldable: true, }, { Header: "Email", accessor: "email", foldable: true, },{ Header: "Gender", accessor: "gender", foldable: true, }]}></FoldableTable> } |
- The
FoldableTable
is also fully compatible with existing HOCs, below is with selectTableHOC.
State management
If you would like to manage the state of FoldableTable, then add the following codes.
1 2 3 4 5 6 7 |
render() { return <FoldableTable onFoldChange={newFolded => this.setState(p => { return { folded: newFolded } })} folded={this.state.folded} /> } |
Custom Components
There are two components shall be customized.
- FoldIconComponent: to render the Icon of buttons.
- FoldButtonComponent: to render the folding buttons for each Column. With default rendering as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const yourFoldIconComponent = ({ collapsed }) => { //Render your Icon here } const yourFoldButtonComponent = ({ header, collapsed, icon, onClick }) =>{ //Render your button here. } render() { return <FoldableTable FoldIconComponent={this.yourFoldIconComponent} FoldButtonComponent={this.yourFoldButtonComponent} /> } |
Sample
Source Code
you can download the FoldableTable source code and samples here.
The foldableTable HOC had been merge to react-table.org. Hopefully, it will be available soon.
Hope this library is useful to you.