mirror of
https://github.com/gosticks/react-table.git
synced 2026-08-19 16:30:21 +00:00
Compare commits
73
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d267b3442 | ||
|
|
388b3ec2b9 | ||
|
|
b2abd82350 | ||
|
|
06a6d6fcc3 | ||
|
|
d8599817ad | ||
|
|
e1060db327 | ||
|
|
ea79cd8388 | ||
|
|
b2ed18696f | ||
|
|
9f8eab5777 | ||
|
|
4f13f32023 | ||
|
|
5bb2e49764 | ||
|
|
d1f26fe7bc | ||
|
|
c7d6562d04 | ||
|
|
e82d2d4b36 | ||
|
|
0716e234a1 | ||
|
|
824ee34ba9 | ||
|
|
514fbabb88 | ||
|
|
2ecf40518e | ||
|
|
fd64486286 | ||
|
|
34909305af | ||
|
|
5043af5071 | ||
|
|
2ca7f78d72 | ||
|
|
811cd015c2 | ||
|
|
9e37846a21 | ||
|
|
723720c8b1 | ||
|
|
2a20ad3ba4 | ||
|
|
c6d37e4834 | ||
|
|
a1a192052d | ||
|
|
455e0730bf | ||
|
|
1d4867fa2f | ||
|
|
c3da2a7c41 | ||
|
|
c3a52c5845 | ||
|
|
6dea8103d3 | ||
|
|
3bab8372d1 | ||
|
|
b85f457e62 | ||
|
|
c6871084d6 | ||
|
|
8946771563 | ||
|
|
fbc8032d29 | ||
|
|
6a3bc5559e | ||
|
|
91e28b93bf | ||
|
|
a6345dbe2b | ||
|
|
0b0de317ee | ||
|
|
0ed0c78286 | ||
|
|
fad6ac4014 | ||
|
|
8f142b3788 | ||
|
|
daff1f9ca3 | ||
|
|
96900f645a | ||
|
|
272a99fcd8 | ||
|
|
07fb9529e4 | ||
|
|
b5d399efd6 | ||
|
|
14e931548a | ||
|
|
bc3ea07c3e | ||
|
|
4a3929cd50 | ||
|
|
e2bb09d8b2 | ||
|
|
18f2dea247 | ||
|
|
ead3599378 | ||
|
|
addf28a011 | ||
|
|
e4429b7143 | ||
|
|
4b2d0eb187 | ||
|
|
5369051b05 | ||
|
|
58d38b668b | ||
|
|
9de149cf3c | ||
|
|
289dca1caf | ||
|
|
f257a3496f | ||
|
|
312a137f02 | ||
|
|
c6167566da | ||
|
|
5b0e68aead | ||
|
|
2f5ebe460f | ||
|
|
2ab4a65dcc | ||
|
|
94a6ee53f7 | ||
|
|
ab32721453 | ||
|
|
98bee0a4a7 | ||
|
|
882e5f6fcf |
@@ -1,15 +1,22 @@
|
|||||||
{
|
{
|
||||||
"parser": "babel-eslint",
|
"parser": "babel-eslint",
|
||||||
"extends": ["standard", "standard-react"],
|
"extends": ["react-app", "prettier"],
|
||||||
"env": {
|
"plugins": [
|
||||||
"es6": true
|
"react-hooks"
|
||||||
},
|
],
|
||||||
"plugins": ["react"],
|
"env": {
|
||||||
"parserOptions": {
|
"es6": true
|
||||||
"sourceType": "module"
|
},
|
||||||
},
|
"parserOptions": {
|
||||||
"rules": {
|
"sourceType": "module"
|
||||||
"space-before-function-paren": 0,
|
},
|
||||||
"react/jsx-boolean-value": 0
|
"rules": {
|
||||||
}
|
"space-before-function-paren": 0,
|
||||||
|
"react/jsx-boolean-value": 0
|
||||||
|
},
|
||||||
|
"settings": {
|
||||||
|
"react": {
|
||||||
|
"version": "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
github: tannerlinsley
|
||||||
|
patreon: tannerlinsley
|
||||||
|
custom: https://youtube.com/tannerlinsley
|
||||||
+4
-4
@@ -1,7 +1,7 @@
|
|||||||
node_modules/
|
node_modules
|
||||||
lib/
|
lib
|
||||||
dist/
|
dist
|
||||||
es/
|
es
|
||||||
docs/build
|
docs/build
|
||||||
react-table.js
|
react-table.js
|
||||||
react-table.min.js
|
react-table.min.js
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
hooks: {
|
||||||
|
'pre-commit': 'lint-staged',
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
moduleDirectories: [
|
||||||
|
'node_modules',
|
||||||
|
/*
|
||||||
|
* make 'test/utils' available in tests, e.g.
|
||||||
|
*
|
||||||
|
* const {myModule} = require('utils/my-test-helper')
|
||||||
|
*/
|
||||||
|
__dirname,
|
||||||
|
],
|
||||||
|
|
||||||
|
rootDir: path.resolve(__dirname, '../..'),
|
||||||
|
|
||||||
|
roots: ['<rootDir>/src', __dirname],
|
||||||
|
};
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
const {rootDir} = require('./jest.common');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
rootDir,
|
||||||
|
|
||||||
|
displayName: 'lint',
|
||||||
|
|
||||||
|
runner: 'jest-runner-eslint',
|
||||||
|
|
||||||
|
testMatch: ['<rootDir>/src/**/*.js'],
|
||||||
|
|
||||||
|
testPathIgnorePatterns: ['node_modules', 'coverage', 'dist', '.test.js'],
|
||||||
|
};
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
const commonConfig = require('./jest.common');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
...commonConfig,
|
||||||
|
|
||||||
|
displayName: 'unit',
|
||||||
|
|
||||||
|
coverageDirectory: '../../coverage',
|
||||||
|
|
||||||
|
testMatch: ['<rootDir>/tests/**/*.js'],
|
||||||
|
|
||||||
|
transform: {
|
||||||
|
// '^.+\\.js$': '<rootDir>/node_modules/babel-jest',
|
||||||
|
},
|
||||||
|
};
|
||||||
Vendored
-2
File diff suppressed because one or more lines are too long
Vendored
-1
File diff suppressed because one or more lines are too long
Vendored
BIN
Binary file not shown.
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"presets": ["react-app"],
|
||||||
|
"plugins": ["styled-components"]
|
||||||
|
}
|
||||||
@@ -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,121 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import styled from 'styled-components'
|
||||||
|
import { useTable } 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
function Table({ columns, data }) {
|
||||||
|
// Use the state and functions returned from useTable to build your UI
|
||||||
|
const { getTableProps, headerGroups, rows, prepareRow } = useTable({
|
||||||
|
columns,
|
||||||
|
data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Render the UI for your table
|
||||||
|
return (
|
||||||
|
<table {...getTableProps()}>
|
||||||
|
<thead>
|
||||||
|
{headerGroups.map(headerGroup => (
|
||||||
|
<tr {...headerGroup.getRowProps()}>
|
||||||
|
{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>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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(20), [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Styles>
|
||||||
|
<Table columns={columns} data={data} />
|
||||||
|
</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
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"presets": ["react-app"],
|
||||||
|
"plugins": ["styled-components"]
|
||||||
|
}
|
||||||
@@ -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,130 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import styled from 'styled-components'
|
||||||
|
import { useTable, useSortBy } 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
function Table({ columns, data }) {
|
||||||
|
const { getTableProps, headerGroups, rows, prepareRow } = useTable(
|
||||||
|
{
|
||||||
|
columns,
|
||||||
|
data,
|
||||||
|
},
|
||||||
|
useSortBy
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<table {...getTableProps()}>
|
||||||
|
<thead>
|
||||||
|
{headerGroups.map(headerGroup => (
|
||||||
|
<tr {...headerGroup.getRowProps()}>
|
||||||
|
{headerGroup.headers.map(column => (
|
||||||
|
// Add the sorting props to control sorting. For this example
|
||||||
|
// we can add them into the header props
|
||||||
|
<th {...column.getHeaderProps(column.getSortByToggleProps())}>
|
||||||
|
{column.render('Header')}
|
||||||
|
{/* Add a sort direction indicator */}
|
||||||
|
<span>
|
||||||
|
{column.sorted ? (column.sortedDesc ? ' 🔽' : ' 🔼') : ''}
|
||||||
|
</span>
|
||||||
|
</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>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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(20), [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Styles>
|
||||||
|
<Table columns={columns} data={data} />
|
||||||
|
</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
@@ -0,0 +1,16 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const lintProject = require('./configs/tests/jest.lint');
|
||||||
|
const unitProject = require('./configs/tests/jest.unit');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
...require('./configs/tests/jest.common'),
|
||||||
|
|
||||||
|
projects: [lintProject, unitProject],
|
||||||
|
|
||||||
|
watchPlugins: [
|
||||||
|
'jest-watch-typeahead/filename',
|
||||||
|
'jest-watch-typeahead/testname',
|
||||||
|
'jest-watch-select-projects',
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = {
|
||||||
|
'*.js': ['prettier --write', 'git add'],
|
||||||
|
}
|
||||||
Generated
+6023
-4491
File diff suppressed because it is too large
Load Diff
+23
-9
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-table",
|
"name": "react-table",
|
||||||
"version": "7.0.0-alpha.2",
|
"version": "7.0.0-alpha.8",
|
||||||
"description": "A fast, lightweight, opinionated table and datagrid built on React",
|
"description": "A fast, lightweight, opinionated table and datagrid built on React",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"homepage": "https://github.com/tannerlinsley/react-table#readme",
|
"homepage": "https://github.com/tannerlinsley/react-table#readme",
|
||||||
@@ -15,11 +15,12 @@
|
|||||||
"datagrid"
|
"datagrid"
|
||||||
],
|
],
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"_module": "dist/index.es.js",
|
"module": "dist/index.es.js",
|
||||||
"_jsnext:main": "dist/index.es.js",
|
"jsnext:main": "dist/index.es.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "cross-env CI=1 react-scripts test --env=jsdom",
|
"test": "is-ci 'test:ci' 'test:dev'",
|
||||||
"test:watch": "react-scripts test --env=jsdom",
|
"test:dev": "jest --watch",
|
||||||
|
"test:ci": "jest",
|
||||||
"build": "rollup -c",
|
"build": "rollup -c",
|
||||||
"start": "rollup -c -w",
|
"start": "rollup -c -w",
|
||||||
"prepare": "yarn build",
|
"prepare": "yarn build",
|
||||||
@@ -46,17 +47,30 @@
|
|||||||
"@babel/preset-react": "^7.0.0",
|
"@babel/preset-react": "^7.0.0",
|
||||||
"@babel/runtime": "^7.2.0",
|
"@babel/runtime": "^7.2.0",
|
||||||
"@svgr/rollup": "^4.1.0",
|
"@svgr/rollup": "^4.1.0",
|
||||||
|
"@testing-library/react": "^8.0.4",
|
||||||
"babel-core": "7.0.0-bridge.0",
|
"babel-core": "7.0.0-bridge.0",
|
||||||
"babel-eslint": "^10.0.1",
|
"babel-eslint": "9.x",
|
||||||
"cross-env": "^5.1.4",
|
"cross-env": "^5.1.4",
|
||||||
"eslint": "^5.10.0",
|
"eslint": "5.x",
|
||||||
|
"eslint-config-prettier": "^4.3.0",
|
||||||
|
"eslint-config-react-app": "^4.0.1",
|
||||||
"eslint-config-standard": "^12.0.0",
|
"eslint-config-standard": "^12.0.0",
|
||||||
"eslint-config-standard-react": "^7.0.2",
|
"eslint-config-standard-react": "^7.0.2",
|
||||||
"eslint-plugin-import": "^2.13.0",
|
"eslint-plugin-flowtype": "2.x",
|
||||||
|
"eslint-plugin-import": "2.x",
|
||||||
|
"eslint-plugin-jsx-a11y": "6.x",
|
||||||
"eslint-plugin-node": "^8.0.0",
|
"eslint-plugin-node": "^8.0.0",
|
||||||
|
"eslint-plugin-prettier": "^3.1.0",
|
||||||
"eslint-plugin-promise": "^4.0.0",
|
"eslint-plugin-promise": "^4.0.0",
|
||||||
"eslint-plugin-react": "^7.10.0",
|
"eslint-plugin-react": "7.x",
|
||||||
|
"eslint-plugin-react-hooks": "1.5.0",
|
||||||
"eslint-plugin-standard": "^4.0.0",
|
"eslint-plugin-standard": "^4.0.0",
|
||||||
|
"is-ci-cli": "^1.1.1",
|
||||||
|
"jest": "^24.8.0",
|
||||||
|
"jest-cli": "^24.8.0",
|
||||||
|
"jest-runner-eslint": "^0.7.4",
|
||||||
|
"jest-watch-select-projects": "^0.1.2",
|
||||||
|
"jest-watch-typeahead": "^0.3.1",
|
||||||
"rollup": "^0.68.0",
|
"rollup": "^0.68.0",
|
||||||
"rollup-plugin-babel": "^4.1.0",
|
"rollup-plugin-babel": "^4.1.0",
|
||||||
"rollup-plugin-commonjs": "^9.1.3",
|
"rollup-plugin-commonjs": "^9.1.3",
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
module.exports = {
|
||||||
|
trailingComma: 'es5',
|
||||||
|
tabWidth: 2,
|
||||||
|
semi: false,
|
||||||
|
singleQuote: true,
|
||||||
|
}
|
||||||
+30
-21
@@ -6,27 +6,36 @@ import { uglify } from 'rollup-plugin-uglify'
|
|||||||
|
|
||||||
import pkg from './package.json'
|
import pkg from './package.json'
|
||||||
|
|
||||||
export default {
|
export default [
|
||||||
input: 'src/index.js',
|
{
|
||||||
output: [
|
input: 'src/index.js',
|
||||||
{
|
output: {
|
||||||
file: pkg.main,
|
file: pkg.main,
|
||||||
format: 'cjs',
|
format: 'cjs',
|
||||||
sourcemap: true
|
sourcemap: true
|
||||||
}
|
},
|
||||||
// {
|
plugins: [
|
||||||
// file: pkg.module,
|
external(),
|
||||||
// format: "es",
|
babel({
|
||||||
// sourcemap: true
|
exclude: 'node_modules/**'
|
||||||
// }
|
}),
|
||||||
],
|
resolve(),
|
||||||
plugins: [
|
commonjs(),
|
||||||
external(),
|
uglify()
|
||||||
babel({
|
]
|
||||||
exclude: 'node_modules/**'
|
},
|
||||||
}),
|
{
|
||||||
resolve(),
|
input: 'src/index.js',
|
||||||
commonjs(),
|
external: Object.keys(pkg.peerDependencies),
|
||||||
uglify()
|
output: {
|
||||||
]
|
file: pkg.module,
|
||||||
}
|
format: 'es',
|
||||||
|
sourcemap: true
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
babel({
|
||||||
|
exclude: ['/**/node_modules/**']
|
||||||
|
})
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|||||||
+15
-4
@@ -1,9 +1,20 @@
|
|||||||
const actions = {}
|
const actions = {}
|
||||||
|
const types = {}
|
||||||
|
|
||||||
export { actions }
|
export { actions, types }
|
||||||
|
|
||||||
export const addActions = acts => {
|
export const addActions = (...acts) => {
|
||||||
Object.keys(acts).forEach(key => {
|
acts.forEach(action => {
|
||||||
actions[key] = acts[key]
|
if (actions[action]) {
|
||||||
|
throw new Error(
|
||||||
|
`An React Table action type called ${action} has already been registered!`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// Action values are formatted this way to discourage
|
||||||
|
// you (the dev) from interacting with them in any way
|
||||||
|
// other than importing `{ actions } from 'react-table'`
|
||||||
|
// and referencing an action via `actions[actionName]`
|
||||||
|
actions[action] = `React Table Action: ${action}`
|
||||||
|
types[actions[action]] = true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
export const text = (rows, id, filterValue) => {
|
||||||
|
return rows.filter(row => {
|
||||||
|
const rowValue = row.values[id]
|
||||||
|
return rowValue !== undefined
|
||||||
|
? String(rowValue)
|
||||||
|
.toLowerCase()
|
||||||
|
.includes(String(filterValue).toLowerCase())
|
||||||
|
: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const exactText = (rows, id, filterValue) => {
|
||||||
|
return rows.filter(row => {
|
||||||
|
const rowValue = row.values[id]
|
||||||
|
return rowValue !== undefined
|
||||||
|
? String(rowValue).toLowerCase() === String(filterValue).toLowerCase()
|
||||||
|
: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const exactTextCase = (rows, id, filterValue) => {
|
||||||
|
return rows.filter(row => {
|
||||||
|
const rowValue = row.values[id]
|
||||||
|
return rowValue !== undefined
|
||||||
|
? String(rowValue) === String(filterValue)
|
||||||
|
: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const includes = (rows, id, filterValue) => {
|
||||||
|
return rows.filter(row => {
|
||||||
|
const rowValue = row.values[id]
|
||||||
|
return filterValue.includes(rowValue)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const includesAll = (rows, id, filterValue) => {
|
||||||
|
return rows.filter(row => {
|
||||||
|
const rowValue = row.values[id]
|
||||||
|
return filterValue.every(val => rowValue.includes(val))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const exact = (rows, id, filterValue) => {
|
||||||
|
return rows.filter(row => {
|
||||||
|
const rowValue = row.values[id]
|
||||||
|
return rowValue === filterValue
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const between = (rows, id, filterValue) => {
|
||||||
|
return rows.filter(row => {
|
||||||
|
const rowValue = row.values[id]
|
||||||
|
return rowValue >= filterValue[0] && rowValue <= filterValue[1]
|
||||||
|
})
|
||||||
|
}
|
||||||
+164
-140
@@ -8,16 +8,166 @@ const propTypes = {
|
|||||||
columns: PropTypes.arrayOf(
|
columns: PropTypes.arrayOf(
|
||||||
PropTypes.shape({
|
PropTypes.shape({
|
||||||
Cell: PropTypes.any,
|
Cell: PropTypes.any,
|
||||||
Header: PropTypes.any
|
Header: PropTypes.any,
|
||||||
})
|
})
|
||||||
)
|
),
|
||||||
|
defaultColumn: PropTypes.any,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the depth of the columns
|
||||||
|
function findMaxDepth(columns, depth = 0) {
|
||||||
|
return columns.reduce((prev, curr) => {
|
||||||
|
if (curr.columns) {
|
||||||
|
return Math.max(prev, findMaxDepth(curr.columns, depth + 1))
|
||||||
|
}
|
||||||
|
return depth
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
function decorateColumn(column, defaultColumn, parent, depth, index) {
|
||||||
|
// Apply the defaultColumn
|
||||||
|
column = { ...defaultColumn, ...column }
|
||||||
|
|
||||||
|
// First check for string accessor
|
||||||
|
let { id, accessor, Header } = column
|
||||||
|
|
||||||
|
if (typeof accessor === 'string') {
|
||||||
|
id = id || accessor
|
||||||
|
const accessorString = accessor
|
||||||
|
accessor = row => getBy(row, accessorString)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id && typeof Header === 'string') {
|
||||||
|
id = Header
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id && column.columns) {
|
||||||
|
console.error(column)
|
||||||
|
throw new Error('A column ID (or unique "Header" value) is required!')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
console.error(column)
|
||||||
|
throw new Error('A column ID (or string accessor) is required!')
|
||||||
|
}
|
||||||
|
|
||||||
|
column = {
|
||||||
|
Header: ({ id }) => id,
|
||||||
|
Cell: ({ value }) => value,
|
||||||
|
show: true,
|
||||||
|
...column,
|
||||||
|
id,
|
||||||
|
accessor,
|
||||||
|
parent,
|
||||||
|
depth,
|
||||||
|
index,
|
||||||
|
}
|
||||||
|
|
||||||
|
return column
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the visible columns, headers and flat column list
|
||||||
|
function decorateColumnTree(columns, defaultColumn, parent, depth = 0) {
|
||||||
|
return columns.map((column, columnIndex) => {
|
||||||
|
column = decorateColumn(column, defaultColumn, parent, depth, columnIndex)
|
||||||
|
if (column.columns) {
|
||||||
|
column.columns = decorateColumnTree(
|
||||||
|
column.columns,
|
||||||
|
defaultColumn,
|
||||||
|
column,
|
||||||
|
depth + 1
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return column
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the header groups from the bottom up
|
||||||
|
function makeHeaderGroups(columns, maxDepth, defaultColumn) {
|
||||||
|
const headerGroups = []
|
||||||
|
|
||||||
|
const removeChildColumns = column => {
|
||||||
|
delete column.columns
|
||||||
|
if (column.parent) {
|
||||||
|
removeChildColumns(column.parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
columns.forEach(removeChildColumns)
|
||||||
|
|
||||||
|
const buildGroup = (columns, depth = 0) => {
|
||||||
|
const headerGroup = {
|
||||||
|
headers: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
const parentColumns = []
|
||||||
|
|
||||||
|
const hasParents = columns.some(col => col.parent)
|
||||||
|
|
||||||
|
columns.forEach(column => {
|
||||||
|
const isFirst = !parentColumns.length
|
||||||
|
let latestParentColumn = [...parentColumns].reverse()[0]
|
||||||
|
|
||||||
|
// If the column has a parent, add it if necessary
|
||||||
|
if (column.parent) {
|
||||||
|
if (isFirst || latestParentColumn.originalID !== column.parent.id) {
|
||||||
|
parentColumns.push({
|
||||||
|
...column.parent,
|
||||||
|
originalID: column.parent.id,
|
||||||
|
id: [column.parent.id, parentColumns.length].join('_'),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else if (hasParents) {
|
||||||
|
// If other columns have parents, add a place holder if necessary
|
||||||
|
const placeholderColumn = decorateColumn(
|
||||||
|
{
|
||||||
|
originalID: [column.id, 'placeholder', maxDepth - depth].join('_'),
|
||||||
|
id: [
|
||||||
|
column.id,
|
||||||
|
'placeholder',
|
||||||
|
maxDepth - depth,
|
||||||
|
parentColumns.length,
|
||||||
|
].join('_'),
|
||||||
|
},
|
||||||
|
defaultColumn
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
isFirst ||
|
||||||
|
latestParentColumn.originalID !== placeholderColumn.originalID
|
||||||
|
) {
|
||||||
|
parentColumns.push(placeholderColumn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establish the new columns[] relationship on the parent
|
||||||
|
if (column.parent || hasParents) {
|
||||||
|
latestParentColumn = [...parentColumns].reverse()[0]
|
||||||
|
latestParentColumn.columns = latestParentColumn.columns || []
|
||||||
|
if (!latestParentColumn.columns.includes(column)) {
|
||||||
|
latestParentColumn.columns.push(column)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
headerGroup.headers.push(column)
|
||||||
|
})
|
||||||
|
|
||||||
|
headerGroups.push(headerGroup)
|
||||||
|
|
||||||
|
if (parentColumns.length) {
|
||||||
|
buildGroup(parentColumns)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buildGroup(columns)
|
||||||
|
|
||||||
|
return headerGroups.reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useColumns = props => {
|
export const useColumns = props => {
|
||||||
const {
|
const {
|
||||||
debug,
|
debug,
|
||||||
columns: userColumns,
|
columns: userColumns,
|
||||||
state: [{ groupBy }]
|
defaultColumn = {},
|
||||||
|
state: [{ groupBy }],
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
PropTypes.checkPropTypes(propTypes, props, 'property', 'useColumns')
|
PropTypes.checkPropTypes(propTypes, props, 'property', 'useColumns')
|
||||||
@@ -26,86 +176,37 @@ export const useColumns = props => {
|
|||||||
if (debug) console.info('getColumns')
|
if (debug) console.info('getColumns')
|
||||||
|
|
||||||
// Decorate All the columns
|
// Decorate All the columns
|
||||||
let columnTree = decorateColumnTree(userColumns)
|
let columnTree = decorateColumnTree(userColumns, defaultColumn)
|
||||||
|
|
||||||
// Get the flat list of all columns
|
// Get the flat list of all columns
|
||||||
let columns = flattenBy(columnTree, 'columns')
|
let columns = flattenBy(columnTree, 'columns')
|
||||||
|
|
||||||
columns = [
|
columns = [
|
||||||
...groupBy.map(g => columns.find(col => col.id === g)),
|
...groupBy.map(g => columns.find(col => col.id === g)),
|
||||||
...columns.filter(col => !groupBy.includes(col.id))
|
...columns.filter(col => !groupBy.includes(col.id)),
|
||||||
]
|
]
|
||||||
|
|
||||||
// Get headerGroups
|
// Get headerGroups
|
||||||
const headerGroups = makeHeaderGroups(columns, findMaxDepth(columnTree))
|
const headerGroups = makeHeaderGroups(
|
||||||
|
columns,
|
||||||
|
findMaxDepth(columnTree),
|
||||||
|
defaultColumn
|
||||||
|
)
|
||||||
|
|
||||||
const headers = flattenBy(headerGroups, 'headers')
|
const headers = flattenBy(headerGroups, 'headers')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
columns,
|
columns,
|
||||||
headerGroups,
|
headerGroups,
|
||||||
headers
|
headers,
|
||||||
}
|
}
|
||||||
}, [groupBy, userColumns])
|
}, [debug, defaultColumn, groupBy, userColumns])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...props,
|
...props,
|
||||||
columns,
|
columns,
|
||||||
headerGroups,
|
headerGroups,
|
||||||
headers
|
headers,
|
||||||
}
|
|
||||||
|
|
||||||
// Find the depth of the columns
|
|
||||||
function findMaxDepth(columns, depth = 0) {
|
|
||||||
return columns.reduce((prev, curr) => {
|
|
||||||
if (curr.columns) {
|
|
||||||
return Math.max(prev, findMaxDepth(curr.columns, depth + 1))
|
|
||||||
}
|
|
||||||
return depth
|
|
||||||
}, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
function decorateColumn(column, parent) {
|
|
||||||
// First check for string accessor
|
|
||||||
let { id, accessor, Header } = column
|
|
||||||
|
|
||||||
if (typeof accessor === 'string') {
|
|
||||||
id = id || accessor
|
|
||||||
const accessorString = accessor
|
|
||||||
accessor = row => getBy(row, accessorString)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!id && typeof Header === 'string') {
|
|
||||||
id = Header
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!id) {
|
|
||||||
// Accessor, but no column id? This is bad.
|
|
||||||
console.error(column)
|
|
||||||
throw new Error('A column id is required!')
|
|
||||||
}
|
|
||||||
|
|
||||||
column = {
|
|
||||||
Header: '',
|
|
||||||
Cell: cell => cell.value,
|
|
||||||
show: true,
|
|
||||||
...column,
|
|
||||||
id,
|
|
||||||
accessor,
|
|
||||||
parent
|
|
||||||
}
|
|
||||||
|
|
||||||
return column
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build the visible columns, headers and flat column list
|
|
||||||
function decorateColumnTree(columns, parent, depth = 0) {
|
|
||||||
return columns.map(column => {
|
|
||||||
column = decorateColumn(column, parent)
|
|
||||||
if (column.columns) {
|
|
||||||
column.columns = decorateColumnTree(column.columns, column, depth + 1)
|
|
||||||
}
|
|
||||||
return column
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function flattenBy(columns, childKey) {
|
function flattenBy(columns, childKey) {
|
||||||
@@ -125,81 +226,4 @@ export const useColumns = props => {
|
|||||||
|
|
||||||
return flatColumns
|
return flatColumns
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the header groups from the bottom up
|
|
||||||
function makeHeaderGroups(columns, maxDepth) {
|
|
||||||
const headerGroups = []
|
|
||||||
|
|
||||||
const removeChildColumns = column => {
|
|
||||||
delete column.columns
|
|
||||||
if (column.parent) {
|
|
||||||
removeChildColumns(column.parent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
columns.forEach(removeChildColumns)
|
|
||||||
|
|
||||||
const buildGroup = (columns, depth = 0) => {
|
|
||||||
const headerGroup = {
|
|
||||||
headers: []
|
|
||||||
}
|
|
||||||
|
|
||||||
const parentColumns = []
|
|
||||||
|
|
||||||
const hasParents = columns.some(col => col.parent)
|
|
||||||
|
|
||||||
columns.forEach(column => {
|
|
||||||
const isFirst = !parentColumns.length
|
|
||||||
let latestParentColumn = [...parentColumns].reverse()[0]
|
|
||||||
|
|
||||||
// If the column has a parent, add it if necessary
|
|
||||||
if (column.parent) {
|
|
||||||
if (isFirst || latestParentColumn.originalID !== column.parent.id) {
|
|
||||||
parentColumns.push({
|
|
||||||
...column.parent,
|
|
||||||
originalID: column.parent.id,
|
|
||||||
id: [column.parent.id, parentColumns.length].join('_')
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else if (hasParents) {
|
|
||||||
// If other columns have parents, add a place holder if necessary
|
|
||||||
const placeholderColumn = decorateColumn({
|
|
||||||
originalID: [column.id, 'placeholder', maxDepth - depth].join('_'),
|
|
||||||
id: [
|
|
||||||
column.id,
|
|
||||||
'placeholder',
|
|
||||||
maxDepth - depth,
|
|
||||||
parentColumns.length
|
|
||||||
].join('_')
|
|
||||||
})
|
|
||||||
if (
|
|
||||||
isFirst ||
|
|
||||||
latestParentColumn.originalID !== placeholderColumn.originalID
|
|
||||||
) {
|
|
||||||
parentColumns.push(placeholderColumn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Establish the new columns[] relationship on the parent
|
|
||||||
if (column.parent || hasParents) {
|
|
||||||
latestParentColumn = [...parentColumns].reverse()[0]
|
|
||||||
latestParentColumn.columns = latestParentColumn.columns || []
|
|
||||||
if (!latestParentColumn.columns.includes(column)) {
|
|
||||||
latestParentColumn.columns.push(column)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
headerGroup.headers.push(column)
|
|
||||||
})
|
|
||||||
|
|
||||||
headerGroups.push(headerGroup)
|
|
||||||
|
|
||||||
if (parentColumns.length) {
|
|
||||||
buildGroup(parentColumns)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buildGroup(columns)
|
|
||||||
|
|
||||||
return headerGroups.reverse()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { useMemo } from 'react'
|
import React from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
subRowsKey: PropTypes.string
|
subRowsKey: PropTypes.string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useRows = props => {
|
export const useRows = props => {
|
||||||
@@ -10,7 +10,7 @@ export const useRows = props => {
|
|||||||
|
|
||||||
const { debug, columns, subRowsKey = 'subRows', data } = props
|
const { debug, columns, subRowsKey = 'subRows', data } = props
|
||||||
|
|
||||||
const accessedRows = useMemo(() => {
|
const accessedRows = React.useMemo(() => {
|
||||||
if (debug) console.info('getAccessedRows')
|
if (debug) console.info('getAccessedRows')
|
||||||
|
|
||||||
// Access the row's data
|
// Access the row's data
|
||||||
@@ -26,9 +26,10 @@ export const useRows = props => {
|
|||||||
const row = {
|
const row = {
|
||||||
original,
|
original,
|
||||||
index: i,
|
index: i,
|
||||||
|
path: [i], // used to create a key for each row even if not nested
|
||||||
subRows,
|
subRows,
|
||||||
depth,
|
depth,
|
||||||
cells: [{}] // This is a dummy cell
|
cells: [{}], // This is a dummy cell
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override common array functions (and the dummy cell's getCellProps function)
|
// Override common array functions (and the dummy cell's getCellProps function)
|
||||||
@@ -56,10 +57,10 @@ export const useRows = props => {
|
|||||||
|
|
||||||
// Use the resolved data
|
// Use the resolved data
|
||||||
return data.map((d, i) => accessRow(d, i))
|
return data.map((d, i) => accessRow(d, i))
|
||||||
}, [data, columns])
|
}, [debug, data, subRowsKey, columns])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...props,
|
...props,
|
||||||
rows: accessedRows
|
rows: accessedRows,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-36
@@ -1,24 +1,32 @@
|
|||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
//
|
//
|
||||||
import { flexRender, applyHooks, applyPropHooks, mergeProps } from '../utils'
|
import { applyHooks, applyPropHooks, mergeProps, flexRender } from '../utils'
|
||||||
|
|
||||||
import { useTableState } from './useTableState'
|
import { useTableState } from './useTableState'
|
||||||
|
import { useColumns } from './useColumns'
|
||||||
const renderErr =
|
import { useRows } from './useRows'
|
||||||
'You must specify a render "type". This could be "Header", "Filter", or any other custom renderers you have set on your column.'
|
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
// General
|
// General
|
||||||
data: PropTypes.array.isRequired,
|
data: PropTypes.array.isRequired,
|
||||||
debug: PropTypes.bool
|
debug: PropTypes.bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const renderErr =
|
||||||
|
'You must specify a valid render component. This could be "column.Cell", "column.Header", "column.Filter", "column.Aggregated" or any other custom renderer component.'
|
||||||
|
|
||||||
export const useTable = (props, ...plugins) => {
|
export const useTable = (props, ...plugins) => {
|
||||||
// Validate props
|
// Validate props
|
||||||
PropTypes.checkPropTypes(propTypes, props, 'property', 'useTable')
|
PropTypes.checkPropTypes(propTypes, props, 'property', 'useTable')
|
||||||
|
|
||||||
// Destructure props
|
// Destructure props
|
||||||
let { data = [], state: userState, debug } = props
|
let {
|
||||||
|
data,
|
||||||
|
state: userState,
|
||||||
|
useColumns: userUseColumns = useColumns,
|
||||||
|
useRows: userUseRows = useRows,
|
||||||
|
debug,
|
||||||
|
} = props
|
||||||
|
|
||||||
debug = process.env.NODE_ENV === 'production' ? false : debug
|
debug = process.env.NODE_ENV === 'production' ? false : debug
|
||||||
|
|
||||||
@@ -36,12 +44,11 @@ export const useTable = (props, ...plugins) => {
|
|||||||
headerGroups: [],
|
headerGroups: [],
|
||||||
rows: [],
|
rows: [],
|
||||||
row: [],
|
row: [],
|
||||||
renderableRows: [],
|
|
||||||
getTableProps: [],
|
getTableProps: [],
|
||||||
getRowProps: [],
|
getRowProps: [],
|
||||||
getHeaderRowProps: [],
|
getHeaderGroupProps: [],
|
||||||
getHeaderProps: [],
|
getHeaderProps: [],
|
||||||
getCellProps: []
|
getCellProps: [],
|
||||||
}
|
}
|
||||||
|
|
||||||
// The initial api
|
// The initial api
|
||||||
@@ -49,50 +56,60 @@ export const useTable = (props, ...plugins) => {
|
|||||||
...props,
|
...props,
|
||||||
data,
|
data,
|
||||||
state,
|
state,
|
||||||
hooks
|
hooks,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debug) console.time('hooks')
|
if (debug) console.time('hooks')
|
||||||
// Loop through plugins to build the api out
|
// Loop through plugins to build the api out
|
||||||
api = plugins.filter(Boolean).reduce((prev, next) => next(prev), api)
|
api = [userUseColumns, userUseRows, ...plugins]
|
||||||
|
.filter(Boolean)
|
||||||
|
.reduce((prev, next) => next(prev), api)
|
||||||
if (debug) console.timeEnd('hooks')
|
if (debug) console.timeEnd('hooks')
|
||||||
|
|
||||||
// Run the beforeRender hook
|
// Determine column visibility
|
||||||
if (debug) console.time('hooks.beforeRender')
|
api.columns.forEach(column => {
|
||||||
applyHooks(api.hooks.beforeRender, undefined, api)
|
column.visible =
|
||||||
if (debug) console.timeEnd('hooks.beforeRender')
|
typeof column.show === 'function' ? column.show(api) : !!column.show
|
||||||
|
})
|
||||||
|
|
||||||
|
// Allow hooks to decorate columns
|
||||||
if (debug) console.time('hooks.columns')
|
if (debug) console.time('hooks.columns')
|
||||||
api.columns = applyHooks(api.hooks.columns, api.columns, api)
|
api.columns = applyHooks(api.hooks.columns, api.columns, api)
|
||||||
if (debug) console.timeEnd('hooks.columns')
|
if (debug) console.timeEnd('hooks.columns')
|
||||||
|
|
||||||
|
// Allow hooks to decorate headers
|
||||||
if (debug) console.time('hooks.headers')
|
if (debug) console.time('hooks.headers')
|
||||||
api.headers = applyHooks(api.hooks.headers, api.headers, api)
|
api.headers = applyHooks(api.hooks.headers, api.headers, api)
|
||||||
if (debug) console.timeEnd('hooks.headers')
|
if (debug) console.timeEnd('hooks.headers')
|
||||||
;[...api.columns, ...api.headers].forEach(column => {
|
;[...api.columns, ...api.headers].forEach(column => {
|
||||||
// Give columns/headers rendering power
|
// Give columns/headers rendering power
|
||||||
column.render = (type, userProps = {}) => {
|
column.render = (type, userProps = {}) => {
|
||||||
if (!type) {
|
const Comp = typeof type === 'string' ? column[type] : type
|
||||||
|
|
||||||
|
if (typeof Comp === 'undefined') {
|
||||||
throw new Error(renderErr)
|
throw new Error(renderErr)
|
||||||
}
|
}
|
||||||
return flexRender(column[type], {
|
|
||||||
|
return flexRender(Comp, {
|
||||||
...api,
|
...api,
|
||||||
...column,
|
...column,
|
||||||
...userProps
|
...userProps,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give columns/headers getHeaderProps
|
// Give columns/headers a default getHeaderProps
|
||||||
column.getHeaderProps = props =>
|
column.getHeaderProps = props =>
|
||||||
mergeProps(
|
mergeProps(
|
||||||
{
|
{
|
||||||
key: ['header', column.id].join('_')
|
key: ['header', column.id].join('_'),
|
||||||
|
colSpan: column.columns ? column.columns.length : 1,
|
||||||
},
|
},
|
||||||
applyPropHooks(api.hooks.getHeaderProps, column, api),
|
applyPropHooks(api.hooks.getHeaderProps, column, api),
|
||||||
props
|
props
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Allow hooks to decorate headerGroups
|
||||||
if (debug) console.time('hooks.headerGroups')
|
if (debug) console.time('hooks.headerGroups')
|
||||||
api.headerGroups = applyHooks(
|
api.headerGroups = applyHooks(
|
||||||
api.hooks.headerGroups,
|
api.hooks.headerGroups,
|
||||||
@@ -116,12 +133,12 @@ export const useTable = (props, ...plugins) => {
|
|||||||
|
|
||||||
// Give headerGroups getRowProps
|
// Give headerGroups getRowProps
|
||||||
if (headerGroup.headers.length) {
|
if (headerGroup.headers.length) {
|
||||||
headerGroup.getRowProps = (props = {}) =>
|
headerGroup.getHeaderGroupProps = (props = {}) =>
|
||||||
mergeProps(
|
mergeProps(
|
||||||
{
|
{
|
||||||
key: [`header${i}`].join('_')
|
key: [`header${i}`].join('_'),
|
||||||
},
|
},
|
||||||
applyPropHooks(api.hooks.getHeaderRowProps, headerGroup, api),
|
applyPropHooks(api.hooks.getHeaderGroupProps, headerGroup, api),
|
||||||
props
|
props
|
||||||
)
|
)
|
||||||
return true
|
return true
|
||||||
@@ -143,8 +160,8 @@ export const useTable = (props, ...plugins) => {
|
|||||||
const { path } = row
|
const { path } = row
|
||||||
row.getRowProps = props =>
|
row.getRowProps = props =>
|
||||||
mergeProps(
|
mergeProps(
|
||||||
{ key: ['row', path].join('_') },
|
{ key: ['row', ...path].join('_') },
|
||||||
applyHooks(api.hooks.getRowProps, row, api),
|
applyPropHooks(api.hooks.getRowProps, row, api),
|
||||||
props
|
props
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -158,30 +175,33 @@ export const useTable = (props, ...plugins) => {
|
|||||||
const cell = {
|
const cell = {
|
||||||
column,
|
column,
|
||||||
row,
|
row,
|
||||||
value: row.values[column.id]
|
value: row.values[column.id],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Give each cell a getCellProps base
|
||||||
cell.getCellProps = props => {
|
cell.getCellProps = props => {
|
||||||
const columnPathStr = [path, column.id].join('_')
|
const columnPathStr = [path, column.id].join('_')
|
||||||
return mergeProps(
|
return mergeProps(
|
||||||
{
|
{
|
||||||
key: ['cell', columnPathStr].join('_')
|
key: ['cell', columnPathStr].join('_'),
|
||||||
},
|
},
|
||||||
applyPropHooks(api.hooks.getCellProps, cell, api),
|
applyPropHooks(api.hooks.getCellProps, cell, api),
|
||||||
props
|
props
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Give each cell a renderer function (supports multiple renderers)
|
||||||
cell.render = (type, userProps = {}) => {
|
cell.render = (type, userProps = {}) => {
|
||||||
if (!type) {
|
const Comp = typeof type === 'string' ? column[type] : type
|
||||||
throw new Error(
|
|
||||||
'You must specify a render "type". This could be "Cell", "Header", "Filter", "Aggregated" or any other custom renderers you have set on your column.'
|
if (typeof Comp === 'undefined') {
|
||||||
)
|
throw new Error(renderErr)
|
||||||
}
|
}
|
||||||
return flexRender(column[type], {
|
|
||||||
|
return flexRender(Comp, {
|
||||||
...api,
|
...api,
|
||||||
...cell,
|
...cell,
|
||||||
...userProps
|
...userProps,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,8 +212,5 @@ export const useTable = (props, ...plugins) => {
|
|||||||
api.getTableProps = userProps =>
|
api.getTableProps = userProps =>
|
||||||
mergeProps(applyPropHooks(api.hooks.getTableProps, api), userProps)
|
mergeProps(applyPropHooks(api.hooks.getTableProps, api), userProps)
|
||||||
|
|
||||||
api.getRowProps = userProps =>
|
|
||||||
mergeProps(applyPropHooks(api.hooks.getRowProps, api), userProps)
|
|
||||||
|
|
||||||
return api
|
return api
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-16
@@ -1,4 +1,6 @@
|
|||||||
import { useState, useMemo } from 'react'
|
import React from 'react'
|
||||||
|
//
|
||||||
|
import { types } from '../actions'
|
||||||
|
|
||||||
export const defaultState = {}
|
export const defaultState = {}
|
||||||
|
|
||||||
@@ -6,29 +8,49 @@ const defaultReducer = (old, newState) => newState
|
|||||||
|
|
||||||
export const useTableState = (
|
export const useTableState = (
|
||||||
initialState = {},
|
initialState = {},
|
||||||
overrides = {},
|
overrides,
|
||||||
{ reducer = defaultReducer, useState: userUseState = useState } = {}
|
{ reducer = defaultReducer, useState: userUseState = React.useState } = {}
|
||||||
) => {
|
) => {
|
||||||
let [state, setState] = userUseState({
|
let [state, setState] = userUseState({
|
||||||
...defaultState,
|
...defaultState,
|
||||||
...initialState
|
...initialState,
|
||||||
})
|
})
|
||||||
|
|
||||||
const overriddenState = useMemo(() => {
|
const overriddenState = React.useMemo(() => {
|
||||||
const newState = {
|
const newState = {
|
||||||
...state
|
...state,
|
||||||
|
}
|
||||||
|
if (overrides) {
|
||||||
|
Object.keys(overrides).forEach(key => {
|
||||||
|
newState[key] = overrides[key]
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Object.keys(overrides).forEach(key => {
|
|
||||||
newState[key] = overrides[key]
|
|
||||||
})
|
|
||||||
return newState
|
return newState
|
||||||
}, [state, ...Object.values(overrides)])
|
}, [overrides, state])
|
||||||
|
|
||||||
const reducedSetState = (updater, type) =>
|
const overriddenStateRef = React.useRef()
|
||||||
setState(old => {
|
overriddenStateRef.current = overriddenState
|
||||||
const newState = updater(old)
|
|
||||||
return reducer(old, newState, type)
|
|
||||||
})
|
|
||||||
|
|
||||||
return [overriddenState, reducedSetState]
|
const reducedSetState = React.useCallback(
|
||||||
|
(updater, type) => {
|
||||||
|
if (!types[type]) {
|
||||||
|
console.info({
|
||||||
|
stateUpdaterFn: updater,
|
||||||
|
actionType: type,
|
||||||
|
currentState: overriddenStateRef.current,
|
||||||
|
})
|
||||||
|
throw new Error('Detected an unknown table action! (Details Above)')
|
||||||
|
}
|
||||||
|
return setState(old => {
|
||||||
|
const newState = updater(old)
|
||||||
|
return reducer(old, newState, type)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
[reducer, setState]
|
||||||
|
)
|
||||||
|
|
||||||
|
return React.useMemo(() => [overriddenState, reducedSetState], [
|
||||||
|
overriddenState,
|
||||||
|
reducedSetState,
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-9
@@ -1,12 +1,14 @@
|
|||||||
|
import * as utils from './utils'
|
||||||
|
export { utils }
|
||||||
export { useTable } from './hooks/useTable'
|
export { useTable } from './hooks/useTable'
|
||||||
export { useColumns } from './hooks/useColumns'
|
export { useColumns } from './hooks/useColumns'
|
||||||
export { useRows } from './hooks/useRows'
|
export { useRows } from './hooks/useRows'
|
||||||
export { useExpanded } from './hooks/useExpanded'
|
export { useTableState, defaultState } from './hooks/useTableState'
|
||||||
export { useFilters } from './hooks/useFilters'
|
export { useExpanded } from './plugin-hooks/useExpanded'
|
||||||
export { useGroupBy } from './hooks/useGroupBy'
|
export { useFilters } from './plugin-hooks/useFilters'
|
||||||
export { useSortBy } from './hooks/useSortBy'
|
export { useGroupBy } from './plugin-hooks/useGroupBy'
|
||||||
export { usePagination } from './hooks/usePagination'
|
export { useSortBy } from './plugin-hooks/useSortBy'
|
||||||
export { useTableState } from './hooks/useTableState'
|
export { usePagination } from './plugin-hooks/usePagination'
|
||||||
export { useFlexLayout } from './hooks/useFlexLayout'
|
export { useFlexLayout } from './plugin-hooks/useFlexLayout'
|
||||||
export { useTokenPagination } from './hooks/useTokenPagination'
|
export { useTokenPagination } from './plugin-hooks/useTokenPagination'
|
||||||
export { actions } from './actions'
|
export { actions, addActions } from './actions'
|
||||||
|
|||||||
@@ -3,17 +3,15 @@ import PropTypes from 'prop-types'
|
|||||||
|
|
||||||
import { getBy, getFirstDefined, setBy } from '../utils'
|
import { getBy, getFirstDefined, setBy } from '../utils'
|
||||||
import { addActions, actions } from '../actions'
|
import { addActions, actions } from '../actions'
|
||||||
import { defaultState } from './useTableState'
|
import { defaultState } from '../hooks/useTableState'
|
||||||
|
|
||||||
defaultState.expanded = {}
|
defaultState.expanded = {}
|
||||||
|
|
||||||
addActions({
|
addActions('toggleExpanded', 'useExpanded')
|
||||||
toggleExpanded: '__toggleExpanded__',
|
|
||||||
useExpanded: '__useExpanded__'
|
|
||||||
})
|
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
expandedKey: PropTypes.string
|
manualExpandedKey: PropTypes.string,
|
||||||
|
paginateSubRows: PropTypes.bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useExpanded = props => {
|
export const useExpanded = props => {
|
||||||
@@ -21,11 +19,11 @@ export const useExpanded = props => {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
debug,
|
debug,
|
||||||
columns,
|
|
||||||
rows,
|
rows,
|
||||||
expandedKey = 'expanded',
|
manualExpandedKey = 'expanded',
|
||||||
hooks,
|
hooks,
|
||||||
state: [{ expanded }, setState]
|
state: [{ expanded }, setState],
|
||||||
|
paginateSubRows = true,
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
const toggleExpandedByPath = (path, set) => {
|
const toggleExpandedByPath = (path, set) => {
|
||||||
@@ -35,7 +33,7 @@ export const useExpanded = props => {
|
|||||||
set = getFirstDefined(set, !existing)
|
set = getFirstDefined(set, !existing)
|
||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
expanded: setBy(expanded, path, set)
|
expanded: setBy(expanded, path, set),
|
||||||
}
|
}
|
||||||
}, actions.toggleExpanded)
|
}, actions.toggleExpanded)
|
||||||
}
|
}
|
||||||
@@ -43,6 +41,7 @@ export const useExpanded = props => {
|
|||||||
hooks.row.push(row => {
|
hooks.row.push(row => {
|
||||||
const { path } = row
|
const { path } = row
|
||||||
row.toggleExpanded = set => toggleExpandedByPath(path, set)
|
row.toggleExpanded = set => toggleExpandedByPath(path, set)
|
||||||
|
return row
|
||||||
})
|
})
|
||||||
|
|
||||||
const expandedRows = useMemo(() => {
|
const expandedRows = useMemo(() => {
|
||||||
@@ -52,27 +51,32 @@ export const useExpanded = props => {
|
|||||||
|
|
||||||
// Here we do some mutation, but it's the last stage in the
|
// Here we do some mutation, but it's the last stage in the
|
||||||
// immutable process so this is safe
|
// immutable process so this is safe
|
||||||
const handleRow = (row, index, depth = 0, parentPath = []) => {
|
const handleRow = (row, depth = 0, parentPath = []) => {
|
||||||
// Compute some final state for the row
|
// Compute some final state for the row
|
||||||
const path = [...parentPath, index]
|
const path = [...parentPath, row.index]
|
||||||
|
|
||||||
row.path = path
|
row.path = path
|
||||||
row.depth = depth
|
row.depth = depth
|
||||||
|
|
||||||
row.isExpanded =
|
row.isExpanded =
|
||||||
(row.original && row.original[expandedKey]) || getBy(expanded, path)
|
(row.original && row.original[manualExpandedKey]) ||
|
||||||
|
getBy(expanded, path)
|
||||||
|
|
||||||
expandedRows.push(row)
|
if (paginateSubRows || (!paginateSubRows && row.depth === 0)) {
|
||||||
|
expandedRows.push(row)
|
||||||
|
}
|
||||||
|
|
||||||
if (row.isExpanded && row.subRows && row.subRows.length) {
|
if (row.isExpanded && row.subRows && row.subRows.length) {
|
||||||
row.subRows.forEach((row, i) => handleRow(row, i, depth + 1, path))
|
row.subRows.forEach((row, i) => handleRow(row, depth + 1, path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return row
|
||||||
}
|
}
|
||||||
|
|
||||||
rows.forEach((row, i) => handleRow(row, i))
|
rows.forEach(row => handleRow(row))
|
||||||
|
|
||||||
return expandedRows
|
return expandedRows
|
||||||
}, [rows, expanded, columns])
|
}, [debug, rows, manualExpandedKey, expanded, paginateSubRows])
|
||||||
|
|
||||||
const expandedDepth = findExpandedDepth(expanded)
|
const expandedDepth = findExpandedDepth(expanded)
|
||||||
|
|
||||||
@@ -80,7 +84,7 @@ export const useExpanded = props => {
|
|||||||
...props,
|
...props,
|
||||||
toggleExpandedByPath,
|
toggleExpandedByPath,
|
||||||
expandedDepth,
|
expandedDepth,
|
||||||
rows: expandedRows
|
rows: expandedRows,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
|
|
||||||
import { defaultFilterFn, getFirstDefined } from '../utils'
|
import { getFirstDefined, isFunction } from '../utils'
|
||||||
|
import * as filterTypes from '../filterTypes'
|
||||||
import { addActions, actions } from '../actions'
|
import { addActions, actions } from '../actions'
|
||||||
import { defaultState } from './useTableState'
|
import { defaultState } from '../hooks/useTableState'
|
||||||
|
|
||||||
defaultState.filters = {}
|
defaultState.filters = {}
|
||||||
addActions({
|
|
||||||
setFilter: '__setFilter__',
|
addActions('setFilter', 'setAllFilters')
|
||||||
setAllFilters: '__setAllFilters__'
|
|
||||||
})
|
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
// General
|
// General
|
||||||
@@ -18,12 +17,12 @@ const propTypes = {
|
|||||||
filterFn: PropTypes.func,
|
filterFn: PropTypes.func,
|
||||||
filterAll: PropTypes.bool,
|
filterAll: PropTypes.bool,
|
||||||
canFilter: PropTypes.bool,
|
canFilter: PropTypes.bool,
|
||||||
Filter: PropTypes.any
|
Filter: PropTypes.any,
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
|
|
||||||
filterFn: PropTypes.func,
|
filterFn: PropTypes.func,
|
||||||
manualFilters: PropTypes.bool
|
manualFilters: PropTypes.bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useFilters = props => {
|
export const useFilters = props => {
|
||||||
@@ -33,26 +32,14 @@ export const useFilters = props => {
|
|||||||
debug,
|
debug,
|
||||||
rows,
|
rows,
|
||||||
columns,
|
columns,
|
||||||
filterFn = defaultFilterFn,
|
filterTypes: userFilterTypes,
|
||||||
|
defaultFilter = filterTypes.text,
|
||||||
manualFilters,
|
manualFilters,
|
||||||
disableFilters,
|
disableFilters,
|
||||||
hooks,
|
hooks,
|
||||||
state: [{ filters }, setState]
|
state: [{ filters }, setState],
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
columns.forEach(column => {
|
|
||||||
const { id, accessor, canFilter } = column
|
|
||||||
column.canFilter = accessor
|
|
||||||
? getFirstDefined(
|
|
||||||
canFilter,
|
|
||||||
disableFilters === true ? false : undefined,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
: false
|
|
||||||
// Was going to add this to the filter hook
|
|
||||||
column.filterValue = filters[id]
|
|
||||||
})
|
|
||||||
|
|
||||||
const setFilter = (id, val) => {
|
const setFilter = (id, val) => {
|
||||||
return setState(old => {
|
return setState(old => {
|
||||||
if (typeof val === 'undefined') {
|
if (typeof val === 'undefined') {
|
||||||
@@ -60,8 +47,8 @@ export const useFilters = props => {
|
|||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
filters: {
|
filters: {
|
||||||
...rest
|
...rest,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,8 +56,8 @@ export const useFilters = props => {
|
|||||||
...old,
|
...old,
|
||||||
filters: {
|
filters: {
|
||||||
...filters,
|
...filters,
|
||||||
[id]: val
|
[id]: val,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}, actions.setFilter)
|
}, actions.setFilter)
|
||||||
}
|
}
|
||||||
@@ -79,17 +66,32 @@ export const useFilters = props => {
|
|||||||
return setState(old => {
|
return setState(old => {
|
||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
filters
|
filters,
|
||||||
}
|
}
|
||||||
}, actions.setAllFilters)
|
}, actions.setAllFilters)
|
||||||
}
|
}
|
||||||
|
|
||||||
hooks.columns.push(columns => {
|
hooks.columns.push(columns => {
|
||||||
columns.forEach(column => {
|
columns.forEach(column => {
|
||||||
if (column.canFilter) {
|
const { id, accessor, canFilter } = column
|
||||||
column.setFilter = val => setFilter(column.id, val)
|
|
||||||
}
|
// Determine if a column is filterable
|
||||||
|
column.canFilter = accessor
|
||||||
|
? getFirstDefined(
|
||||||
|
canFilter,
|
||||||
|
disableFilters === true ? false : undefined,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
: false
|
||||||
|
|
||||||
|
// Provide the column a way of updating the filter value
|
||||||
|
column.setFilter = val => setFilter(column.id, val)
|
||||||
|
|
||||||
|
// Provide the current filter value to the column for
|
||||||
|
// convenience
|
||||||
|
column.filterValue = filters[id]
|
||||||
})
|
})
|
||||||
|
|
||||||
return columns
|
return columns
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -114,15 +116,33 @@ export const useFilters = props => {
|
|||||||
return filteredSoFar
|
return filteredSoFar
|
||||||
}
|
}
|
||||||
|
|
||||||
const filterMethod = column.filterMethod || filterFn
|
// Look up filter functions in this order:
|
||||||
|
// column function
|
||||||
|
// column string lookup on user filters
|
||||||
|
// column string lookup on built-in filters
|
||||||
|
// default function
|
||||||
|
// default string lookup on user filters
|
||||||
|
// default string lookup on built-in filters
|
||||||
|
const filterMethod =
|
||||||
|
isFunction(column.filter) ||
|
||||||
|
(userFilterTypes || {})[column.filter] ||
|
||||||
|
filterTypes[column.filter] ||
|
||||||
|
isFunction(defaultFilter) ||
|
||||||
|
(userFilterTypes || {})[defaultFilter] ||
|
||||||
|
filterTypes[defaultFilter]
|
||||||
|
|
||||||
// If 'filterAll' is set to true, pass the entire dataset to the filter method
|
if (!filterMethod) {
|
||||||
if (column.filterAll) {
|
console.warn(
|
||||||
return filterMethod(filteredSoFar, columnID, filterValue, column)
|
`Could not find a valid 'column.filter' for column with the ID: ${
|
||||||
|
column.id
|
||||||
|
}.`
|
||||||
|
)
|
||||||
|
return filteredSoFar
|
||||||
}
|
}
|
||||||
return filteredSoFar.filter(row =>
|
|
||||||
filterMethod(row, columnID, filterValue, column)
|
// Pass the rows, id, filterValue and column to the filterMethod
|
||||||
)
|
// to get the filtered rows back
|
||||||
|
return filterMethod(filteredSoFar, columnID, filterValue, column)
|
||||||
},
|
},
|
||||||
rows
|
rows
|
||||||
)
|
)
|
||||||
@@ -134,7 +154,7 @@ export const useFilters = props => {
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...row,
|
...row,
|
||||||
subRows: filterRows(row.subRows)
|
subRows: filterRows(row.subRows),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -150,12 +170,20 @@ export const useFilters = props => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return filterRows(rows)
|
return filterRows(rows)
|
||||||
}, [rows, filters, manualFilters])
|
}, [
|
||||||
|
manualFilters,
|
||||||
|
filters,
|
||||||
|
debug,
|
||||||
|
rows,
|
||||||
|
columns,
|
||||||
|
userFilterTypes,
|
||||||
|
defaultFilter,
|
||||||
|
])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...props,
|
...props,
|
||||||
setFilter,
|
setFilter,
|
||||||
setAllFilters,
|
setAllFilters,
|
||||||
rows: filteredRows
|
rows: filteredRows,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import { getFirstDefined, sum } from '../utils'
|
|||||||
export const actions = {}
|
export const actions = {}
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
defaultFlex: PropTypes.number
|
defaultFlex: PropTypes.number,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useFlexLayout = props => {
|
export const useFlexLayout = props => {
|
||||||
@@ -16,19 +16,14 @@ export const useFlexLayout = props => {
|
|||||||
hooks: {
|
hooks: {
|
||||||
columns: columnsHooks,
|
columns: columnsHooks,
|
||||||
getRowProps,
|
getRowProps,
|
||||||
getHeaderRowProps,
|
getHeaderGroupProps,
|
||||||
getHeaderProps,
|
getHeaderProps,
|
||||||
getCellProps
|
getCellProps,
|
||||||
}
|
},
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
columnsHooks.push((columns, api) => {
|
columnsHooks.push((columns, api) => {
|
||||||
const visibleColumns = columns.filter(column => {
|
const visibleColumns = columns.filter(column => column.visible)
|
||||||
column.visible =
|
|
||||||
typeof column.show === 'function' ? column.show(api) : !!column.show
|
|
||||||
return column.visible
|
|
||||||
})
|
|
||||||
|
|
||||||
const columnMeasurements = {}
|
const columnMeasurements = {}
|
||||||
|
|
||||||
let sumWidth = 0
|
let sumWidth = 0
|
||||||
@@ -52,47 +47,33 @@ export const useFlexLayout = props => {
|
|||||||
const rowStyles = {
|
const rowStyles = {
|
||||||
style: {
|
style: {
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
minWidth: `${sumWidth}px`
|
minWidth: `${sumWidth}px`,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
api.rowStyles = rowStyles
|
api.rowStyles = rowStyles
|
||||||
|
|
||||||
getRowProps.push(() => rowStyles)
|
getRowProps.push(() => rowStyles)
|
||||||
getHeaderRowProps.push(() => rowStyles)
|
getHeaderGroupProps.push(() => rowStyles)
|
||||||
|
|
||||||
getHeaderProps.push(column => ({
|
getHeaderProps.push(column => ({
|
||||||
style: {
|
style: {
|
||||||
boxSizing: 'border-box',
|
boxSizing: 'border-box',
|
||||||
...getStylesForColumn(column, columnMeasurements, defaultFlex, api)
|
...getStylesForColumn(column, columnMeasurements, defaultFlex, api),
|
||||||
}
|
},
|
||||||
// [refKey]: el => {
|
|
||||||
// renderedCellInfoRef.current[key] = {
|
|
||||||
// column,
|
|
||||||
// el
|
|
||||||
// };
|
|
||||||
// },
|
|
||||||
}))
|
}))
|
||||||
|
|
||||||
getCellProps.push(cell => {
|
getCellProps.push(cell => {
|
||||||
return {
|
return {
|
||||||
style: {
|
style: {
|
||||||
// display: 'block',
|
|
||||||
// boxSizing: 'border-box',
|
|
||||||
...getStylesForColumn(
|
...getStylesForColumn(
|
||||||
cell.column,
|
cell.column,
|
||||||
columnMeasurements,
|
columnMeasurements,
|
||||||
defaultFlex,
|
defaultFlex,
|
||||||
undefined,
|
undefined,
|
||||||
api
|
api
|
||||||
)
|
),
|
||||||
}
|
},
|
||||||
// [refKey]: el => {
|
|
||||||
// renderedCellInfoRef.current[columnPathStr] = {
|
|
||||||
// column,
|
|
||||||
// el
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -115,7 +96,7 @@ function getStylesForColumn(column, columnMeasurements, defaultFlex, api) {
|
|||||||
return {
|
return {
|
||||||
flex: `${flex} 0 auto`,
|
flex: `${flex} 0 auto`,
|
||||||
width: `${width}px`,
|
width: `${width}px`,
|
||||||
maxWidth: `${maxWidth}px`
|
maxWidth: `${maxWidth}px`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +108,7 @@ function getSizesForColumn(
|
|||||||
) {
|
) {
|
||||||
if (columns) {
|
if (columns) {
|
||||||
columns = columns
|
columns = columns
|
||||||
|
.filter(col => col.show || col.visible)
|
||||||
.map(column =>
|
.map(column =>
|
||||||
getSizesForColumn(column, columnMeasurements, defaultFlex, api)
|
getSizesForColumn(column, columnMeasurements, defaultFlex, api)
|
||||||
)
|
)
|
||||||
@@ -143,7 +125,7 @@ function getSizesForColumn(
|
|||||||
return {
|
return {
|
||||||
flex,
|
flex,
|
||||||
width,
|
width,
|
||||||
maxWidth
|
maxWidth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,63 +135,6 @@ function getSizesForColumn(
|
|||||||
width === 'auto'
|
width === 'auto'
|
||||||
? columnMeasurements[id] || defaultFlex
|
? columnMeasurements[id] || defaultFlex
|
||||||
: getFirstDefined(width, minWidth, defaultFlex),
|
: getFirstDefined(width, minWidth, defaultFlex),
|
||||||
maxWidth
|
maxWidth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// const resetRefs = () => {
|
|
||||||
// if (debug) console.info("resetRefs");
|
|
||||||
// renderedCellInfoRef.current = {};
|
|
||||||
// };
|
|
||||||
|
|
||||||
// const calculateAutoWidths = () => {
|
|
||||||
// RAF(() => {
|
|
||||||
// const newColumnMeasurements = {};
|
|
||||||
// Object.values(renderedCellInfoRef.current).forEach(({ column, el }) => {
|
|
||||||
// if (!el) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// let measurement = 0;
|
|
||||||
|
|
||||||
// const measureChildren = children => {
|
|
||||||
// if (children) {
|
|
||||||
// [].slice.call(children).forEach(child => {
|
|
||||||
// measurement = Math.max(
|
|
||||||
// measurement,
|
|
||||||
// Math.ceil(child.offsetWidth) || 0
|
|
||||||
// );
|
|
||||||
// measureChildren(child.children);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// return measurement;
|
|
||||||
// };
|
|
||||||
|
|
||||||
// const parentDims = getElementDimensions(el);
|
|
||||||
// measureChildren(el.children);
|
|
||||||
|
|
||||||
// newColumnMeasurements[column.id] = Math.max(
|
|
||||||
// newColumnMeasurements[column.id] || 0,
|
|
||||||
// measurement + parentDims.paddingLeft + parentDims.paddingRight
|
|
||||||
// );
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const oldKeys = Object.keys(columnMeasurements);
|
|
||||||
// const newKeys = Object.keys(newColumnMeasurements);
|
|
||||||
|
|
||||||
// const needsUpdate =
|
|
||||||
// oldKeys.length !== newKeys.length ||
|
|
||||||
// oldKeys.some(key => {
|
|
||||||
// return columnMeasurements[key] !== newColumnMeasurements[key];
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (needsUpdate) {
|
|
||||||
// setState(old => {
|
|
||||||
// return {
|
|
||||||
// ...old,
|
|
||||||
// columnMeasurements: newColumnMeasurements
|
|
||||||
// };
|
|
||||||
// }, actions.updateAutoWidth);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// };
|
|
||||||
@@ -3,19 +3,17 @@ import PropTypes from 'prop-types'
|
|||||||
|
|
||||||
import * as aggregations from '../aggregations'
|
import * as aggregations from '../aggregations'
|
||||||
import { addActions, actions } from '../actions'
|
import { addActions, actions } from '../actions'
|
||||||
import { defaultState } from './useTableState'
|
import { defaultState } from '../hooks/useTableState'
|
||||||
import {
|
import {
|
||||||
mergeProps,
|
mergeProps,
|
||||||
applyPropHooks,
|
applyPropHooks,
|
||||||
defaultGroupByFn,
|
defaultGroupByFn,
|
||||||
getFirstDefined
|
getFirstDefined,
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
|
|
||||||
defaultState.groupBy = []
|
defaultState.groupBy = []
|
||||||
|
|
||||||
addActions({
|
addActions('toggleGroupBy')
|
||||||
toggleGroupBy: '__toggleGroupBy__'
|
|
||||||
})
|
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
// General
|
// General
|
||||||
@@ -23,12 +21,12 @@ const propTypes = {
|
|||||||
PropTypes.shape({
|
PropTypes.shape({
|
||||||
aggregate: PropTypes.func,
|
aggregate: PropTypes.func,
|
||||||
canGroupBy: PropTypes.bool,
|
canGroupBy: PropTypes.bool,
|
||||||
Aggregated: PropTypes.any
|
Aggregated: PropTypes.any,
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
groupByFn: PropTypes.func,
|
groupByFn: PropTypes.func,
|
||||||
manualGrouping: PropTypes.bool,
|
manualGrouping: PropTypes.bool,
|
||||||
aggregations: PropTypes.object
|
aggregations: PropTypes.object,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useGroupBy = props => {
|
export const useGroupBy = props => {
|
||||||
@@ -43,7 +41,7 @@ export const useGroupBy = props => {
|
|||||||
disableGrouping,
|
disableGrouping,
|
||||||
aggregations: userAggregations = {},
|
aggregations: userAggregations = {},
|
||||||
hooks,
|
hooks,
|
||||||
state: [{ groupBy }, setState]
|
state: [{ groupBy }, setState],
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
columns.forEach(column => {
|
columns.forEach(column => {
|
||||||
@@ -52,10 +50,10 @@ export const useGroupBy = props => {
|
|||||||
|
|
||||||
column.canGroupBy = accessor
|
column.canGroupBy = accessor
|
||||||
? getFirstDefined(
|
? getFirstDefined(
|
||||||
canGroupBy,
|
canGroupBy,
|
||||||
disableGrouping === true ? false : undefined,
|
disableGrouping === true ? false : undefined,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
: false
|
: false
|
||||||
|
|
||||||
column.Aggregated = column.Aggregated || column.Cell
|
column.Aggregated = column.Aggregated || column.Cell
|
||||||
@@ -68,12 +66,12 @@ export const useGroupBy = props => {
|
|||||||
if (resolvedToggle) {
|
if (resolvedToggle) {
|
||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
groupBy: [...groupBy, id]
|
groupBy: [...groupBy, id],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
groupBy: groupBy.filter(d => d !== id)
|
groupBy: groupBy.filter(d => d !== id),
|
||||||
}
|
}
|
||||||
}, actions.toggleGroupBy)
|
}, actions.toggleGroupBy)
|
||||||
}
|
}
|
||||||
@@ -97,14 +95,14 @@ export const useGroupBy = props => {
|
|||||||
{
|
{
|
||||||
onClick: canGroupBy
|
onClick: canGroupBy
|
||||||
? e => {
|
? e => {
|
||||||
e.persist()
|
e.persist()
|
||||||
column.toggleGroupBy()
|
column.toggleGroupBy()
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
style: {
|
style: {
|
||||||
cursor: canGroupBy ? 'pointer' : undefined
|
cursor: canGroupBy ? 'pointer' : undefined,
|
||||||
},
|
},
|
||||||
title: 'Toggle GroupBy'
|
title: 'Toggle GroupBy',
|
||||||
},
|
},
|
||||||
applyPropHooks(api.hooks.getGroupByToggleProps, column, api),
|
applyPropHooks(api.hooks.getGroupByToggleProps, column, api),
|
||||||
props
|
props
|
||||||
@@ -169,7 +167,7 @@ export const useGroupBy = props => {
|
|||||||
values,
|
values,
|
||||||
subRows,
|
subRows,
|
||||||
depth,
|
depth,
|
||||||
index
|
index,
|
||||||
}
|
}
|
||||||
return row
|
return row
|
||||||
}
|
}
|
||||||
@@ -180,10 +178,18 @@ export const useGroupBy = props => {
|
|||||||
|
|
||||||
// Assign the new data
|
// Assign the new data
|
||||||
return groupRecursively(rows, groupBy)
|
return groupRecursively(rows, groupBy)
|
||||||
}, [rows, groupBy, columns, manualGroupBy])
|
}, [
|
||||||
|
manualGroupBy,
|
||||||
|
groupBy,
|
||||||
|
debug,
|
||||||
|
rows,
|
||||||
|
columns,
|
||||||
|
userAggregations,
|
||||||
|
groupByFn,
|
||||||
|
])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...props,
|
...props,
|
||||||
rows: groupedRows
|
rows: groupedRows,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,33 @@
|
|||||||
import { useMemo, useLayoutEffect } from 'react'
|
import React from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
|
|
||||||
//
|
//
|
||||||
import { addActions, actions } from '../actions'
|
import { addActions, actions } from '../actions'
|
||||||
import { defaultState } from './useTableState'
|
import { defaultState } from '../hooks/useTableState'
|
||||||
|
|
||||||
defaultState.pageSize = 10
|
defaultState.pageSize = 10
|
||||||
defaultState.pageIndex = 0
|
defaultState.pageIndex = 0
|
||||||
|
|
||||||
addActions({
|
addActions('pageChange', 'pageSizeChange')
|
||||||
pageChange: '__pageChange__'
|
|
||||||
})
|
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
// General
|
// General
|
||||||
manualPagination: PropTypes.bool
|
manualPagination: PropTypes.bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SSR has issues with useLayoutEffect still, so pony-fill with useEffect during SSR
|
||||||
|
let useLayoutEffect =
|
||||||
|
typeof window !== 'undefined' && process.env.NODE_ENV === 'production'
|
||||||
|
? React.useLayoutEffect
|
||||||
|
: React.useEffect
|
||||||
|
|
||||||
export const usePagination = props => {
|
export const usePagination = props => {
|
||||||
PropTypes.checkPropTypes(propTypes, props, 'property', 'usePagination')
|
PropTypes.checkPropTypes(propTypes, props, 'property', 'usePagination')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
rows,
|
rows,
|
||||||
manualPagination,
|
manualPagination,
|
||||||
|
disablePageResetOnDataChange,
|
||||||
debug,
|
debug,
|
||||||
state: [
|
state: [
|
||||||
{
|
{
|
||||||
@@ -31,27 +36,34 @@ export const usePagination = props => {
|
|||||||
pageCount: userPageCount,
|
pageCount: userPageCount,
|
||||||
filters,
|
filters,
|
||||||
groupBy,
|
groupBy,
|
||||||
sortBy
|
sortBy,
|
||||||
},
|
},
|
||||||
setState
|
setState,
|
||||||
]
|
],
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
|
const pageOptions = React.useMemo(
|
||||||
|
() => [...new Array(userPageCount)].map((d, i) => i),
|
||||||
|
[userPageCount]
|
||||||
|
)
|
||||||
|
|
||||||
|
const rowDep = disablePageResetOnDataChange ? null : rows
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
setState(
|
setState(
|
||||||
old => ({
|
old => ({
|
||||||
...old,
|
...old,
|
||||||
pageIndex: 0
|
pageIndex: 0,
|
||||||
}),
|
}),
|
||||||
actions.pageChange
|
actions.pageChange
|
||||||
)
|
)
|
||||||
}, [filters, groupBy, sortBy])
|
}, [setState, rowDep, filters, groupBy, sortBy])
|
||||||
|
|
||||||
const { pages, pageCount } = useMemo(() => {
|
const { pages, pageCount } = React.useMemo(() => {
|
||||||
if (manualPagination) {
|
if (manualPagination) {
|
||||||
return {
|
return {
|
||||||
pages: [rows],
|
pages: [rows],
|
||||||
pageCount: userPageCount
|
pageCount: userPageCount,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (debug) console.info('getPages')
|
if (debug) console.info('getPages')
|
||||||
@@ -72,11 +84,10 @@ export const usePagination = props => {
|
|||||||
return {
|
return {
|
||||||
pages,
|
pages,
|
||||||
pageCount,
|
pageCount,
|
||||||
pageOptions
|
pageOptions,
|
||||||
}
|
}
|
||||||
}, [rows, pageSize, userPageCount])
|
}, [manualPagination, debug, rows, pageOptions, userPageCount, pageSize])
|
||||||
|
|
||||||
const pageOptions = [...new Array(pageCount)].map((d, i) => i)
|
|
||||||
const page = manualPagination ? rows : pages[pageIndex] || []
|
const page = manualPagination ? rows : pages[pageIndex] || []
|
||||||
const canPreviousPage = pageIndex > 0
|
const canPreviousPage = pageIndex > 0
|
||||||
const canNextPage = pageIndex < pageCount - 1
|
const canNextPage = pageIndex < pageCount - 1
|
||||||
@@ -89,7 +100,7 @@ export const usePagination = props => {
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
pageIndex
|
pageIndex,
|
||||||
}
|
}
|
||||||
}, actions.pageChange)
|
}, actions.pageChange)
|
||||||
}
|
}
|
||||||
@@ -109,21 +120,24 @@ export const usePagination = props => {
|
|||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
pageIndex,
|
pageIndex,
|
||||||
pageSize
|
pageSize,
|
||||||
}
|
}
|
||||||
}, actions.setPageSize)
|
}, actions.pageSizeChange)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...props,
|
...props,
|
||||||
pages,
|
pages,
|
||||||
pageOptions,
|
pageOptions,
|
||||||
|
pageCount,
|
||||||
page,
|
page,
|
||||||
canPreviousPage,
|
canPreviousPage,
|
||||||
canNextPage,
|
canNextPage,
|
||||||
gotoPage,
|
gotoPage,
|
||||||
previousPage,
|
previousPage,
|
||||||
nextPage,
|
nextPage,
|
||||||
setPageSize
|
setPageSize,
|
||||||
|
pageIndex,
|
||||||
|
pageSize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,34 +2,37 @@ import { useMemo } from 'react'
|
|||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
|
|
||||||
import { addActions, actions } from '../actions'
|
import { addActions, actions } from '../actions'
|
||||||
import { defaultState } from './useTableState'
|
import { defaultState } from '../hooks/useTableState'
|
||||||
|
import * as sortTypes from '../sortTypes'
|
||||||
import {
|
import {
|
||||||
mergeProps,
|
mergeProps,
|
||||||
applyPropHooks,
|
applyPropHooks,
|
||||||
getFirstDefined,
|
getFirstDefined,
|
||||||
defaultOrderByFn,
|
defaultOrderByFn,
|
||||||
defaultSortByFn
|
isFunction,
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
|
|
||||||
defaultState.sortBy = []
|
defaultState.sortBy = []
|
||||||
|
|
||||||
addActions({
|
addActions('sortByChange')
|
||||||
sortByChange: '__sortByChange__'
|
|
||||||
})
|
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
// General
|
// General
|
||||||
columns: PropTypes.arrayOf(
|
columns: PropTypes.arrayOf(
|
||||||
PropTypes.shape({
|
PropTypes.shape({
|
||||||
sortByFn: PropTypes.func,
|
sortBy: PropTypes.func,
|
||||||
efaultSortDesc: PropTypes.bool
|
defaultSortDesc: PropTypes.bool,
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
sortByFn: PropTypes.func,
|
orderByFn: PropTypes.func,
|
||||||
|
sortTypes: PropTypes.object,
|
||||||
|
defaultSortType: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
|
||||||
manualSorting: PropTypes.bool,
|
manualSorting: PropTypes.bool,
|
||||||
disableSorting: PropTypes.bool,
|
disableSorting: PropTypes.bool,
|
||||||
defaultSortDesc: PropTypes.bool,
|
defaultSortDesc: PropTypes.bool,
|
||||||
disableMultiSort: PropTypes.bool
|
disableMultiSort: PropTypes.bool,
|
||||||
|
disableSortRemove: PropTypes.bool,
|
||||||
|
disableMultiRemove: PropTypes.bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useSortBy = props => {
|
export const useSortBy = props => {
|
||||||
@@ -40,22 +43,26 @@ export const useSortBy = props => {
|
|||||||
rows,
|
rows,
|
||||||
columns,
|
columns,
|
||||||
orderByFn = defaultOrderByFn,
|
orderByFn = defaultOrderByFn,
|
||||||
sortByFn = defaultSortByFn,
|
defaultSort = 'alphanumeric',
|
||||||
|
sortTypes: userSortTypes,
|
||||||
manualSorting,
|
manualSorting,
|
||||||
disableSorting,
|
disableSorting,
|
||||||
defaultSortDesc,
|
defaultSortDesc,
|
||||||
|
disableSortRemove,
|
||||||
|
disableMultiRemove,
|
||||||
|
disableMultiSort,
|
||||||
hooks,
|
hooks,
|
||||||
state: [{ sortBy }, setState]
|
state: [{ sortBy }, setState],
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
columns.forEach(column => {
|
columns.forEach(column => {
|
||||||
const { accessor, canSortBy } = column
|
const { accessor, canSortBy } = column
|
||||||
column.canSortBy = accessor
|
column.canSortBy = accessor
|
||||||
? getFirstDefined(
|
? getFirstDefined(
|
||||||
canSortBy,
|
canSortBy,
|
||||||
disableSorting === true ? false : undefined,
|
disableSorting === true ? false : undefined,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
: false
|
: false
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -73,77 +80,77 @@ export const useSortBy = props => {
|
|||||||
|
|
||||||
// Find any existing sortBy for this column
|
// Find any existing sortBy for this column
|
||||||
const existingSortBy = sortBy.find(d => d.id === columnID)
|
const existingSortBy = sortBy.find(d => d.id === columnID)
|
||||||
|
const existingIndex = sortBy.findIndex(d => d.id == columnID)
|
||||||
const hasDescDefined = typeof desc !== 'undefined' && desc !== null
|
const hasDescDefined = typeof desc !== 'undefined' && desc !== null
|
||||||
|
|
||||||
let newSortBy = []
|
let newSortBy = []
|
||||||
|
|
||||||
// What should we do with this filter?
|
// What should we do with this sort action?
|
||||||
let action
|
let action
|
||||||
|
|
||||||
if (!multi) {
|
if (!disableMultiSort && multi) {
|
||||||
if (sortBy.length <= 1 && existingSortBy) {
|
if (existingSortBy) {
|
||||||
if (existingSortBy.desc) {
|
action = 'toggle'
|
||||||
action = 'remove'
|
} else {
|
||||||
} else {
|
action = 'add'
|
||||||
action = 'toggle'
|
}
|
||||||
}
|
} else {
|
||||||
|
// Normal mode
|
||||||
|
if (existingIndex !== sortBy.length - 1) {
|
||||||
|
action = 'replace'
|
||||||
|
} else if (existingSortBy) {
|
||||||
|
action = 'toggle'
|
||||||
} else {
|
} else {
|
||||||
action = 'replace'
|
action = 'replace'
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
if (!existingSortBy) {
|
|
||||||
action = 'add'
|
// Handle toggle states that will remove the sortBy
|
||||||
} else {
|
if (
|
||||||
if (hasDescDefined) {
|
action === 'toggle' && // Must be toggling
|
||||||
action = 'set'
|
!disableSortRemove && // If disableSortRemove, disable in general
|
||||||
} else {
|
!hasDescDefined && // Must not be setting desc
|
||||||
action = 'toggle'
|
(multi ? !disableMultiRemove : true) && // If multi, don't allow if disableMultiRemove
|
||||||
}
|
((existingSortBy && // Finally, detect if it should indeed be removed
|
||||||
}
|
(existingSortBy.desc && !resolvedDefaultSortDesc)) ||
|
||||||
|
(!existingSortBy.desc && resolvedDefaultSortDesc))
|
||||||
|
) {
|
||||||
|
action = 'remove'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action === 'replace') {
|
if (action === 'replace') {
|
||||||
newSortBy = [
|
newSortBy = [
|
||||||
{
|
{
|
||||||
id: columnID,
|
id: columnID,
|
||||||
desc: hasDescDefined ? desc : resolvedDefaultSortDesc
|
desc: hasDescDefined ? desc : resolvedDefaultSortDesc,
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
} else if (action === 'add') {
|
} else if (action === 'add') {
|
||||||
newSortBy = [
|
newSortBy = [
|
||||||
...sortBy,
|
...sortBy,
|
||||||
{
|
{
|
||||||
id: columnID,
|
id: columnID,
|
||||||
desc: hasDescDefined ? desc : resolvedDefaultSortDesc
|
desc: hasDescDefined ? desc : resolvedDefaultSortDesc,
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
} else if (action === 'set') {
|
|
||||||
newSortBy = sortBy.map(d => {
|
|
||||||
if (d.id === columnID) {
|
|
||||||
return {
|
|
||||||
...d,
|
|
||||||
desc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return d
|
|
||||||
})
|
|
||||||
} else if (action === 'toggle') {
|
} else if (action === 'toggle') {
|
||||||
|
// This flips (or sets) the
|
||||||
newSortBy = sortBy.map(d => {
|
newSortBy = sortBy.map(d => {
|
||||||
if (d.id === columnID) {
|
if (d.id === columnID) {
|
||||||
return {
|
return {
|
||||||
...d,
|
...d,
|
||||||
desc: !existingSortBy.desc
|
desc: hasDescDefined ? desc : !existingSortBy.desc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return d
|
return d
|
||||||
})
|
})
|
||||||
} else if (action === 'remove') {
|
} else if (action === 'remove') {
|
||||||
newSortBy = []
|
newSortBy = sortBy.filter(d => d.id !== columnID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...old,
|
...old,
|
||||||
sortBy: newSortBy
|
sortBy: newSortBy,
|
||||||
}
|
}
|
||||||
}, actions.sortByChange)
|
}, actions.sortByChange)
|
||||||
}
|
}
|
||||||
@@ -168,17 +175,17 @@ export const useSortBy = props => {
|
|||||||
{
|
{
|
||||||
onClick: canSortBy
|
onClick: canSortBy
|
||||||
? e => {
|
? e => {
|
||||||
e.persist()
|
e.persist()
|
||||||
column.toggleSortBy(
|
column.toggleSortBy(
|
||||||
undefined,
|
undefined,
|
||||||
!api.disableMultiSort && e.shiftKey
|
!api.disableMultiSort && e.shiftKey
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
style: {
|
style: {
|
||||||
cursor: canSortBy ? 'pointer' : undefined
|
cursor: canSortBy ? 'pointer' : undefined,
|
||||||
},
|
},
|
||||||
title: 'Toggle SortBy'
|
title: 'Toggle SortBy',
|
||||||
},
|
},
|
||||||
applyPropHooks(api.hooks.getSortByToggleProps, column, api),
|
applyPropHooks(api.hooks.getSortByToggleProps, column, api),
|
||||||
props
|
props
|
||||||
@@ -205,13 +212,11 @@ export const useSortBy = props => {
|
|||||||
}
|
}
|
||||||
if (debug) console.info('getSortedRows')
|
if (debug) console.info('getSortedRows')
|
||||||
|
|
||||||
const sortMethodsByColumnID = {}
|
const sortTypesByColumnID = {}
|
||||||
|
|
||||||
columns
|
columns.forEach(col => {
|
||||||
.filter(col => col.sortMethod)
|
sortTypesByColumnID[col.id] = col.sortBy
|
||||||
.forEach(col => {
|
})
|
||||||
sortMethodsByColumnID[col.id] = col.sortMethod
|
|
||||||
})
|
|
||||||
|
|
||||||
const sortData = rows => {
|
const sortData = rows => {
|
||||||
// Use the orderByFn to compose multiple sortBy's together.
|
// Use the orderByFn to compose multiple sortBy's together.
|
||||||
@@ -221,21 +226,32 @@ export const useSortBy = props => {
|
|||||||
rows,
|
rows,
|
||||||
sortBy.map(sort => {
|
sortBy.map(sort => {
|
||||||
// Support custom sorting methods for each column
|
// Support custom sorting methods for each column
|
||||||
const columnSortBy = sortMethodsByColumnID[sort.id]
|
const columnSort = sortTypesByColumnID[sort.id]
|
||||||
|
|
||||||
|
// Look up sortBy functions in this order:
|
||||||
|
// column function
|
||||||
|
// column string lookup on user sortType
|
||||||
|
// column string lookup on built-in sortType
|
||||||
|
// default function
|
||||||
|
// default string lookup on user sortType
|
||||||
|
// default string lookup on built-in sortType
|
||||||
|
const sortMethod =
|
||||||
|
isFunction(columnSort) ||
|
||||||
|
(userSortTypes || {})[columnSort] ||
|
||||||
|
sortTypes[columnSort] ||
|
||||||
|
isFunction(defaultSort) ||
|
||||||
|
(userSortTypes || {})[defaultSort] ||
|
||||||
|
sortTypes[defaultSort]
|
||||||
|
|
||||||
// Return the correct sortFn
|
// Return the correct sortFn
|
||||||
return (a, b) =>
|
return (a, b) =>
|
||||||
(columnSortBy || sortByFn)(
|
sortMethod(a.values[sort.id], b.values[sort.id], sort.desc)
|
||||||
a.values[sort.id],
|
|
||||||
b.values[sort.id],
|
|
||||||
sort.desc
|
|
||||||
)
|
|
||||||
}),
|
}),
|
||||||
// Map the directions
|
// Map the directions
|
||||||
sortBy.map(d => !d.desc)
|
sortBy.map(d => !d.desc)
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: this should be optimized. Not good to loop again
|
// If there are sub-rows, sort them
|
||||||
sortedData.forEach(row => {
|
sortedData.forEach(row => {
|
||||||
if (!row.subRows) {
|
if (!row.subRows) {
|
||||||
return
|
return
|
||||||
@@ -247,10 +263,19 @@ export const useSortBy = props => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return sortData(rows)
|
return sortData(rows)
|
||||||
}, [rows, columns, sortBy, manualSorting])
|
}, [
|
||||||
|
manualSorting,
|
||||||
|
sortBy,
|
||||||
|
debug,
|
||||||
|
columns,
|
||||||
|
rows,
|
||||||
|
orderByFn,
|
||||||
|
userSortTypes,
|
||||||
|
defaultSort,
|
||||||
|
])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...props,
|
...props,
|
||||||
rows: sortedRows
|
rows: sortedRows,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import { useState } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
// Token pagination behaves a bit differently from
|
// Token pagination behaves a bit differently from
|
||||||
// index based pagination. This hook aids in that process.
|
// index based pagination. This hook aids in that process.
|
||||||
|
|
||||||
export const useTokenPagination = () => {
|
export const useTokenPagination = () => {
|
||||||
const [pageToken, setPageToken] = useState()
|
const [pageToken, setPageToken] = React.useState()
|
||||||
const [nextPageToken, setNextPageToken] = useState()
|
const [nextPageToken, setNextPageToken] = React.useState()
|
||||||
const [previousPageTokens, setPreviousPageTokens] = useState([])
|
const [previousPageTokens, setPreviousPageTokens] = React.useState([])
|
||||||
const [pageIndex, setPageIndex] = useState(0)
|
const [pageIndex, setPageIndex] = React.useState(0)
|
||||||
|
|
||||||
// Since we're using pagination tokens intead of index, we need
|
// Since we're using pagination tokens intead of index, we need
|
||||||
// to be a bit clever with page-like navigation here.
|
// to be a bit clever with page-like navigation here.
|
||||||
@@ -46,6 +46,6 @@ export const useTokenPagination = () => {
|
|||||||
nextPage,
|
nextPage,
|
||||||
canPreviousPage,
|
canPreviousPage,
|
||||||
canNextPage,
|
canNextPage,
|
||||||
resetPagination
|
resetPagination,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
const reSplitAlphaNumeric = /([0-9]+)/gm
|
||||||
|
|
||||||
|
// Mixed sorting is slow, but very inclusive of many edge cases.
|
||||||
|
// It handles numbers, mixed alphanumeric combinations, and even
|
||||||
|
// null, undefined, and Infinity
|
||||||
|
export const alphanumeric = (a, b) => {
|
||||||
|
// Force to strings (or "" for unsupported types)
|
||||||
|
a = toString(a)
|
||||||
|
b = toString(b)
|
||||||
|
|
||||||
|
// Split on number groups, but keep the delimiter
|
||||||
|
// Then remove falsey split values
|
||||||
|
a = a.split(reSplitAlphaNumeric).filter(Boolean)
|
||||||
|
b = b.split(reSplitAlphaNumeric).filter(Boolean)
|
||||||
|
|
||||||
|
// While
|
||||||
|
while (a.length && b.length) {
|
||||||
|
let aa = a.shift()
|
||||||
|
let bb = b.shift()
|
||||||
|
|
||||||
|
const an = parseInt(aa, 10)
|
||||||
|
const bn = parseInt(bb, 10)
|
||||||
|
|
||||||
|
const combo = [an, bn].sort()
|
||||||
|
|
||||||
|
// Both are string
|
||||||
|
if (isNaN(combo[0])) {
|
||||||
|
if (aa > bb) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if (bb > aa) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// One is a string, one is a number
|
||||||
|
if (isNaN(combo[1])) {
|
||||||
|
return isNaN(an) ? -1 : 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Both are numbers
|
||||||
|
if (an > bn) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if (bn > an) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return a.length - b.length
|
||||||
|
}
|
||||||
|
|
||||||
|
export function datetime(a, b) {
|
||||||
|
a = a.getTime()
|
||||||
|
b = b.getTime()
|
||||||
|
return numeric(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function numeric(a, b) {
|
||||||
|
return a === b ? 0 : a > b ? 1 : -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
|
||||||
|
function toString(a) {
|
||||||
|
if (typeof a === 'number') {
|
||||||
|
if (isNaN(a) || a === Infinity || a === -Infinity) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
return String(a)
|
||||||
|
}
|
||||||
|
if (typeof a === 'string') {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
}
|
||||||
+14
-39
@@ -28,25 +28,6 @@ export function defaultOrderByFn(arr, funcs, dirs) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function defaultSortByFn(a, b, desc) {
|
|
||||||
// force null and undefined to the bottom
|
|
||||||
a = a === null || a === undefined ? '' : a
|
|
||||||
b = b === null || b === undefined ? '' : b
|
|
||||||
// force any string values to lowercase
|
|
||||||
a = typeof a === 'string' ? a.toLowerCase() : a
|
|
||||||
b = typeof b === 'string' ? b.toLowerCase() : b
|
|
||||||
// Return either 1 or -1 to indicate a sort priority
|
|
||||||
if (a > b) {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
if (a < b) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
// returning 0, undefined or any falsey value will defer to the next
|
|
||||||
// sorting mechanism or eventually the columns index via the orderByFn
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getFirstDefined(...args) {
|
export function getFirstDefined(...args) {
|
||||||
for (let i = 0; i < args.length; i += 1) {
|
for (let i = 0; i < args.length; i += 1) {
|
||||||
if (typeof args[i] !== 'undefined') {
|
if (typeof args[i] !== 'undefined') {
|
||||||
@@ -67,14 +48,6 @@ export function defaultGroupByFn(rows, grouper) {
|
|||||||
}, {})
|
}, {})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function defaultFilterFn(row, id, value, column) {
|
|
||||||
return row.values[id] !== undefined
|
|
||||||
? String(row.values[id])
|
|
||||||
.toLowerCase()
|
|
||||||
.includes(String(value).toLowerCase())
|
|
||||||
: true
|
|
||||||
}
|
|
||||||
|
|
||||||
export function setBy(obj = {}, path, value) {
|
export function setBy(obj = {}, path, value) {
|
||||||
const recurse = (obj, depth = 0) => {
|
const recurse = (obj, depth = 0) => {
|
||||||
const key = path[depth]
|
const key = path[depth]
|
||||||
@@ -83,7 +56,7 @@ export function setBy(obj = {}, path, value) {
|
|||||||
depth === path.length - 1 ? value : recurse(target, depth + 1)
|
depth === path.length - 1 ? value : recurse(target, depth + 1)
|
||||||
return {
|
return {
|
||||||
...obj,
|
...obj,
|
||||||
[key]: subValue
|
[key]: subValue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,11 +68,11 @@ export function getElementDimensions(element) {
|
|||||||
const style = window.getComputedStyle(element)
|
const style = window.getComputedStyle(element)
|
||||||
const margins = {
|
const margins = {
|
||||||
left: parseInt(style.marginLeft),
|
left: parseInt(style.marginLeft),
|
||||||
right: parseInt(style.marginRight)
|
right: parseInt(style.marginRight),
|
||||||
}
|
}
|
||||||
const padding = {
|
const padding = {
|
||||||
left: parseInt(style.paddingLeft),
|
left: parseInt(style.paddingLeft),
|
||||||
right: parseInt(style.paddingRight)
|
right: parseInt(style.paddingRight),
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
left: Math.ceil(rect.left),
|
left: Math.ceil(rect.left),
|
||||||
@@ -111,17 +84,13 @@ export function getElementDimensions(element) {
|
|||||||
marginRight: margins.right,
|
marginRight: margins.right,
|
||||||
paddingLeft: padding.left,
|
paddingLeft: padding.left,
|
||||||
paddingRight: padding.right,
|
paddingRight: padding.right,
|
||||||
scrollWidth: element.scrollWidth
|
scrollWidth: element.scrollWidth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function flexRender(Comp, props) {
|
export function flexRender(Comp, props) {
|
||||||
if (typeof Comp === 'function') {
|
if (typeof Comp === 'function' || typeof Comp === 'object') {
|
||||||
return Object.getPrototypeOf(Comp).isReactComponent ? (
|
return <Comp {...props} />
|
||||||
<Comp {...props} />
|
|
||||||
) : (
|
|
||||||
Comp(props)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return Comp
|
return Comp
|
||||||
}
|
}
|
||||||
@@ -134,9 +103,9 @@ export const mergeProps = (...groups) => {
|
|||||||
...rest,
|
...rest,
|
||||||
style: {
|
style: {
|
||||||
...(props.style || {}),
|
...(props.style || {}),
|
||||||
...style
|
...style,
|
||||||
},
|
},
|
||||||
className: [props.className, className].filter(Boolean).join(' ')
|
className: [props.className, className].filter(Boolean).join(' '),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return props
|
return props
|
||||||
@@ -162,6 +131,12 @@ export function sum(arr) {
|
|||||||
return arr.reduce((prev, curr) => prev + curr, 0)
|
return arr.reduce((prev, curr) => prev + curr, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isFunction(a) {
|
||||||
|
if (typeof a === 'function') {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function makePathArray(obj) {
|
function makePathArray(obj) {
|
||||||
return flattenDeep(obj)
|
return flattenDeep(obj)
|
||||||
.join('.')
|
.join('.')
|
||||||
|
|||||||
Reference in New Issue
Block a user