1.2.1 • Published 5 years ago

react-data-sort v1.2.1

Weekly downloads
388
License
MIT
Repository
github
Last release
5 years ago

React data sort

A simple react component that helps you sort and paginate a list of data.

GitHub license Build Status npm.io npm.io

The problem

You want to display a custom set of data in a table or list and want to be able to sort and/or paginate it. You also want to have freedom of styling and a simple API.

This solution

Components with a render prop like Downshift and React Router's Route are gaining popularity. The render prop pattern gives you maximum flexibility in the way you render and style your components because the render prop itself doens't render anything.

I've made this component because I was looking for a React table component that would give me as much control as possible over rendering and styling. I couldn't find it, so I decided to build something myself. This is my first open source React Component, any feedback or contributions are very welcome!

Note: If you need to render a really large dataset where performance is vital, something like react-virtualized is probably a better fit.

Table of Contents

Installation

This modules is distributed via npm. You can install it with npm:

npm install --save react-data-sort

This package has react and prop-types as peerDependencies. Make sure to install them if you haven't.

Usage

import Datasort from 'react-data-sort'

const tableData = [{ id: 1, name: 'b', id: 2, name: 'c', id: 3, name: 'a' }]

function App() {
  return (
    <Datasort
      data={tableData}
      paginate
      render={({ data }) => (
        <table>
          <thead>
            <tr>
              <td>Id</td>
              <td>Name</td>
            </tr>
          </thead>
          <tbody>
            {data.map(({ id, name }) => (
              <tr key={id}>
                <td>{id}</td>
                <td>{name}</td>
              </tr>
            ))}
          </tbody>
        </table>
      )}
    />
  )
}

export default App

By default, it will return the data in the same order that you've given it. The above code will result in this table:

IDName
1b
2c
3a

Props

data

array | defaults to [] An array of data that you want to render

defaultDirection

string | defaults to desc | can be asc or desc This is the direction in which the data is sorted by default.

defaultSortBy

string | defaults to null | can be null or an object key in your data array. This is the key by which your data is sorted.

itemsPerPage

number | defaults to 10 The number of items to show on one page. Only works if paginate prop is true.

paginate

boolean | defaults to false

Enables pagination functionality and slices your data to the current page.

searchInKeys

array | defaults to the keys of the first item in data

Sets the keys to search in

Controlled vs Uncontrolled

The internal state manages direction, sortBy, searchQuery and activePage. In some cases, you want to control that state outside the component, for example if you use redux or mobx to manage your state. You can set direction, sortBy, searchQuery and active as props, thus making that part of the state 'controlled'.

Render Prop Function

The render prop expects a function and doesn't render anything. It's argument is an object, with the internal state and a couple of actions.

<Datasort
      data={tableData}
      paginate
      render={({
         data,
         activePage,
         pages,
         sortBy,
         searchQuery,
         // etc..
        }) => (
        // Render jsx stuff here
      )}
    />

actions

You can change the internal state with these actions.

propertytypedescription
toggleDirectionfunction()toggle the direction from asc to desc or viceversa
setDirectionfunction(direction: string)set the direction to asc or desc
prevPagefunction()go to the previous page (only if paginate is true)
nextPagefunction()go to the next page (only if paginate is true)
goToPagefunction(index: number)go to a specific page
setSortByfunction(key: string)set the key to sort the data by
resetfunction()reset to the initial state
searchfunction(query: string)search for a query in given data

state

These are the internal state values

propertytypedescription
activePagenumberthe current active page
pagesnumberthe total amount of pages The current active page
sortBystring / nullthe current key where the data is sorted by
directionstringthe current direction where the data is sorted by
searchQuerystringthe current search query

Examples

TODO

  • UMD build
  • Add helpers for aria labels
  • Change the name to something fancier?

License

MIT

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.0

6 years ago

1.0.0

6 years ago