Skip to content

Commit 3d69ac5

Browse files
author
Corjen Moll
committed
feat: Initial commit
0 parents  commit 3d69ac5

23 files changed

+9310
-0
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": [["env"], "react"],
3+
"plugins": ["transform-class-properties"]
4+
}

.eslintrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
parser: 'babel-eslint',
3+
extends: ['standard', 'prettier'],
4+
plugins: ['react', 'import'],
5+
env: {
6+
browser: true,
7+
jest: true
8+
},
9+
rules: {
10+
'react/jsx-uses-react': 'error',
11+
'react/jsx-uses-vars': 'error'
12+
}
13+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
coverage

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
src
2+
.babelrc
3+
.eslintrc.js
4+
.prettierrc

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"printWidth": 140,
4+
"singleQuote": true
5+
}

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const config = {}
2+
config.setupFiles = ['<rootDir>/other/setup-tests.js']
3+
4+
module.exports = config

other/raf-polyfill.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-disable */
2+
/**
3+
* requestAnimationFrame polyfill from https://gist.github.com/paulirish/1579671
4+
* http://paulirish.com/2011/requestanimationframe-for-smart-animating/
5+
* http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
6+
* requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
7+
* MIT license
8+
*/
9+
10+
var lastTime = 0
11+
var vendors = ['ms', 'moz', 'webkit', 'o']
12+
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
13+
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']
14+
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']
15+
}
16+
17+
if (!window.requestAnimationFrame) {
18+
window.requestAnimationFrame = function(callback, element) {
19+
var currTime = new Date().getTime()
20+
var timeToCall = Math.max(0, 16 - (currTime - lastTime))
21+
var id = window.setTimeout(function() {
22+
// eslint-disable-next-line consumerweb/no-callback-literal
23+
callback(currTime + timeToCall)
24+
}, timeToCall)
25+
lastTime = currTime + timeToCall
26+
return id
27+
}
28+
global.requestAnimationFrame = window.requestAnimationFrame
29+
}
30+
31+
if (!window.cancelAnimationFrame) {
32+
window.cancelAnimationFrame = function(id) {
33+
clearTimeout(id)
34+
}
35+
}

other/setup-tests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import './raf-polyfill'
2+
import Enzyme from 'enzyme'
3+
import Adapter from 'enzyme-adapter-react-16'
4+
5+
Enzyme.configure({ adapter: new Adapter() })

0 commit comments

Comments
 (0)