Compare commits

...
18 Commits
Author SHA1 Message Date
Tanner Linsley 1fef30311c 2.1.3 2016-10-26 00:43:52 -06:00
Tanner Linsley 5351f79293 Better warnings and more column accessor freedom 2016-10-26 00:43:42 -06:00
Tanner Linsley 0bb3f2421e Readme Updates 2016-10-26 00:23:18 -06:00
Tanner Linsley c392c90eb6 Readme Updates 2016-10-26 00:21:24 -06:00
Tanner Linsley fc5cc00fce Readme Updates 2016-10-26 00:20:40 -06:00
Tanner Linsley a8c8211ef8 Readme updates 2016-10-26 00:13:51 -06:00
Tanner Linsley 8bff135119 2.1.2 2016-10-25 23:37:05 -06:00
Tanner Linsley e930d834c6 Fixed index to point to original index, added viewIndex 2016-10-25 23:36:59 -06:00
Tanner Linsley 7700f6a1a0 Standard ignore lib 2016-10-25 17:07:09 -06:00
Tanner Linsley 7a941b880f 2.1.1 2016-10-25 17:01:42 -06:00
Tanner Linsley 2e143e8d27 Babel lib as main input 2016-10-25 17:01:34 -06:00
Tanner Linsley ee2345f43c Slack badge has a slash now 2016-10-24 09:40:14 -06:00
Tanner Linsley e3129a2c80 Readme Updates 2016-10-24 09:30:04 -06:00
Tanner Linsley 1f943ffe25 Added nib to devDeps 2016-10-24 09:11:12 -06:00
Tanner Linsley c854c6914d Updated Readme and Added npm-run-all to devDeps 2016-10-24 09:05:55 -06:00
Tanner Linsley 80cef0d4c6 Use Node 6 in Travis 2016-10-24 09:02:19 -06:00
Tanner Linsley 302eb3d951 Removed React from devDeps 2016-10-24 09:01:23 -06:00
Tanner Linsley 4af3637c0b Added Travis with standard testing 2016-10-24 08:59:22 -06:00
7 changed files with 1452 additions and 133 deletions
+4
View File
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "6"
script: npm test
+139 -99
View File
@@ -3,7 +3,8 @@
# react-table
A fast, lightweight, opinionated table and datagrid built on React
[![react-table on Slack](https://img.shields.io/badge/slack-reactTable-blue.svg)](https://react-table-slack.herokuapp.com/)
[![Build Status](https://travis-ci.org/tannerlinsley/react-table.svg?branch=master)](https://travis-ci.org/tannerlinsley/react-table)
[![react-table on Slack](https://img.shields.io/badge/slack-react--table-blue.svg)](https://react-table-slack.herokuapp.com/)
## Features
@@ -14,21 +15,28 @@ A fast, lightweight, opinionated table and datagrid built on React
- Server-side data
- Minimal design & easily themeable
Why you may **not** want to use this component:
- No support for infinite scrolling. We chose to avoid the complex problems that come with it, and instead provide reliable and predictable pagination.
- You need tables built in something other than traditional HTML table elements.
## [Demo](http://react-table.zabapps.com)
## Table of Contents
- [Installation](#nstallation)
- [Example](#example)
- [Data](#data)
- [Default Props](#default-props)
- [Columns](#columns)
- [Styles](#styles)
- [Header Groups](#header-groups)
- [Server-side Data](#server-side-data)
- [Multi-sort](#multi-sort)
- [Component Overrides](#component-overrides)
<a name="installation"></a>
## Installation
```bash
$ npm install react-table
```
#### Styles
React-table is built to be dropped into existing applications or styled from the ground up, but if you'd like a decent starting point, you can optionally include our default theme `react-table.css`. We think it looks great, honestly :)
## Quick Usage
<a name="example"></a>
## Example
```javascript
import ReactTable from 'react-table'
@@ -63,9 +71,109 @@ const columns = [{
/>
```
## Client-side Data
To use client-side data, simply pass the `data` prop an array. Client-side filtering and pagination is built in, and your table will update gracefully if you change any props.
<a name="data"></a>
## Data
Every React-Table instance requires you to set the `data` prop. To use client-side data, simply pass the `data` prop anything that resembles an array or object. Client-side filtering and pagination is built in, and your table will update gracefully if you change any props. [Server-side data](#server-side-data) is also supported.
<a name="default-props"></a>
## Default Props
These are the default props for the main react component `<ReactTable />`
```javascript
{
// Classes
className: '-striped -highlight', // The most top level className for the component
tableClassName: '', // ClassName for the `table` element
theadClassName: '', // ClassName for the `thead` element
tbodyClassName: '', // ClassName for the `tbody` element
trClassName: '', // ClassName for all `tr` elements
paginationClassName: '' // ClassName for `pagination` element
//
pageSize: 20,
minRows: 0, // Ensure this many rows are always rendered, regardless of rows on page
// Global Column Defaults
column: { // default properties for every column's model
sortable: true,
show: true
},
// Text
previousText: 'Previous',
nextText: 'Next'
}
```
You can easily override the core defaults like so:
```javascript
import { ReactTableDefaults } from 'react-table'
Object.assign(ReactTableDefaults, {
pageSize: 10,
minRows: 3,
// etc...
})
```
Or just define them on the component per-instance
```javascript
<ReactTable
pageSize={10}
minRows={3}
// etc...
/>
```
<a name="columns"></a>
## Columns
Every React-Table instance requires a `columns` prop, which is an array of objects containing the following properties
```javascript
[{
// Required
header: 'Header Name' or JSX eg. ({data, column}) => <div>Header Name</div>,
accessor: 'propertyName' or Accessor eg. (row) => row.propertyName,
// A unique ID is required if the accessor is not a string or if you would like to override the column name used in server-side calls
id: 'myProperty',
// Optional
className: '', // Set the classname of the `th/td` element of the column
innerClassName: '', // Set the classname of the `.th-inner/.td-inner` element of the column
columns: [...] // See Header Groups section below
render: JSX eg. ({row, value, index, viewIndex}) => <span>{value}</span>, // Provide a JSX element or stateless function to render whatever you want as the column's cell with access to the entire row
sortable: true,
sort: 'asc' or 'desc',
show: true,
width: Number, // Locks the column width to this amount
minWidth: Number // Allows the column to flex above this minimum amount
}]
```
<a name="styles"></a>
## Styles
React-table is built to be dropped into existing applications or styled from the ground up, but if you'd like a decent starting point, you can optionally include our default theme `react-table.css`. We think it looks great, honestly :)
<a name="header-groups"></a>
## Header Groups
To group columns with another header column, just nest your columns in a header column like so:
```javascript
const columns = [{
header: 'Favorites',
columns: [{
header: 'Color',
accessor: 'favorites.color'
}, {
header: 'Food',
accessor: 'favorites.food'
} {
header: 'Actor',
accessor: 'favorites.actor'
}]
}]
```
<a name="server-side-data"></a>
## Server-side Data
If you want to handle pagination, and sorting on the server, `react-table` makes it easy on you. Instead of passing the `data` prop an array, you provide a function instead.
@@ -108,107 +216,39 @@ This function will be called on mount, pagination events, and sorting events. It
/>
```
<a name="multi-sort"></a>
## Multi-Sort
When clicking on a column header, hold shift to multi-sort! You can toggle `ascending` `descending` and `none` for multi-sort columns. Clicking on a header without holding shift will clear the multi-sort and replace it with the single sort of that column. It's quite handy!
## Default Props
```javascript
{
// Classes
className: '-striped -highlight', // The most top level className for the component
tableClassName: '', // ClassName for the `table` element
theadClassName: '', // ClassName for the `thead` element
tbodyClassName: '', // ClassName for the `tbody` element
trClassName: '', // ClassName for all `tr` elements
paginationClassName: '' // ClassName for `pagination` element
//
pageSize: 20,
minRows: 0, // Ensure this many rows are always rendered, regardless of rows on page
// Global Column Defaults
column: { // default properties for every column's model
sortable: true,
show: true
},
// Text
previousText: 'Previous',
nextText: 'Next',
// Component Overrides
tableComponent: null,
theadComponent: null,
tbodyComponent: null,
trComponent: null,
thComponent: null,
tdComponent: null,
paginationComponent: null,
previousComponent: null,
nextComponent: null,
loadingComponent: null
}
```
You can easily override the core defaults like so:
<a name="component-overrides"></a>
## Component Overrides
Though we wouldn't suggest it, `react-table` has the ability to change the core componentry it used to render it's table. You can do so by assigning a react component to it's corresponding global prop, or on a one-off basis like so:
```javascript
// Change the global default
import { ReactTableDefaults } from 'react-table'
Object.assign(ReactTableDefaults, {
pageSize: 10,
minRows: 3,
// etc...
tableComponent: Component,
theadComponent: Component,
tbodyComponent: Component,
trComponent: Component,
thComponent: Component,
tdComponent: Component,
paginationComponent: Component,
previousComponent: Component,
nextComponent: Component,
loadingComponent: Component
})
```
Or just define them on the component
```javascript
// Or change per instance
<ReactTable
pageSize={10}
minRows={3}
tableComponent={Component},
theadComponent={Component},
// etc...
})
/>
```
## Column Props
If you choose to change the core components React-Table uses to render, you must make sure your components support and utilized all of the neeeded features for that component to work properly. For a broad reference on how to do this, investigate [the source](https://github.com/tannerlinsley/react-table/blob/master/src/index.js) for the component you wish to replace.
```javascript
[{
// Required
header: 'Header Name' or JSX eg. ({data, column}) => <div>Header Name</div>,
accessor: 'propertyName' or Accessor eg. (row) => row.propertyName,
// A unique ID is required if the accessor is not a string or if you would like to override the column name used in server-side calls
id: 'myProperty',
// Optional
className: '', // Set the classname of the `th/td` element of the column
innerClassName: '', // Set the classname of the `.th-inner/.td-inner` element of the column
columns: [...] // See Header Groups section below
render: JSX eg. ({row, value, index}) => <span>{value}</span>, // Provide a JSX element or stateless function to render whatever you want as the column's cell with access to the entire row
sortable: true,
sort: 'asc' or 'desc',
show: true,
width: Number, // Locks the column width to this amount
minWidth: Number // Allows the column to flex above this minimum amount
}]
```
## Header Groups
To group columns with another header column, just nest your columns in a header column like so:
```javascript
const columns = [{
header: 'Favorites',
columns: [{
header: 'Color',
accessor: 'favorites.color'
}, {
header: 'Food',
accessor: 'favorites.food'
} {
header: 'Actor',
accessor: 'favorites.actor'
}]
}]
```
## Contributing
To suggest a feature, create an issue if it does not already exist.
+635 -15
View File
File diff suppressed because one or more lines are too long
+643
View File
File diff suppressed because one or more lines are too long
+14 -7
View File
@@ -1,6 +1,6 @@
{
"name": "react-table",
"version": "2.1.0",
"version": "2.1.3",
"description": "A fast, lightweight, opinionated table and datagrid built on React",
"license": "MIT",
"homepage": "https://github.com/tannerlinsley/react-table#readme",
@@ -14,17 +14,19 @@
"react-table",
"datagrid"
],
"main": "react-table.js",
"main": "lib/index.js",
"scripts": {
"build:js": "rm -rf react-table.js && browserify src/index.js -s reactTable -u react -t babelify -g uglifyify -o react-table.js",
"build:node": "babel src --out-dir lib --source-maps inline",
"build:css": "rm -rf react-table.css && stylus src/index.styl --use ./node_modules/nib/lib/nib.js --compress -o react-table.css",
"watch": "onchange 'src/**' -i -- npm-run-all build:*",
"test": "standard",
"prepublish": "npm-run-all build:*"
"umd": "rm -rf react-table.js && browserify lib/index.js -s reactTable -x react -t babelify -g uglifyify -o react-table.js",
"prepublish": "npm-run-all build:* && npm run umd"
},
"dependencies": {
"classnames": "^2.2.5",
"lodash": "^4.16.4",
"classnames": "^2.2.5"
},
"peerDependencies": {
"react": "^15.3.1"
},
"devDependencies": {
@@ -35,17 +37,22 @@
"babel-preset-stage-2": "6.13.0",
"babelify": "^7.3.0",
"browserify": "13.1.0",
"nib": "^1.1.2",
"npm-run-all": "^3.1.1",
"onchange": "^3.0.2",
"react": "15.3.1",
"standard": "8.0.0",
"stylus": "^0.54.5",
"uglifyify": "3.0.3"
},
"browserify-shim": {
"react": "global:React"
},
"standard": {
"parser": "babel-eslint",
"ignore": [
"node_modules",
"dist",
"lib",
"example",
"react-table.js"
]
+4 -4
View File
File diff suppressed because one or more lines are too long
+13 -8
View File
@@ -96,17 +96,20 @@ export default React.createClass({
}
const makeDecoratedColumn = (column) => {
const dcol = Object.assign({}, this.props.column, column)
if (typeof dcol.accessor === 'string') {
dcol.id = dcol.id || dcol.accessor
const accessorString = dcol.accessor
dcol.accessor = row => _.get(row, accessorString)
return dcol
}
if (!dcol.id) {
console.warn('No column ID found for column: ', dcol)
}
if (!dcol.accessor) {
console.warn('No column accessor found for column: ', dcol)
if (dcol.accessor && !dcol.id) {
console.warn(dcol)
throw new Error('A column id is required if using a non-string accessor for column above.')
}
dcol.accessor = d => undefined
return dcol
}
@@ -196,9 +199,10 @@ export default React.createClass({
}
},
accessData (data) {
return data.map((d) => {
return data.map((d, i) => {
const row = {
__original: d
__original: d,
__index: i
}
this.decoratedColumns.forEach(column => {
row[column.id] = column.accessor(d)
@@ -337,7 +341,8 @@ export default React.createClass({
<Cell
value={row[column.id]}
row={row.__original}
index={i}
index={row.__index}
viewIndex={i}
/>
) : typeof Cell !== 'undefined' ? Cell
: row[column.id]}