Skip to content
Home » The Foldable HOC for react-table.js.org

The Foldable HOC for react-table.js.org

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

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
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 Header Columns

  • With Normal Columns
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>
}

With Normal Columns

  • The FoldableTable is also fully compatible with existing HOCs, below is with selectTableHOC.

With Normal Columns

State management

If you would like to manage the state of FoldableTable, then add the following codes.

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.
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.

3 thoughts on “The Foldable HOC for react-table.js.org”

  1. I was excited to discover this website. I want to to thank you for your time for this wonderful read!! I definitely liked every part of it and i also have you book-marked to look at new things on your website.

  2. Im very pleased to find this site. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and I have you bookmarked to see new information on your site.

Leave a Reply

Your email address will not be published.