fix(multiple): sub-components example, minor refactors

Fixes #1423
This commit is contained in:
tannerlinsley
2019-08-02 07:42:42 -06:00
parent 39c2b152f6
commit 512790bc1c
30 changed files with 11881 additions and 114 deletions
+323 -30
View File
@@ -401,6 +401,15 @@ The following additional properties are available on every `row` object returned
- This function is used to resolve any props needed for this row.
- You can use the `getRowProps` hook to extend its functionality.
- Custom props may be passed. **NOTE: Custom props will override built-in table props, so be careful!**
- `index: Int`
- The index of the original row in the `data` array that was passed to `useTable`. If this row is a subRow, it is the original index within the parent row's subRows array
- `original: Object`
- The original row object from the `data` array that was used to materialize this row.
- `path: Array<string>`
- This array is the sequential path of indices one could use to navigate to it, eg. a row path of `[3, 1, 0]` would mean that it is the **first** subRow of a parent that is the **second** subRow of a parent that is the **fourth** row in the original `data` array.
- This array is used with plugin hooks like `useExpanded` and `useGroupBy` to compute expanded states for individual rows.
- `subRows: Array<Row>`
- If subRows were detect on the original data object, this will be an array of those materialized row objects.
### Cell Properties
@@ -419,6 +428,7 @@ The following additional properties are available on every `Cell` object returne
- You can use the `getCellProps` hook to extend its functionality.
- Custom props may be passed. **NOTE: Custom props will override built-in table props, so be careful!**
- `render: Function(type: String | Function | Component, ?props)`
- **Required**
- This function is used to render content in context of a cell.
- If `type` is a string, will render using the `column[type]` renderer. React Table ships with a default `Cell` renderer. Other renderers like `Aggregated` are available via hooks like `useFilters`.
- If a function or component is passed instead of a string, it will be be passed the table instance and cell model as props and is expected to return any valid JSX.
@@ -1058,49 +1068,169 @@ function App() {
- Plugin Hook
- Optional
`useExpanded` is the hook that implements **row expanding**. It is most often used with `useGroupBy` to expand grouped rows, but is not limited to that use-case. It supports expanding rows both via internal table state and also via a hard-coded key on the raw row model.
`useExpanded` is the hook that implements **row expanding**. It is most often used with `useGroupBy` to expand grouped rows or on its own with nested `subRows` in tree-li,ke `data` sets, but is not limited to these use-cases. It supports expanding rows both via internal table state and also via a hard-coded key on the raw row model.
### Table Options
The following options are supported via the main options object passed to `useTable(options)`
- `state[0].expanded: Object<[pathIndex]: Boolean | ExpandedStateObject>`
- Optional
- Must be **memoized**
- An nested object of expanded paths.
- A `pathIndex` can be set as the key and its value set to `true` to expand that row's subRows into view. For example, if `{ '3': true }` was passed as the `expanded` state, the **4th row in the original data array** would be expanded.
- For nested expansion, you may **use another object** instead of a Boolean to expand sub rows. For example, if `{ '3': { '5' : true }}` was passed as the `expanded` state, then the **6th subRow of the 4th row and the 4th row of the original data array** would be expanded.
- This information is stored in state since the table is allowed to manipulate the filter through user interaction.
- `subRowsKey: String`
- Required
- Defaults to `subRows`
- React Table will use this key when materializing the final row object. It also uses this key to infer sub-rows from the raw data.
- See [Grouping and Aggregation](#grouping-and-aggregation) for more information
- `paginateSubRows: Bool`
- Defaults to `true`
- Optional
- See the [useTable hook](#table-options) for more details
- `nestExpandedRows: Bool`
- Optional
- Defaults to `false`
- If set to `false`, expanded rows will not be paginated. Thus, any expanded subrows would potentially increase the size of any given page by the amount of total expanded subrows on the page.
- `manualExpandedKey: String`
- Optional
- Defaults to `expanded`
- This string is used as the key to detect manual expanded state on any given row. For example, if a raw data row like `{ name: 'Tanner Linsley', friends: [...], expanded: true}` was detected, it would be forcibly expanded, regardless of state.
- This string is used as the key to detect manual expanded state on any given row. For example, if a raw data row like `{ name: 'Tanner Linsley', friends: [...], expanded: true}` was detected, it would always be expanded, regardless of state.
### Instance Variables
### Instance Properties
The following values are provided to the table `instance`:
The following properties are available on the table instance returned from `useTable`
- `rows: Array<Row>`
- An array of **sorted** rows.
### Row Properties
The following additional properties are available on every `row` object returned by the table instance.
- `isExpanded: Bool`
- If `true`, this row is in an expanded state.
- `toggleExpanded: Function(?isExpanded: Bool) => void`
- This function will toggle the expanded state of a row between `true` and `false` or, if an `isExpanded` boolean is passed to the function, it will be set as the new `isExpanded` value.
- Rows with a hard-coded `manualExpandedKey` (defaults to `expanded`) set to `true` are not affected by this function or the internal expanded state.
### Example
```js
const state = useTableState({ expanded: { '3': true, '5': { '2': true } } })
function Table({ columns: userColumns, data }) {
const {
getTableProps,
headerGroups,
rows,
prepareRow,
state: [{ expanded }],
} = useTable(
{
columns: userColumns,
data,
},
useExpanded // Use the useExpanded plugin hook
)
const { rows } = useTable(
{
// state[0].sortBy === { '3': true, '5': { '2': true } }
state,
},
useExpanded
)
return (
<>
<pre>
<code>{JSON.stringify({ expanded }, null, 2)}</code>
</pre>
<table {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody>
{rows.map(
(row, i) =>
prepareRow(row) || (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
)
})}
</tr>
)
)}
</tbody>
</table>
<br />
<div>Showing the first 20 results of {rows.length} rows</div>
</>
)
}
function App() {
const columns = React.useMemo(
() => [
{
// Build our expander column
Header: () => null, // No header, please
id: 'expander', // Make sure it has an ID
Cell: ({ row }) =>
// Use the row.canExpand and row.getExpandedToggleProps prop getter
// to build the toggle for expanding a row
row.canExpand ? (
<span
{...row.getExpandedToggleProps({
style: {
// We can even use the row.depth property
// and paddingLeft to indicate the depth
// of the row
paddingLeft: `${row.depth * 2}rem`,
},
})}
>
{row.isExpanded ? '👇' : '👉'}
</span>
) : null,
},
{
Header: 'Name',
columns: [
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
],
},
{
Header: 'Info',
columns: [
{
Header: 'Age',
accessor: 'age',
},
{
Header: 'Visits',
accessor: 'visits',
},
{
Header: 'Status',
accessor: 'status',
},
{
Header: 'Profile Progress',
accessor: 'progress',
},
],
},
],
[]
)
const data = React.useMemo(() => makeData(5, 5, 5), [])
return <Table columns={columns} data={data} />
}
```
## `usePagination`
@@ -1173,18 +1303,179 @@ The following values are provided to the table `instance`:
### Example
```js
const state = useTableState({ pageSize: 20, pageIndex: 1 })
function Table({ columns, data }) {
// Use the state and functions returned from useTable to build your UI
const {
getTableProps,
headerGroups,
prepareRow,
page, // Instead of using 'rows', we'll use page,
// which has only the rows for the active page
const { rows } = useTable(
{
// state[0] === { pageSize: 20, pageIndex: 1 }
state,
},
usePagination
)
// The rest of these things are super handy, too ;)
canPreviousPage,
canNextPage,
pageOptions,
pageCount,
gotoPage,
nextPage,
previousPage,
setPageSize,
state: [{ pageIndex, pageSize }],
} = useTable(
{
columns,
data,
},
usePagination
)
// Render the UI for your table
return (
<>
<pre>
<code>
{JSON.stringify(
{
pageIndex,
pageSize,
pageCount,
canNextPage,
canPreviousPage,
},
null,
2
)}
</code>
</pre>
<table {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody>
{page.map(
(row, i) =>
prepareRow(row) || (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
)
})}
</tr>
)
)}
</tbody>
</table>
{/*
Pagination can be built however you'd like.
This is just a very basic UI implementation:
*/}
<div className="pagination">
<button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
{'<<'}
</button>{' '}
<button onClick={() => previousPage()} disabled={!canPreviousPage}>
{'<'}
</button>{' '}
<button onClick={() => nextPage()} disabled={!canNextPage}>
{'>'}
</button>{' '}
<button onClick={() => gotoPage(pageCount - 1)} disabled={!canNextPage}>
{'>>'}
</button>{' '}
<span>
Page{' '}
<strong>
{pageIndex + 1} of {pageOptions.length}
</strong>{' '}
</span>
<span>
| Go to page:{' '}
<input
type="number"
defaultValue={pageIndex + 1}
onChange={e => {
const page = e.target.value ? Number(e.target.value) - 1 : 0
gotoPage(page)
}}
style={{ width: '100px' }}
/>
</span> <select
value={pageSize}
onChange={e => {
setPageSize(Number(e.target.value))
}}
>
{[10, 20, 30, 40, 50].map(pageSize => (
<option key={pageSize} value={pageSize}>
Show {pageSize}
</option>
))}
</select>
</div>
</>
)
}
function App() {
const columns = React.useMemo(
() => [
{
Header: 'Name',
columns: [
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
],
},
{
Header: 'Info',
columns: [
{
Header: 'Age',
accessor: 'age',
},
{
Header: 'Visits',
accessor: 'visits',
},
{
Header: 'Status',
accessor: 'status',
},
{
Header: 'Profile Progress',
accessor: 'progress',
},
],
},
],
[]
)
const data = React.useMemo(() => makeData(100000), [])
return (
<Styles>
<Table columns={columns} data={data} />
</Styles>
)
}
```
## `useTokenPagination`
## `useTokenPagination (Coming Soon)`
- Plugin Hook
- Optional
@@ -1209,7 +1500,7 @@ Some common use cases for this hook are:
- Disallowing specific states via a custom state reducer
- Enabling parent/unrelated components to manipulate the table state
### Table Options
### Hook Options
The following options are supported via the main options object passed to `useTable(options)`
@@ -1232,14 +1523,16 @@ The following options are supported via the main options object passed to `useTa
- Defaults to `React.useState`
- This function, if defined will be used as the state hook internally instead of the default `React.useState`. This can be useful for implementing custom state storage hooks like useLocalStorage, etc.
### Instance Variables
### Output
- `tableStateTuple: [tableState, setTableState]`
- Similar in structure to the result of `React.useState`
- **Memoized**. This tuple array will not change between renders unless state or `useTableState` options change.
- **Memoized** - This tuple array will not change between renders unless the state or `useTableState` options change.
- `tableState: Object`
- **Memoized** - This object reference will not change unless the state changes.
- This is the final state object of the table, which is the product of the `initialState`, `overrides` and the `reducer` options (if applicable)
- `setTableState: Function(updater, type) => void`
- **Memoized** - This function reference will not change unless the internal state `reducer` is changed
- This function is used both internally by React Table, and optionally by you (the developer) to update the table state programmatically.
- `updater: Function`
- This function signature is **almost** (see next point) identical to the functional API exposed by `React.setState`. It is passed the previous state and is expected to return a new version of the state.
+30 -39
View File
@@ -33,20 +33,19 @@ const Styles = styled.div`
}
`
function Table({ columns: userColumns, data, SubComponent }) {
function Table({ columns: userColumns, data }) {
const {
getTableProps,
headerGroups,
rows,
prepareRow,
columns,
state: [{ expanded }],
} = useTable(
{
columns: userColumns,
data,
},
useExpanded
useExpanded // Use the useExpanded plugin hook
)
return (
@@ -68,20 +67,13 @@ function Table({ columns: userColumns, data, SubComponent }) {
{rows.map(
(row, i) =>
prepareRow(row) || (
<>
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
)
})}
</tr>
{!row.subRows.length && row.isExpanded ? (
<tr>
<td colSpan={columns.length}>{SubComponent({ row })}</td>
</tr>
) : null}
</>
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
)
})}
</tr>
)
)}
</tbody>
@@ -96,19 +88,26 @@ function App() {
const columns = React.useMemo(
() => [
{
Header: () => null,
id: 'expander',
Cell: ({ row }) => (
<span
style={{
cursor: 'pointer',
paddingLeft: `${row.depth * 2}rem`,
}}
onClick={() => row.toggleExpanded()}
>
{row.isExpanded ? '👇' : '👉'}
</span>
),
// Build our expander column
Header: () => null, // No header, please
id: 'expander', // Make sure it has an ID
Cell: ({ row }) =>
// Use the row.canExpand and row.getExpandedToggleProps prop getter
// to build the toggle for expanding a row
row.canExpand ? (
<span
{...row.getExpandedToggleProps({
style: {
// We can even use the row.depth property
// and paddingLeft to indicate the depth
// of the row
paddingLeft: `${row.depth * 2}rem`,
},
})}
>
{row.isExpanded ? '👇' : '👉'}
</span>
) : null,
},
{
Header: 'Name',
@@ -152,15 +151,7 @@ function App() {
return (
<Styles>
<Table
columns={columns}
data={data}
SubComponent={({ row }) => (
<pre>
<code>{JSON.stringify({ values: row.values }, null, 2)}</code>
</pre>
)}
/>
<Table columns={columns} data={data} />
</Styles>
)
}
+3 -6
View File
@@ -53,6 +53,8 @@ function Table({ columns, data }) {
// it at 20 for this use case
const firstPageRows = rows.slice(0, 100)
console.log(firstPageRows)
return (
<>
<pre>
@@ -102,12 +104,7 @@ function Table({ columns, data }) {
{cell.grouped ? (
// If it's a grouped cell, add an expander and row count
<>
<span
style={{
cursor: 'pointer',
}}
onClick={() => row.toggleExpanded()}
>
<span {...row.getExpandedToggleProps()}>
{row.isExpanded ? '👇' : '👉'}
</span>{' '}
{cell.render('Cell')} ({row.subRows.length})
@@ -0,0 +1,4 @@
{
"presets": ["react-app"],
"plugins": ["styled-components"]
}
+1
View File
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
@@ -0,0 +1,7 @@
{
"extends": ["react-app", "prettier"],
"rules": {
// "eqeqeq": 0,
// "jsx-a11y/anchor-is-valid": 0
}
}
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
@@ -0,0 +1,29 @@
const path = require('path')
const resolveFrom = require('resolve-from')
const fixLinkedDependencies = config => {
config.resolve = {
...config.resolve,
alias: {
...config.resolve.alias,
react$: resolveFrom(path.resolve('node_modules'), 'react'),
'react-dom$': resolveFrom(path.resolve('node_modules'), 'react-dom'),
},
}
return config
}
const includeSrcDirectory = config => {
config.resolve = {
...config.resolve,
modules: [path.resolve('src'), ...config.resolve.modules],
}
return config
}
module.exports = [
['use-babel-config', '.babelrc'],
['use-eslint-config', '.eslintrc'],
fixLinkedDependencies,
// includeSrcDirectory,
]
@@ -0,0 +1,6 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app) and Rescripts.
You can:
- [Open this example in a new CodeSandbox](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/basic)
- `yarn` and `yarn start` to run and edit the example
@@ -0,0 +1,35 @@
{
"private": true,
"scripts": {
"start": "rescripts start",
"build": "rescripts build",
"test": "rescripts test",
"eject": "rescripts eject"
},
"dependencies": {
"namor": "^1.1.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"react-table": "next",
"styled-components": "^4.3.2"
},
"devDependencies": {
"@rescripts/cli": "^0.0.11",
"@rescripts/rescript-use-babel-config": "^0.0.8",
"@rescripts/rescript-use-eslint-config": "^0.0.9",
"babel-eslint": "10.0.1"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
@@ -0,0 +1,198 @@
import React from 'react'
import styled from 'styled-components'
import { useTable, useExpanded } from 'react-table'
import makeData from './makeData'
const Styles = styled.div`
padding: 1rem;
table {
border-spacing: 0;
border: 1px solid black;
tr {
:last-child {
td {
border-bottom: 0;
}
}
}
th,
td {
margin: 0;
padding: 0.5rem;
border-bottom: 1px solid black;
border-right: 1px solid black;
:last-child {
border-right: 0;
}
}
}
`
// A simple way to support a renderRowSubComponent is to make a render prop
// This is NOT part of the React Table API, it's merely a rendering
// option we are creating for ourselves in our table renderer
function Table({ columns: userColumns, data, renderRowSubComponent }) {
const {
getTableProps,
headerGroups,
rows,
prepareRow,
columns,
state: [{ expanded }],
} = useTable(
{
columns: userColumns,
data,
},
useExpanded // We can useExpanded to track the expanded state
// for sub components too!
)
return (
<>
<pre>
<code>{JSON.stringify({ expanded }, null, 2)}</code>
</pre>
<table {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody>
{rows.map(
(row, i) =>
prepareRow(row) || (
// Use a React.Fragment here so the table markup is still valid
<>
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
)
})}
</tr>
{/*
If the row is in an expanded state, render a row with a
column that spands the entire length of the table.
*/}
{row.isExpanded ? (
<tr>
<td colSpan={columns.length}>
{/*
Inside it, call our renderRowSubComponent function. In reality,
you coul pass whatever you want as props to
a component like this, including the entire
table instance. But for this example, we'll just
pass the row
*/}
{renderRowSubComponent({ row })}
</td>
</tr>
) : null}
</>
)
)}
</tbody>
</table>
<br />
<div>Showing the first 20 results of {rows.length} rows</div>
</>
)
}
function App() {
const columns = React.useMemo(
() => [
{
// Make an expander cell
Header: () => null, // No header
id: 'expander', // It needs an ID
Cell: ({ row }) => (
// Use Cell to render an expander for each row.
// We can use the getExpandedToggleProps prop-getter
// to build the expander.
<span {...row.getExpandedToggleProps()}>
{row.isExpanded ? '👇' : '👉'}
</span>
),
},
{
Header: 'Name',
columns: [
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
],
},
{
Header: 'Info',
columns: [
{
Header: 'Age',
accessor: 'age',
},
{
Header: 'Visits',
accessor: 'visits',
},
{
Header: 'Status',
accessor: 'status',
},
{
Header: 'Profile Progress',
accessor: 'progress',
},
],
},
],
[]
)
const data = React.useMemo(() => makeData(10), [])
// Create a function that will render our row sub components
const renderRowSubComponent = React.useCallback(
({ row }) => (
<pre
style={{
fontSize: '10px',
}}
>
<code>{JSON.stringify({ values: row.values }, null, 2)}</code>
</pre>
),
[]
)
return (
<Styles>
<Table
columns={columns}
data={data}
// We added this as a prop for our table component
// Remember, this is not part of the React Table API,
// it's merely a rendering option we created for
// ourselves
renderRowSubComponent={renderRowSubComponent}
/>
</Styles>
)
}
export default App
@@ -0,0 +1,9 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<App />, div)
ReactDOM.unmountComponentAtNode(div)
})
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
@@ -0,0 +1,12 @@
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'
import * as serviceWorker from './serviceWorker'
ReactDOM.render(<App />, document.getElementById('root'))
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister()
@@ -0,0 +1,40 @@
import namor from 'namor'
const range = len => {
const arr = []
for (let i = 0; i < len; i++) {
arr.push(i)
}
return arr
}
const newPerson = () => {
const statusChance = Math.random()
return {
firstName: namor.generate({ words: 1, numbers: 0 }),
lastName: namor.generate({ words: 1, numbers: 0 }),
age: Math.floor(Math.random() * 30),
visits: Math.floor(Math.random() * 100),
progress: Math.floor(Math.random() * 100),
status:
statusChance > 0.66
? 'relationship'
: statusChance > 0.33
? 'complicated'
: 'single',
}
}
export default function makeData(...lens) {
const makeDataLevel = (depth = 0) => {
const len = lens[depth]
return range(len).map(d => {
return {
...newPerson(),
subRows: lens[depth + 1] ? makeDataLevel(depth + 1) : undefined,
}
})
}
return makeDataLevel()
}
@@ -0,0 +1,135 @@
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
)
export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href)
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`
if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config)
// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
)
})
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config)
}
})
}
}
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing
if (installingWorker == null) {
return
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
)
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration)
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.')
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration)
}
}
}
}
}
})
.catch(error => {
console.error('Error during service worker registration:', error)
})
}
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type')
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload()
})
})
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config)
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
)
})
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister()
})
}
}
File diff suppressed because it is too large Load Diff
+25 -16
View File
@@ -79,13 +79,15 @@ export const useTable = (props, ...plugins) => {
})
// Allow plugins to register hooks
if (debug) console.time('plugins')
if (process.env.NODE_ENV === 'development' && debug) console.time('plugins')
plugins.filter(Boolean).forEach(plugin => {
plugin(instanceRef.current.hooks)
})
if (debug) console.timeEnd('plugins')
if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('plugins')
if (debug) console.info('buildColumns/headerGroup/headers')
if (process.env.NODE_ENV === 'development' && debug)
console.info('buildColumns/headerGroup/headers')
// Decorate All the columns
let columnTree = React.useMemo(
() => decorateColumnTree(userColumns, defaultColumn),
@@ -100,13 +102,15 @@ export const useTable = (props, ...plugins) => {
// Allow hooks to decorate columns (and trigger this memoization via deps)
columns = React.useMemo(
() => {
if (debug) console.time('hooks.columnsBeforeHeaderGroups')
if (process.env.NODE_ENV === 'development' && debug)
console.time('hooks.columnsBeforeHeaderGroups')
const newColumns = applyHooks(
instanceRef.current.hooks.columnsBeforeHeaderGroups,
columns,
instanceRef.current
)
if (debug) console.timeEnd('hooks.columnsBeforeHeaderGroups')
if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('hooks.columnsBeforeHeaderGroups')
return newColumns
},
[
@@ -140,7 +144,8 @@ export const useTable = (props, ...plugins) => {
// Access the row model
instanceRef.current.rows = React.useMemo(
() => {
if (debug) console.time('getAccessedRows')
if (process.env.NODE_ENV === 'development' && debug)
console.time('getAccessedRows')
// Access the row's data
const accessRow = (originalRow, i, depth = 0, parentPath = []) => {
// Keep the original reference around
@@ -190,7 +195,8 @@ export const useTable = (props, ...plugins) => {
// Use the resolved data
const accessedData = data.map((d, i) => accessRow(d, i))
if (debug) console.timeEnd('getAccessedRows')
if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('getAccessedRows')
return accessedData
},
[debug, data, subRowsKey]
@@ -204,7 +210,8 @@ export const useTable = (props, ...plugins) => {
: !!column.show
})
if (debug) console.time('hooks.useMain')
if (process.env.NODE_ENV === 'development' && debug)
console.time('hooks.useMain')
instanceRef.current = applyHooks(
instanceRef.current.hooks.useMain,
instanceRef.current
@@ -213,22 +220,22 @@ export const useTable = (props, ...plugins) => {
console.timeEnd('hooks.useMain')
// // Allow hooks to decorate columns
// if (debug) console.time('hooks.useColumns')
// if (process.env.NODE_ENV === 'development' && debug) console.time('hooks.useColumns')
// instanceRef.current.columns = applyHooks(
// instanceRef.current.hooks.useColumns,
// instanceRef.current.columns,
// instanceRef.current
// )
// if (debug) console.timeEnd('hooks.useColumns')
// if (process.env.NODE_ENV === 'development' && debug) console.timeEnd('hooks.useColumns')
// // Allow hooks to decorate headers
// if (debug) console.time('hooks.useHeaders')
// if (process.env.NODE_ENV === 'development' && debug) console.time('hooks.useHeaders')
// instanceRef.current.headers = applyHooks(
// instanceRef.current.hooks.useHeaders,
// instanceRef.current.headers,
// instanceRef.current
// )
// if (debug) console.timeEnd('hooks.useHeaders')
// if (process.env.NODE_ENV === 'development' && debug) console.timeEnd('hooks.useHeaders')
;[...instanceRef.current.columns, ...instanceRef.current.headers].forEach(
column => {
// Give columns/headers rendering power
@@ -264,7 +271,7 @@ export const useTable = (props, ...plugins) => {
)
// // Allow hooks to decorate headerGroups
// if (debug) console.time('hooks.useHeaderGroups')
// if (process.env.NODE_ENV === 'development' && debug) console.time('hooks.useHeaderGroups')
// instanceRef.current.headerGroups = applyHooks(
// instanceRef.current.hooks.useHeaderGroups,
// instanceRef.current.headerGroups,
@@ -306,16 +313,18 @@ export const useTable = (props, ...plugins) => {
return false
})
// if (debug) console.timeEnd('hooks.useHeaderGroups')
// if (process.env.NODE_ENV === 'development' && debug) console.timeEnd('hooks.useHeaderGroups')
// Run the rows (this could be a dangerous hook with a ton of data)
if (debug) console.time('hooks.useRows')
if (process.env.NODE_ENV === 'development' && debug)
console.time('hooks.useRows')
instanceRef.current.rows = applyHooks(
instanceRef.current.hooks.useRows,
instanceRef.current.rows,
instanceRef.current
)
if (debug) console.timeEnd('hooks.useRows')
if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('hooks.useRows')
// The prepareRow function is absolutely necessary and MUST be called on
// any rows the user wishes to be displayed.
+1 -1
View File
@@ -8,5 +8,5 @@ export { useGroupBy } from './plugin-hooks/useGroupBy'
export { useSortBy } from './plugin-hooks/useSortBy'
export { usePagination } from './plugin-hooks/usePagination'
export { useFlexLayout } from './plugin-hooks/useFlexLayout'
export { useTokenPagination } from './plugin-hooks/useTokenPagination'
export { useTokenPagination } from './utility-hooks/useTokenPagination'
export { actions, addActions } from './actions'
@@ -0,0 +1,520 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders a paginated table 1`] = `
"Snapshot Diff:
- First value
+ Second value
@@ -65,11 +65,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 1
+ tanner 11
</td>
<td
class=\\"\\"
>
linsley
@@ -99,11 +99,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 2
+ tanner 12
</td>
<td
class=\\"\\"
>
linsley
@@ -133,11 +133,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 3
+ tanner 13
</td>
<td
class=\\"\\"
>
linsley
@@ -167,11 +167,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 4
+ tanner 14
</td>
<td
class=\\"\\"
>
linsley
@@ -201,11 +201,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 5
+ tanner 15
</td>
<td
class=\\"\\"
>
linsley
@@ -235,11 +235,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 6
+ tanner 16
</td>
<td
class=\\"\\"
>
linsley
@@ -269,11 +269,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 7
+ tanner 17
</td>
<td
class=\\"\\"
>
linsley
@@ -303,11 +303,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 8
+ tanner 18
</td>
<td
class=\\"\\"
>
linsley
@@ -337,11 +337,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 9
+ tanner 19
</td>
<td
class=\\"\\"
>
linsley
@@ -371,11 +371,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 10
+ tanner 20
</td>
<td
class=\\"\\"
>
linsley
@@ -404,19 +404,15 @@
</tbody>
</table>
<div
class=\\"pagination\\"
>
- <button
- disabled=\\"\\"
- >
+ <button>
&lt;&lt;
</button>
- <button
- disabled=\\"\\"
- >
+ <button>
&lt;
</button>
<button>
&gt;
@@ -427,20 +423,20 @@
</button>
<span>
Page
<strong>
- 1 of 10
+ 2 of 10
</strong>
</span>
<span>
| Go to page:
<input
style=\\"width: 100px;\\"
type=\\"number\\"
- value=\\"1\\"
+ value=\\"2\\"
/>
</span>
<select>
<option"
`;
exports[`renders a paginated table 2`] = `
"Snapshot Diff:
- First value
+ Second value
@@ -65,11 +65,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 11
+ tanner 21
</td>
<td
class=\\"\\"
>
linsley
@@ -99,11 +99,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 12
+ tanner 22
</td>
<td
class=\\"\\"
>
linsley
@@ -133,11 +133,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 13
+ tanner 23
</td>
<td
class=\\"\\"
>
linsley
@@ -167,11 +167,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 14
+ tanner 24
</td>
<td
class=\\"\\"
>
linsley
@@ -201,11 +201,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 15
+ tanner 25
</td>
<td
class=\\"\\"
>
linsley
@@ -235,11 +235,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 16
+ tanner 26
</td>
<td
class=\\"\\"
>
linsley
@@ -269,11 +269,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 17
+ tanner 27
</td>
<td
class=\\"\\"
>
linsley
@@ -303,11 +303,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 18
+ tanner 28
</td>
<td
class=\\"\\"
>
linsley
@@ -337,11 +337,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 19
+ tanner 29
</td>
<td
class=\\"\\"
>
linsley
@@ -371,11 +371,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 20
+ tanner 30
</td>
<td
class=\\"\\"
>
linsley
@@ -423,20 +423,20 @@
</button>
<span>
Page
<strong>
- 2 of 10
+ 3 of 10
</strong>
</span>
<span>
| Go to page:
<input
style=\\"width: 100px;\\"
type=\\"number\\"
- value=\\"2\\"
+ value=\\"3\\"
/>
</span>
<select>
<option"
`;
exports[`renders a paginated table 3`] = `
"Snapshot Diff:
- First value
+ Second value
@@ -65,11 +65,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 21
+ tanner 91
</td>
<td
class=\\"\\"
>
linsley
@@ -99,11 +99,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 22
+ tanner 92
</td>
<td
class=\\"\\"
>
linsley
@@ -133,11 +133,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 23
+ tanner 93
</td>
<td
class=\\"\\"
>
linsley
@@ -167,11 +167,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 24
+ tanner 94
</td>
<td
class=\\"\\"
>
linsley
@@ -201,11 +201,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 25
+ tanner 95
</td>
<td
class=\\"\\"
>
linsley
@@ -235,11 +235,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 26
+ tanner 96
</td>
<td
class=\\"\\"
>
linsley
@@ -269,11 +269,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 27
+ tanner 97
</td>
<td
class=\\"\\"
>
linsley
@@ -303,11 +303,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 28
+ tanner 98
</td>
<td
class=\\"\\"
>
linsley
@@ -337,11 +337,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 29
+ tanner 99
</td>
<td
class=\\"\\"
>
linsley
@@ -371,11 +371,11 @@
class=\\"\\"
>
<td
class=\\"\\"
>
- tanner 30
+ tanner 100
</td>
<td
class=\\"\\"
>
linsley
@@ -412,31 +412,35 @@
<button>
&lt;
</button>
- <button>
+ <button
+ disabled=\\"\\"
+ >
&gt;
</button>
- <button>
+ <button
+ disabled=\\"\\"
+ >
&gt;&gt;
</button>
<span>
Page
<strong>
- 3 of 10
+ 10 of 10
</strong>
</span>
<span>
| Go to page:
<input
style=\\"width: 100px;\\"
type=\\"number\\"
- value=\\"3\\"
+ value=\\"10\\"
/>
</span>
<select>
<option"
`;
@@ -0,0 +1,180 @@
import '@testing-library/react/cleanup-after-each'
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import { useTable } from '../../hooks/useTable'
import { usePagination } from '../usePagination'
const data = [...new Array(100)].map((d, i) => ({
firstName: 'tanner ' + (i + 1),
lastName: 'linsley',
age: 29,
visits: 100,
status: 'In Relationship',
progress: 50,
}))
function Table({ columns, data }) {
const {
getTableProps,
headerGroups,
prepareRow,
page,
canPreviousPage,
canNextPage,
pageOptions,
pageCount,
gotoPage,
nextPage,
previousPage,
setPageSize,
state: [{ pageIndex, pageSize }],
} = useTable(
{
columns,
data,
},
usePagination
)
return (
<>
<table {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody>
{page.map(
(row, i) =>
prepareRow(row) || (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
)
})}
</tr>
)
)}
</tbody>
</table>
<div className="pagination">
<button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
{'<<'}
</button>{' '}
<button onClick={() => previousPage()} disabled={!canPreviousPage}>
{'<'}
</button>{' '}
<button onClick={() => nextPage()} disabled={!canNextPage}>
{'>'}
</button>{' '}
<button onClick={() => gotoPage(pageCount - 1)} disabled={!canNextPage}>
{'>>'}
</button>{' '}
<span>
Page{' '}
<strong>
{pageIndex + 1} of {pageOptions.length}
</strong>{' '}
</span>
<span>
| Go to page:{' '}
<input
type="number"
defaultValue={pageIndex + 1}
onChange={e => {
const page = e.target.value ? Number(e.target.value) - 1 : 0
gotoPage(page)
}}
style={{ width: '100px' }}
/>
</span>{' '}
<select
value={pageSize}
onChange={e => {
setPageSize(Number(e.target.value))
}}
>
{[10, 20, 30, 40, 50].map(pageSize => (
<option key={pageSize} value={pageSize}>
Show {pageSize}
</option>
))}
</select>
</div>
</>
)
}
function App() {
const columns = React.useMemo(
() => [
{
Header: 'Name',
columns: [
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
],
},
{
Header: 'Info',
columns: [
{
Header: 'Age',
accessor: 'age',
},
{
Header: 'Visits',
accessor: 'visits',
},
{
Header: 'Status',
accessor: 'status',
},
{
Header: 'Profile Progress',
accessor: 'progress',
},
],
},
],
[]
)
return <Table columns={columns} data={data} />
}
test('renders a paginated table', () => {
const { getByText, asFragment } = render(<App />)
const beforeSort = asFragment()
fireEvent.click(getByText('>'))
const afterSort1 = asFragment()
fireEvent.click(getByText('>'))
const afterSort2 = asFragment()
fireEvent.click(getByText('>>'))
const afterSort3 = asFragment()
expect(beforeSort).toMatchDiffSnapshot(afterSort1)
expect(afterSort1).toMatchDiffSnapshot(afterSort2)
expect(afterSort2).toMatchDiffSnapshot(afterSort3)
})
+30 -6
View File
@@ -1,7 +1,13 @@
import { useMemo } from 'react'
import PropTypes from 'prop-types'
import { getBy, getFirstDefined, setBy } from '../utils'
import {
getBy,
getFirstDefined,
setBy,
mergeProps,
applyPropHooks,
} from '../utils'
import { addActions, actions } from '../actions'
import { defaultState } from '../hooks/useTableState'
@@ -11,10 +17,11 @@ addActions('toggleExpanded', 'useExpanded')
const propTypes = {
manualExpandedKey: PropTypes.string,
paginateSubRows: PropTypes.bool,
nestExpandedRows: PropTypes.bool,
}
export const useExpanded = hooks => {
hooks.getExpandedToggleProps = []
hooks.useMain.push(useMain)
}
@@ -27,7 +34,7 @@ function useMain(instance) {
manualExpandedKey = 'expanded',
hooks,
state: [{ expanded }, setState],
paginateSubRows = true,
nestExpandedRows,
} = instance
const toggleExpandedByPath = (path, set) => {
@@ -44,12 +51,29 @@ function useMain(instance) {
hooks.prepareRow.push(row => {
row.toggleExpanded = set => toggleExpandedByPath(row.path, set)
row.getExpandedToggleProps = props => {
return mergeProps(
{
onClick: e => {
e.persist()
row.toggleExpanded()
},
style: {
cursor: 'pointer',
},
title: 'Toggle Expanded',
},
applyPropHooks(instance.hooks.getExpandedToggleProps, row, instance),
props
)
}
return row
})
const expandedRows = useMemo(
() => {
if (debug) console.info('getExpandedRows')
if (process.env.NODE_ENV === 'development' && debug)
console.info('getExpandedRows')
const expandedRows = []
@@ -60,7 +84,7 @@ function useMain(instance) {
(row.original && row.original[manualExpandedKey]) ||
getBy(expanded, row.path)
if (paginateSubRows || (!paginateSubRows && row.depth === 0)) {
if (!nestExpandedRows || (nestExpandedRows && row.depth === 0)) {
expandedRows.push(row)
}
@@ -77,7 +101,7 @@ function useMain(instance) {
return expandedRows
},
[debug, rows, manualExpandedKey, expanded, paginateSubRows]
[debug, rows, manualExpandedKey, expanded, nestExpandedRows]
)
const expandedDepth = findExpandedDepth(expanded)
+2 -1
View File
@@ -128,7 +128,8 @@ function useMain(instance) {
return rows
}
if (debug) console.info('getFilteredRows')
if (process.env.NODE_ENV === 'development' && debug)
console.info('getFilteredRows')
// Filters top level and nested rows
const filterRows = rows => {
+2 -1
View File
@@ -150,7 +150,8 @@ function useMain(instance) {
return rows
}
if (debug) console.info('getGroupedRows')
if (process.env.NODE_ENV === 'development' && debug)
console.info('getGroupedRows')
// Find the columns that can or are aggregating
// Uses each column to aggregate rows into a single value
+39 -2
View File
@@ -33,6 +33,7 @@ function useMain(instance) {
manualPagination,
disablePageResetOnDataChange,
debug,
plugins,
state: [
{
pageSize,
@@ -46,6 +47,40 @@ function useMain(instance) {
],
} = instance
// If usePagination should probably come after useFilters for
// the best performance, so let's hint to the user about that...
const pluginIndex = plugins.indexOf(usePagination)
if (process.env.NODE_ENV === 'development') {
const useFiltersIndex = plugins.findIndex(
plugin => plugin.name === 'useFilters'
)
const useGroupByIndex = plugins.findIndex(
plugin => plugin.name === 'useGroupBy'
)
const useSortByIndex = plugins.findIndex(
plugin => plugin.name === 'useSortBy'
)
if (useFiltersIndex > pluginIndex) {
throw new Error(
'React Table: The usePagination plugin hook must be placed before the useFilters plugin hook!'
)
}
if (useGroupByIndex > pluginIndex) {
throw new Error(
'React Table: The usePagination plugin hook must be placed before the useGroupBy plugin hook!'
)
}
if (useSortByIndex > pluginIndex) {
throw new Error(
'React Table: The usePagination plugin hook must be placed before the useSortBy plugin hook!'
)
}
}
const rowDep = disablePageResetOnDataChange ? null : rows
useLayoutEffect(
@@ -69,7 +104,8 @@ function useMain(instance) {
pageCount: userPageCount,
}
}
if (debug) console.info('getPages')
if (process.env.NODE_ENV === 'development' && debug)
console.info('getPages')
// Create a new pages with the first page ready to go.
const pages = rows.length ? [] : [[]]
@@ -102,7 +138,8 @@ function useMain(instance) {
const canNextPage = pageIndex < pageCount - 1
const gotoPage = pageIndex => {
if (debug) console.info('gotoPage')
if (process.env.NODE_ENV === 'development' && debug)
console.info('gotoPage')
return setState(old => {
if (pageIndex < 0 || pageIndex > pageCount - 1) {
return old
+16 -12
View File
@@ -57,18 +57,20 @@ function useMain(instance) {
plugins,
} = instance
// If useSortBy should probably come after useFilters for
// the best performance, so let's hint to the user about that...
const pluginIndex = plugins.indexOf(useSortBy)
if (process.env.NODE_ENV === 'development') {
// If useSortBy should probably come after useFilters for
// the best performance, so let's hint to the user about that...
const pluginIndex = plugins.indexOf(useSortBy)
const useFiltersIndex = plugins.findIndex(
plugin => plugin.name === 'useFilters'
)
if (useFiltersIndex > pluginIndex) {
console.warn(
'React Table: useSortBy should be placed before useFilters in your plugin list for better performance!'
const useFiltersIndex = plugins.findIndex(
plugin => plugin.name === 'useFilters'
)
if (useFiltersIndex > pluginIndex) {
console.warn(
'React Table: useSortBy should be placed before useFilters in your plugin list for better performance!'
)
}
}
// Add custom hooks
@@ -211,7 +213,8 @@ function useMain(instance) {
if (manualSorting || !sortBy.length) {
return rows
}
if (debug) console.time('getSortedRows')
if (process.env.NODE_ENV === 'development' && debug)
console.time('getSortedRows')
const sortData = rows => {
// Use the orderByFn to compose multiple sortBy's together.
@@ -261,7 +264,8 @@ function useMain(instance) {
row.subRows = sortData(row.subRows)
})
if (debug) console.timeEnd('getSortedRows')
if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('getSortedRows')
return sortedData
}