npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

responsive-sortable-react-table

v1.0.3

Published

A reusable table component made for react, both responsive and sortable.

Downloads

4

Readme

responsive-sortable-react-table

Fully Customizable React Table Component

  • Responsive
  • Sortable
  • Pagination

Live Demos

Installation

    npm install responsive-sortable-react-table

Usage

    import Table from 'responsive-sortable-react-table';
    import 'responsive-sortable-react-table/dist/style.css'

    const App = () => {

      const headers = {
        "id": "ID",
        "name": "Name",
        "email": "Email",
        "major": "Major"
      }

      const content = [
        {
            "id": "112018495",
            "name": "Jaquel",
            "email": "[email protected]",
            "major": "Comp. Science"
        },
        {
            "id": "111694728",
            "name": "Joe",
            "email": "[email protected]",
            "major": "Business"
        },
        {
            "id": "114729405",
            "name": "Clara",
            "email": "[email protected]",
            "major": "Criminal Justice"
        }
      ]

      const handleSort = (obj) => {
        content.sort((a, b) => (a[obj.key] > b[obj.key]) ? obj.dir : -1*obj.dir)
      }

      return (
        <Table
          headers={headers}
          content={content}
          breakpoint={768}
          pageSize={4}
          onSort={(obj) => {handleSort(obj)}}
          className="my-table"
          paginationClass="my-pagination"
          />
      )
    }

    export default App;

Options

  • content: Array of Objects | Required
    Array of objects. Table's content array where each object maps field keys to values
  • headers: Object
    Object mapping field keys to strings representing the label that will be displayed on the header of the table
  • className: String
    Class name for the table component. This will be inserted to the table html tag. This is not required as you could just use the class name it comes with by default which is 'responsive-sortable-react-table'
  • breakpoint: Number
    Number in 'px'. Determines breakpoint for the desktop-mobile layout transition of the table
  • onSort: Function
    Function to be called whenever user clicks a header for sorting. This function will be provided an object with the two following fields, 'key' and 'dir'. Please make use of those for your sorting algorithm. 'key' contains the key field the user clicked on while 'dir' contains the direction of the sorting which is either 1 or -1.
  • sortDir: Number 1 | -1
    You can let the Table know which direction of sorting your content originally arrives the Table. Thus, updating sorting icons acordingly
  • sortKey: String
    You can let the Table know on which key is the original sorting based on. Thus, being able to return expected parameters when clicking on a header for sorting
  • paginationClass: String
    Class name applied to pagination container
  • pageSize: Number
    Maximum number of rows per page. This number prop serves as pagination activator, meaning, if it is not provided, there would be no pagination
  • mobilePageSize: Number
    Maximum number of rows per page while on Mobile layout. If not provided, Mobile Layout will share Desktop's page size

Sorting

Sorting feature gets activated when you provide the sorting function prop (onSort). That function will be called whenever user clicks a header for sorting. This function will be provided an object with the two following fields, 'key' and 'dir'. Please make use of those for your sorting algorithm. 'key' contains a string which represents the key field the user clicked on while 'dir' contains the direction of the sorting which is either 1 or -1.

Customization

The easiest way to customize the table is to create another stylesheet to override the default styles.

User can provide custom class names for the table and the pagination container separately. Follow the 'options' sections for specific directions. Note that classNames are not required for customization since both the table and pagination container already have default class names, which are: 'responsive-sortable-react-table' and 'responsive-sortable-react-table-pagination' respectively.

User can further customize the header and body tags by using css tag selectors. For example: assume 'my-table' is the custom class you provided then you could do: '.my-table tbody { }', '.my-table td {}' etc...

Styling Mobile View

The table has a conditional className that only gets applied while in mobile layout. Use '--mobile-view' to style the mobile view of the table

Note that in mobile view each item is enclosed in a tbody tag as oposed to the desktop view where each item is enclosed by a tr tag. Please inspect the html tree for further clarifications

Customizing Specific Cells, Rows or Columns

All cells get applied two classnames by default behavior. Those two are taken from the content and headers props objects user provides to the component:

  • Given the headers prop object (key,value) pairs, each cell will get applied the corresponding key as classname
  • Given the content prop object, the table takes the value of the first (key, value) pair and asssignes it to the cell as className For Example: Following the data from the 'Usage' section, the second cell would have the following classname by default: '112018495 name'

Customizing Sorting and Pagination Icons

  • Color Customization:
  .responsive-sortable-react-table g {
    fill: rgb(255, 255, 255)
  }

  .responsive-sortable-react-table-pagination g {
    fill: rgb(255, 255, 255)
  }
  • Size and Related Properties:
  .responsive-sortable-react-table svg {
    width: 10px;
  }

Feedback - Report an Issue

Follow this link