|
| 1 | +# React data sort |
| 2 | + |
| 3 | +A simple react component that helps you sort and paginate a list of data. |
| 4 | + |
| 5 | +## Badges here |
| 6 | + |
| 7 | +# The problem |
| 8 | + |
| 9 | +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 |
| 10 | +styling and a simple API. |
| 11 | + |
| 12 | +# This solution |
| 13 | + |
| 14 | +Components with a (render prop)[https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce] like [Downshift](downshift) and React Router's |
| 15 | +[Route](https://reacttraining.com/react-router/web/api/Route) are gaining popularity. The render prop pattern gives you maximum flexibility |
| 16 | +in the way you render and style your components because the render prop itself doens't render anything. |
| 17 | + |
| 18 | +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 |
| 19 | +styling. I couldn't find it, so I decided to build something myself. This is my first open source React Component, any feedback or |
| 20 | +contributions are very welcome! |
| 21 | + |
| 22 | +# Table of Contents |
| 23 | + |
| 24 | +* [Installation](#installation) |
| 25 | +* [Usage](#usage) |
| 26 | +* [Props](#props) |
| 27 | +* [Render prop function](#render-prop-function) |
| 28 | +* [Examples](#examples) |
| 29 | +* [Todo](#todo) |
| 30 | +* [License](#license) |
| 31 | + |
| 32 | +# [Installation](#installation) |
| 33 | + |
| 34 | +This modules is distributed via [npm](https://www.npmjs.com/package/react-data-sort). You can install it with npm: |
| 35 | + |
| 36 | +``` |
| 37 | +npm install --save react-data-sort |
| 38 | +``` |
| 39 | + |
| 40 | +This package has `react` and `prop-types` as [peerDependencies](https://nodejs.org/en/blog/npm/peer-dependencies/). Make sure to install |
| 41 | +them if you haven't. |
| 42 | + |
| 43 | +# [Usage](usage) |
| 44 | + |
| 45 | +```javascript |
| 46 | +import Datasort from 'react-data-sort' |
| 47 | + |
| 48 | +const tableData = [{ id: 1, name: 'b', id: 2, name: 'c', id: 3, name: 'a' }] |
| 49 | + |
| 50 | +function App() { |
| 51 | + return ( |
| 52 | + <Datasort |
| 53 | + data={tableData} |
| 54 | + paginate |
| 55 | + render={({ data }) => ( |
| 56 | + <table> |
| 57 | + <thead> |
| 58 | + <tr> |
| 59 | + <td>Id</td> |
| 60 | + <td>Name</td> |
| 61 | + </tr> |
| 62 | + </thead> |
| 63 | + <tbody> |
| 64 | + {data.map(({ id, name }) => ( |
| 65 | + <tr key={id}> |
| 66 | + <td>{id}</td> |
| 67 | + <td>{name}</td> |
| 68 | + </tr> |
| 69 | + ))} |
| 70 | + </tbody> |
| 71 | + </table> |
| 72 | + )} |
| 73 | + /> |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +export default App |
| 78 | +``` |
| 79 | + |
| 80 | +By default, it will return the data in the same order that you've given it. The above code will result in this table: |
| 81 | + |
| 82 | +| ID | Name | |
| 83 | +| --- | ---- | |
| 84 | +| 1 | b | |
| 85 | +| 2 | c | |
| 86 | +| 3 | a | |
| 87 | + |
| 88 | +# [Props](#props) |
| 89 | + |
| 90 | +## data |
| 91 | + |
| 92 | +> `array` | defaults to `[]` An array of data that you want to render |
| 93 | +
|
| 94 | +## defaultDirection |
| 95 | + |
| 96 | +> `string` | defaults to `desc` | can be `asc' or`desc` This is the direction in which the data is sorted by default. |
| 97 | +
|
| 98 | +## defaultSortBy |
| 99 | + |
| 100 | +> `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. |
| 101 | +
|
| 102 | +## itemsPerPage |
| 103 | + |
| 104 | +> `number` | defaults to `10` The number of items to show on one page. Only works of `paginate` prop is `true`. |
| 105 | +
|
| 106 | +## paginate |
| 107 | + |
| 108 | +> `boolean` | defaults to `false` |
| 109 | +
|
| 110 | +Enables pagination functionality and slices your data to the current page. |
| 111 | + |
| 112 | +# [Controlled vs Uncontrolled](#controlled-vs-uncontrolled) |
| 113 | + |
| 114 | +The internal state manages `direction`, `sortBy` and `activePage`. In some cases, you want to control that state outside the component, for |
| 115 | +example if you use `redux` or `mobx` to manage your state. You can set `direction`, `sortBy` and `active` as props, thus making that part of |
| 116 | +the state 'controlled'. |
| 117 | + |
| 118 | +# [Render Prop Function](#render-prop-function) |
| 119 | + |
| 120 | +The render prop expects a function and doesn't render anything. It's argument is an object though, with the internal state and a couple of |
| 121 | +actions. |
| 122 | + |
| 123 | +```javascript |
| 124 | +<Datasort |
| 125 | + data={tableData} |
| 126 | + paginate |
| 127 | + render={({ |
| 128 | + data, |
| 129 | + activePage, |
| 130 | + pages, |
| 131 | + sortBy, |
| 132 | + // etc.. |
| 133 | + }) => ( |
| 134 | + // Render jsx stuff here |
| 135 | + )} |
| 136 | + /> |
| 137 | +``` |
| 138 | + |
| 139 | +## actions |
| 140 | + |
| 141 | +You can change the internal state with these actions. |
| 142 | + |
| 143 | +| property | type | description | |
| 144 | +| --------------- | ----------------------------- | ------------------------------------------------------ | |
| 145 | +| toggleDirection | `function()` | toggle the direction from `asc` to `desc` or viceversa | |
| 146 | +| setDirection | `function(direction: string)` | set the direction to `asc` or `desc` | |
| 147 | +| prevPage | `function()` | go to the previous page (only if `paginate` is true) | |
| 148 | +| nextPage | `function()` | go to the next page (only if `paginate` is true) | |
| 149 | +| gotToPage | `function(index: number)` | go to a specific page | |
| 150 | +| setSortBy | `function(key: string)` | set the key to sort the data by | |
| 151 | +| reset | `function()` | reset to the initial state | |
| 152 | + |
| 153 | +## state |
| 154 | + |
| 155 | +These are the internal state values |
| 156 | + |
| 157 | +| property | type | description | |
| 158 | +| ---------- | ----------------- | ------------------------------------------------- | |
| 159 | +| activePage | `number` | the current active page | |
| 160 | +| pages | `number` | the total amount of pages The current active page | |
| 161 | +| sortBy | `string` / `null` | the current key where the data is sorted by | |
| 162 | +| direction | `string` | the current direction where the data is sorted by | |
| 163 | + |
| 164 | +# [Examples](#examples) |
| 165 | + |
| 166 | +# [TODO](#todo) |
| 167 | + |
| 168 | +* UMD build |
| 169 | +* Search/filter |
| 170 | +* Change the name to something fancier? |
| 171 | + |
| 172 | +# [License](#license) |
| 173 | + |
| 174 | +MIT |
0 commit comments