mirror of
https://github.com/gosticks/react-table.git
synced 2026-08-19 08:20:30 +00:00
Compare commits
25
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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",
|
||||
"extends": ["standard", "standard-react"],
|
||||
"env": {
|
||||
"es6": true
|
||||
},
|
||||
"plugins": ["react"],
|
||||
"parserOptions": {
|
||||
"sourceType": "module"
|
||||
},
|
||||
"rules": {
|
||||
"space-before-function-paren": 0,
|
||||
"react/jsx-boolean-value": 0
|
||||
}
|
||||
"parser": "babel-eslint",
|
||||
"extends": ["react-app", "prettier"],
|
||||
"plugins": [
|
||||
"react-hooks"
|
||||
],
|
||||
"env": {
|
||||
"es6": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"sourceType": "module"
|
||||
},
|
||||
"rules": {
|
||||
"space-before-function-paren": 0,
|
||||
"react/jsx-boolean-value": 0
|
||||
},
|
||||
"settings": {
|
||||
"react": {
|
||||
"version": "latest"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -1,7 +1,7 @@
|
||||
node_modules/
|
||||
lib/
|
||||
dist/
|
||||
es/
|
||||
node_modules
|
||||
lib
|
||||
dist
|
||||
es
|
||||
docs/build
|
||||
react-table.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,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",
|
||||
"version": "7.0.0-alpha.2",
|
||||
"version": "7.0.0-alpha.7",
|
||||
"description": "A fast, lightweight, opinionated table and datagrid built on React",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/tannerlinsley/react-table#readme",
|
||||
@@ -15,11 +15,12 @@
|
||||
"datagrid"
|
||||
],
|
||||
"main": "dist/index.js",
|
||||
"_module": "dist/index.es.js",
|
||||
"_jsnext:main": "dist/index.es.js",
|
||||
"module": "dist/index.es.js",
|
||||
"jsnext:main": "dist/index.es.js",
|
||||
"scripts": {
|
||||
"test": "cross-env CI=1 react-scripts test --env=jsdom",
|
||||
"test:watch": "react-scripts test --env=jsdom",
|
||||
"test": "is-ci 'test:ci' 'test:dev'",
|
||||
"test:dev": "jest --watch",
|
||||
"test:ci": "jest",
|
||||
"build": "rollup -c",
|
||||
"start": "rollup -c -w",
|
||||
"prepare": "yarn build",
|
||||
@@ -46,17 +47,30 @@
|
||||
"@babel/preset-react": "^7.0.0",
|
||||
"@babel/runtime": "^7.2.0",
|
||||
"@svgr/rollup": "^4.1.0",
|
||||
"@testing-library/react": "^8.0.4",
|
||||
"babel-core": "7.0.0-bridge.0",
|
||||
"babel-eslint": "^10.0.1",
|
||||
"babel-eslint": "9.x",
|
||||
"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-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-prettier": "^3.1.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",
|
||||
"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-plugin-babel": "^4.1.0",
|
||||
"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'
|
||||
|
||||
export default {
|
||||
input: 'src/index.js',
|
||||
output: [
|
||||
{
|
||||
export default [
|
||||
{
|
||||
input: 'src/index.js',
|
||||
output: {
|
||||
file: pkg.main,
|
||||
format: 'cjs',
|
||||
sourcemap: true
|
||||
}
|
||||
// {
|
||||
// file: pkg.module,
|
||||
// format: "es",
|
||||
// sourcemap: true
|
||||
// }
|
||||
],
|
||||
plugins: [
|
||||
external(),
|
||||
babel({
|
||||
exclude: 'node_modules/**'
|
||||
}),
|
||||
resolve(),
|
||||
commonjs(),
|
||||
uglify()
|
||||
]
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
external(),
|
||||
babel({
|
||||
exclude: 'node_modules/**'
|
||||
}),
|
||||
resolve(),
|
||||
commonjs(),
|
||||
uglify()
|
||||
]
|
||||
},
|
||||
{
|
||||
input: 'src/index.js',
|
||||
external: Object.keys(pkg.peerDependencies),
|
||||
output: {
|
||||
file: pkg.module,
|
||||
format: 'es',
|
||||
sourcemap: true
|
||||
},
|
||||
plugins: [
|
||||
babel({
|
||||
exclude: ['/**/node_modules/**']
|
||||
})
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
+138
-138
@@ -8,16 +8,147 @@ const propTypes = {
|
||||
columns: PropTypes.arrayOf(
|
||||
PropTypes.shape({
|
||||
Cell: PropTypes.any,
|
||||
Header: PropTypes.any
|
||||
Header: 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, 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
|
||||
})
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
export const useColumns = props => {
|
||||
const {
|
||||
debug,
|
||||
columns: userColumns,
|
||||
state: [{ groupBy }]
|
||||
state: [{ groupBy }],
|
||||
} = props
|
||||
|
||||
PropTypes.checkPropTypes(propTypes, props, 'property', 'useColumns')
|
||||
@@ -33,7 +164,7 @@ export const useColumns = props => {
|
||||
|
||||
columns = [
|
||||
...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
|
||||
@@ -43,69 +174,15 @@ export const useColumns = props => {
|
||||
return {
|
||||
columns,
|
||||
headerGroups,
|
||||
headers
|
||||
headers,
|
||||
}
|
||||
}, [groupBy, userColumns])
|
||||
}, [debug, groupBy, userColumns])
|
||||
|
||||
return {
|
||||
...props,
|
||||
columns,
|
||||
headerGroups,
|
||||
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
|
||||
})
|
||||
headers,
|
||||
}
|
||||
|
||||
function flattenBy(columns, childKey) {
|
||||
@@ -125,81 +202,4 @@ export const useColumns = props => {
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,12 @@ defaultState.expanded = {}
|
||||
|
||||
addActions({
|
||||
toggleExpanded: '__toggleExpanded__',
|
||||
useExpanded: '__useExpanded__'
|
||||
useExpanded: '__useExpanded__',
|
||||
})
|
||||
|
||||
const propTypes = {
|
||||
expandedKey: PropTypes.string
|
||||
expandedKey: PropTypes.string,
|
||||
paginateSubRows: PropTypes.bool,
|
||||
}
|
||||
|
||||
export const useExpanded = props => {
|
||||
@@ -21,11 +22,11 @@ export const useExpanded = props => {
|
||||
|
||||
const {
|
||||
debug,
|
||||
columns,
|
||||
rows,
|
||||
expandedKey = 'expanded',
|
||||
hooks,
|
||||
state: [{ expanded }, setState]
|
||||
state: [{ expanded }, setState],
|
||||
paginateSubRows = true,
|
||||
} = props
|
||||
|
||||
const toggleExpandedByPath = (path, set) => {
|
||||
@@ -35,7 +36,7 @@ export const useExpanded = props => {
|
||||
set = getFirstDefined(set, !existing)
|
||||
return {
|
||||
...old,
|
||||
expanded: setBy(expanded, path, set)
|
||||
expanded: setBy(expanded, path, set),
|
||||
}
|
||||
}, actions.toggleExpanded)
|
||||
}
|
||||
@@ -62,17 +63,21 @@ export const useExpanded = props => {
|
||||
row.isExpanded =
|
||||
(row.original && row.original[expandedKey]) || getBy(expanded, path)
|
||||
|
||||
expandedRows.push(row)
|
||||
if (paginateSubRows || (!paginateSubRows && row.depth === 0)) {
|
||||
expandedRows.push(row)
|
||||
}
|
||||
|
||||
if (row.isExpanded && row.subRows && row.subRows.length) {
|
||||
row.subRows.forEach((row, i) => handleRow(row, i, depth + 1, path))
|
||||
}
|
||||
|
||||
return row
|
||||
}
|
||||
|
||||
rows.forEach((row, i) => handleRow(row, i))
|
||||
|
||||
return expandedRows
|
||||
}, [rows, expanded, columns])
|
||||
}, [debug, rows, expandedKey, expanded, paginateSubRows])
|
||||
|
||||
const expandedDepth = findExpandedDepth(expanded)
|
||||
|
||||
@@ -80,7 +85,7 @@ export const useExpanded = props => {
|
||||
...props,
|
||||
toggleExpandedByPath,
|
||||
expandedDepth,
|
||||
rows: expandedRows
|
||||
rows: expandedRows,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
-16
@@ -8,7 +8,7 @@ import { defaultState } from './useTableState'
|
||||
defaultState.filters = {}
|
||||
addActions({
|
||||
setFilter: '__setFilter__',
|
||||
setAllFilters: '__setAllFilters__'
|
||||
setAllFilters: '__setAllFilters__',
|
||||
})
|
||||
|
||||
const propTypes = {
|
||||
@@ -18,12 +18,12 @@ const propTypes = {
|
||||
filterFn: PropTypes.func,
|
||||
filterAll: PropTypes.bool,
|
||||
canFilter: PropTypes.bool,
|
||||
Filter: PropTypes.any
|
||||
Filter: PropTypes.any,
|
||||
})
|
||||
),
|
||||
|
||||
filterFn: PropTypes.func,
|
||||
manualFilters: PropTypes.bool
|
||||
manualFilters: PropTypes.bool,
|
||||
}
|
||||
|
||||
export const useFilters = props => {
|
||||
@@ -37,17 +37,17 @@ export const useFilters = props => {
|
||||
manualFilters,
|
||||
disableFilters,
|
||||
hooks,
|
||||
state: [{ filters }, setState]
|
||||
state: [{ filters }, setState],
|
||||
} = props
|
||||
|
||||
columns.forEach(column => {
|
||||
const { id, accessor, canFilter } = column
|
||||
column.canFilter = accessor
|
||||
? getFirstDefined(
|
||||
canFilter,
|
||||
disableFilters === true ? false : undefined,
|
||||
true
|
||||
)
|
||||
canFilter,
|
||||
disableFilters === true ? false : undefined,
|
||||
true
|
||||
)
|
||||
: false
|
||||
// Was going to add this to the filter hook
|
||||
column.filterValue = filters[id]
|
||||
@@ -60,8 +60,8 @@ export const useFilters = props => {
|
||||
return {
|
||||
...old,
|
||||
filters: {
|
||||
...rest
|
||||
}
|
||||
...rest,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,8 +69,8 @@ export const useFilters = props => {
|
||||
...old,
|
||||
filters: {
|
||||
...filters,
|
||||
[id]: val
|
||||
}
|
||||
[id]: val,
|
||||
},
|
||||
}
|
||||
}, actions.setFilter)
|
||||
}
|
||||
@@ -79,7 +79,7 @@ export const useFilters = props => {
|
||||
return setState(old => {
|
||||
return {
|
||||
...old,
|
||||
filters
|
||||
filters,
|
||||
}
|
||||
}, actions.setAllFilters)
|
||||
}
|
||||
@@ -134,7 +134,7 @@ export const useFilters = props => {
|
||||
}
|
||||
return {
|
||||
...row,
|
||||
subRows: filterRows(row.subRows)
|
||||
subRows: filterRows(row.subRows),
|
||||
}
|
||||
})
|
||||
|
||||
@@ -150,12 +150,12 @@ export const useFilters = props => {
|
||||
}
|
||||
|
||||
return filterRows(rows)
|
||||
}, [rows, filters, manualFilters])
|
||||
}, [manualFilters, filters, debug, rows, columns, filterFn])
|
||||
|
||||
return {
|
||||
...props,
|
||||
setFilter,
|
||||
setAllFilters,
|
||||
rows: filteredRows
|
||||
rows: filteredRows,
|
||||
}
|
||||
}
|
||||
|
||||
+14
-18
@@ -5,7 +5,7 @@ import { getFirstDefined, sum } from '../utils'
|
||||
export const actions = {}
|
||||
|
||||
const propTypes = {
|
||||
defaultFlex: PropTypes.number
|
||||
defaultFlex: PropTypes.number,
|
||||
}
|
||||
|
||||
export const useFlexLayout = props => {
|
||||
@@ -18,17 +18,12 @@ export const useFlexLayout = props => {
|
||||
getRowProps,
|
||||
getHeaderRowProps,
|
||||
getHeaderProps,
|
||||
getCellProps
|
||||
}
|
||||
getCellProps,
|
||||
},
|
||||
} = props
|
||||
|
||||
columnsHooks.push((columns, api) => {
|
||||
const visibleColumns = columns.filter(column => {
|
||||
column.visible =
|
||||
typeof column.show === 'function' ? column.show(api) : !!column.show
|
||||
return column.visible
|
||||
})
|
||||
|
||||
const visibleColumns = columns.filter(column => column.visible)
|
||||
const columnMeasurements = {}
|
||||
|
||||
let sumWidth = 0
|
||||
@@ -52,8 +47,8 @@ export const useFlexLayout = props => {
|
||||
const rowStyles = {
|
||||
style: {
|
||||
display: 'flex',
|
||||
minWidth: `${sumWidth}px`
|
||||
}
|
||||
minWidth: `${sumWidth}px`,
|
||||
},
|
||||
}
|
||||
|
||||
api.rowStyles = rowStyles
|
||||
@@ -64,8 +59,8 @@ export const useFlexLayout = props => {
|
||||
getHeaderProps.push(column => ({
|
||||
style: {
|
||||
boxSizing: 'border-box',
|
||||
...getStylesForColumn(column, columnMeasurements, defaultFlex, api)
|
||||
}
|
||||
...getStylesForColumn(column, columnMeasurements, defaultFlex, api),
|
||||
},
|
||||
// [refKey]: el => {
|
||||
// renderedCellInfoRef.current[key] = {
|
||||
// column,
|
||||
@@ -85,8 +80,8 @@ export const useFlexLayout = props => {
|
||||
defaultFlex,
|
||||
undefined,
|
||||
api
|
||||
)
|
||||
}
|
||||
),
|
||||
},
|
||||
// [refKey]: el => {
|
||||
// renderedCellInfoRef.current[columnPathStr] = {
|
||||
// column,
|
||||
@@ -115,7 +110,7 @@ function getStylesForColumn(column, columnMeasurements, defaultFlex, api) {
|
||||
return {
|
||||
flex: `${flex} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
maxWidth: `${maxWidth}px`,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,6 +122,7 @@ function getSizesForColumn(
|
||||
) {
|
||||
if (columns) {
|
||||
columns = columns
|
||||
.filter(col => col.show || col.visible)
|
||||
.map(column =>
|
||||
getSizesForColumn(column, columnMeasurements, defaultFlex, api)
|
||||
)
|
||||
@@ -143,7 +139,7 @@ function getSizesForColumn(
|
||||
return {
|
||||
flex,
|
||||
width,
|
||||
maxWidth
|
||||
maxWidth,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +149,7 @@ function getSizesForColumn(
|
||||
width === 'auto'
|
||||
? columnMeasurements[id] || defaultFlex
|
||||
: getFirstDefined(width, minWidth, defaultFlex),
|
||||
maxWidth
|
||||
maxWidth,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+27
-19
@@ -8,13 +8,13 @@ import {
|
||||
mergeProps,
|
||||
applyPropHooks,
|
||||
defaultGroupByFn,
|
||||
getFirstDefined
|
||||
getFirstDefined,
|
||||
} from '../utils'
|
||||
|
||||
defaultState.groupBy = []
|
||||
|
||||
addActions({
|
||||
toggleGroupBy: '__toggleGroupBy__'
|
||||
toggleGroupBy: '__toggleGroupBy__',
|
||||
})
|
||||
|
||||
const propTypes = {
|
||||
@@ -23,12 +23,12 @@ const propTypes = {
|
||||
PropTypes.shape({
|
||||
aggregate: PropTypes.func,
|
||||
canGroupBy: PropTypes.bool,
|
||||
Aggregated: PropTypes.any
|
||||
Aggregated: PropTypes.any,
|
||||
})
|
||||
),
|
||||
groupByFn: PropTypes.func,
|
||||
manualGrouping: PropTypes.bool,
|
||||
aggregations: PropTypes.object
|
||||
aggregations: PropTypes.object,
|
||||
}
|
||||
|
||||
export const useGroupBy = props => {
|
||||
@@ -43,7 +43,7 @@ export const useGroupBy = props => {
|
||||
disableGrouping,
|
||||
aggregations: userAggregations = {},
|
||||
hooks,
|
||||
state: [{ groupBy }, setState]
|
||||
state: [{ groupBy }, setState],
|
||||
} = props
|
||||
|
||||
columns.forEach(column => {
|
||||
@@ -52,10 +52,10 @@ export const useGroupBy = props => {
|
||||
|
||||
column.canGroupBy = accessor
|
||||
? getFirstDefined(
|
||||
canGroupBy,
|
||||
disableGrouping === true ? false : undefined,
|
||||
true
|
||||
)
|
||||
canGroupBy,
|
||||
disableGrouping === true ? false : undefined,
|
||||
true
|
||||
)
|
||||
: false
|
||||
|
||||
column.Aggregated = column.Aggregated || column.Cell
|
||||
@@ -68,12 +68,12 @@ export const useGroupBy = props => {
|
||||
if (resolvedToggle) {
|
||||
return {
|
||||
...old,
|
||||
groupBy: [...groupBy, id]
|
||||
groupBy: [...groupBy, id],
|
||||
}
|
||||
}
|
||||
return {
|
||||
...old,
|
||||
groupBy: groupBy.filter(d => d !== id)
|
||||
groupBy: groupBy.filter(d => d !== id),
|
||||
}
|
||||
}, actions.toggleGroupBy)
|
||||
}
|
||||
@@ -97,14 +97,14 @@ export const useGroupBy = props => {
|
||||
{
|
||||
onClick: canGroupBy
|
||||
? e => {
|
||||
e.persist()
|
||||
column.toggleGroupBy()
|
||||
}
|
||||
e.persist()
|
||||
column.toggleGroupBy()
|
||||
}
|
||||
: undefined,
|
||||
style: {
|
||||
cursor: canGroupBy ? 'pointer' : undefined
|
||||
cursor: canGroupBy ? 'pointer' : undefined,
|
||||
},
|
||||
title: 'Toggle GroupBy'
|
||||
title: 'Toggle GroupBy',
|
||||
},
|
||||
applyPropHooks(api.hooks.getGroupByToggleProps, column, api),
|
||||
props
|
||||
@@ -169,7 +169,7 @@ export const useGroupBy = props => {
|
||||
values,
|
||||
subRows,
|
||||
depth,
|
||||
index
|
||||
index,
|
||||
}
|
||||
return row
|
||||
}
|
||||
@@ -180,10 +180,18 @@ export const useGroupBy = props => {
|
||||
|
||||
// Assign the new data
|
||||
return groupRecursively(rows, groupBy)
|
||||
}, [rows, groupBy, columns, manualGroupBy])
|
||||
}, [
|
||||
manualGroupBy,
|
||||
groupBy,
|
||||
debug,
|
||||
rows,
|
||||
columns,
|
||||
userAggregations,
|
||||
groupByFn,
|
||||
])
|
||||
|
||||
return {
|
||||
...props,
|
||||
rows: groupedRows
|
||||
rows: groupedRows,
|
||||
}
|
||||
}
|
||||
|
||||
+23
-14
@@ -9,12 +9,12 @@ defaultState.pageSize = 10
|
||||
defaultState.pageIndex = 0
|
||||
|
||||
addActions({
|
||||
pageChange: '__pageChange__'
|
||||
pageChange: '__pageChange__',
|
||||
})
|
||||
|
||||
const propTypes = {
|
||||
// General
|
||||
manualPagination: PropTypes.bool
|
||||
manualPagination: PropTypes.bool,
|
||||
}
|
||||
|
||||
export const usePagination = props => {
|
||||
@@ -23,6 +23,7 @@ export const usePagination = props => {
|
||||
const {
|
||||
rows,
|
||||
manualPagination,
|
||||
disablePageResetOnDataChange,
|
||||
debug,
|
||||
state: [
|
||||
{
|
||||
@@ -31,27 +32,34 @@ export const usePagination = props => {
|
||||
pageCount: userPageCount,
|
||||
filters,
|
||||
groupBy,
|
||||
sortBy
|
||||
sortBy,
|
||||
},
|
||||
setState
|
||||
]
|
||||
setState,
|
||||
],
|
||||
} = props
|
||||
|
||||
const pageOptions = useMemo(
|
||||
() => [...new Array(userPageCount)].map((d, i) => i),
|
||||
[userPageCount]
|
||||
)
|
||||
|
||||
const rowDep = disablePageResetOnDataChange ? null : rows
|
||||
|
||||
useLayoutEffect(() => {
|
||||
setState(
|
||||
old => ({
|
||||
...old,
|
||||
pageIndex: 0
|
||||
pageIndex: 0,
|
||||
}),
|
||||
actions.pageChange
|
||||
)
|
||||
}, [filters, groupBy, sortBy])
|
||||
}, [setState, rowDep, filters, groupBy, sortBy])
|
||||
|
||||
const { pages, pageCount } = useMemo(() => {
|
||||
if (manualPagination) {
|
||||
return {
|
||||
pages: [rows],
|
||||
pageCount: userPageCount
|
||||
pageCount: userPageCount,
|
||||
}
|
||||
}
|
||||
if (debug) console.info('getPages')
|
||||
@@ -72,11 +80,10 @@ export const usePagination = props => {
|
||||
return {
|
||||
pages,
|
||||
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 canPreviousPage = pageIndex > 0
|
||||
const canNextPage = pageIndex < pageCount - 1
|
||||
@@ -89,7 +96,7 @@ export const usePagination = props => {
|
||||
}
|
||||
return {
|
||||
...old,
|
||||
pageIndex
|
||||
pageIndex,
|
||||
}
|
||||
}, actions.pageChange)
|
||||
}
|
||||
@@ -109,7 +116,7 @@ export const usePagination = props => {
|
||||
return {
|
||||
...old,
|
||||
pageIndex,
|
||||
pageSize
|
||||
pageSize,
|
||||
}
|
||||
}, actions.setPageSize)
|
||||
}
|
||||
@@ -124,6 +131,8 @@ export const usePagination = props => {
|
||||
gotoPage,
|
||||
previousPage,
|
||||
nextPage,
|
||||
setPageSize
|
||||
setPageSize,
|
||||
pageIndex,
|
||||
pageSize,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useMemo } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
const propTypes = {
|
||||
subRowsKey: PropTypes.string
|
||||
subRowsKey: PropTypes.string,
|
||||
}
|
||||
|
||||
export const useRows = props => {
|
||||
@@ -26,9 +26,10 @@ export const useRows = props => {
|
||||
const row = {
|
||||
original,
|
||||
index: i,
|
||||
path: [i], // used to create a key for each row even if not nested
|
||||
subRows,
|
||||
depth,
|
||||
cells: [{}] // This is a dummy cell
|
||||
cells: [{}], // This is a dummy cell
|
||||
}
|
||||
|
||||
// Override common array functions (and the dummy cell's getCellProps function)
|
||||
@@ -56,10 +57,10 @@ export const useRows = props => {
|
||||
|
||||
// Use the resolved data
|
||||
return data.map((d, i) => accessRow(d, i))
|
||||
}, [data, columns])
|
||||
}, [debug, data, subRowsKey, columns])
|
||||
|
||||
return {
|
||||
...props,
|
||||
rows: accessedRows
|
||||
rows: accessedRows,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
export const useSimpleLayout = props => {
|
||||
const {
|
||||
hooks: { columns: columnsHooks, getHeaderProps, getCellProps },
|
||||
} = props
|
||||
|
||||
columnsHooks.push(columns => {
|
||||
getHeaderProps.push(column => ({
|
||||
style: {
|
||||
boxSizing: 'border-box',
|
||||
width: column.width !== undefined ? `${column.width}px` : 'auto',
|
||||
},
|
||||
}))
|
||||
|
||||
getCellProps.push(cell => {
|
||||
return {
|
||||
style: {
|
||||
boxSizing: 'border-box',
|
||||
width:
|
||||
cell.column.width !== undefined ? `${cell.column.width}px` : 'auto',
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return columns
|
||||
})
|
||||
|
||||
return props
|
||||
}
|
||||
+33
-28
@@ -8,13 +8,13 @@ import {
|
||||
applyPropHooks,
|
||||
getFirstDefined,
|
||||
defaultOrderByFn,
|
||||
defaultSortByFn
|
||||
defaultSortByFn,
|
||||
} from '../utils'
|
||||
|
||||
defaultState.sortBy = []
|
||||
|
||||
addActions({
|
||||
sortByChange: '__sortByChange__'
|
||||
sortByChange: '__sortByChange__',
|
||||
})
|
||||
|
||||
const propTypes = {
|
||||
@@ -22,14 +22,15 @@ const propTypes = {
|
||||
columns: PropTypes.arrayOf(
|
||||
PropTypes.shape({
|
||||
sortByFn: PropTypes.func,
|
||||
efaultSortDesc: PropTypes.bool
|
||||
defaultSortDesc: PropTypes.bool,
|
||||
})
|
||||
),
|
||||
sortByFn: PropTypes.func,
|
||||
manualSorting: PropTypes.bool,
|
||||
disableSorting: PropTypes.bool,
|
||||
defaultSortDesc: PropTypes.bool,
|
||||
disableMultiSort: PropTypes.bool
|
||||
disableMultiSort: PropTypes.bool,
|
||||
disableSortRemove: PropTypes.bool,
|
||||
}
|
||||
|
||||
export const useSortBy = props => {
|
||||
@@ -44,18 +45,19 @@ export const useSortBy = props => {
|
||||
manualSorting,
|
||||
disableSorting,
|
||||
defaultSortDesc,
|
||||
disableSortRemove,
|
||||
hooks,
|
||||
state: [{ sortBy }, setState]
|
||||
state: [{ sortBy }, setState],
|
||||
} = props
|
||||
|
||||
columns.forEach(column => {
|
||||
const { accessor, canSortBy } = column
|
||||
column.canSortBy = accessor
|
||||
? getFirstDefined(
|
||||
canSortBy,
|
||||
disableSorting === true ? false : undefined,
|
||||
true
|
||||
)
|
||||
canSortBy,
|
||||
disableSorting === true ? false : undefined,
|
||||
true
|
||||
)
|
||||
: false
|
||||
})
|
||||
|
||||
@@ -82,8 +84,11 @@ export const useSortBy = props => {
|
||||
|
||||
if (!multi) {
|
||||
if (sortBy.length <= 1 && existingSortBy) {
|
||||
if (existingSortBy.desc) {
|
||||
action = 'remove'
|
||||
if (
|
||||
(existingSortBy.desc && !resolvedDefaultSortDesc) ||
|
||||
(!existingSortBy.desc && resolvedDefaultSortDesc)
|
||||
) {
|
||||
action = disableSortRemove ? 'toggle' : 'remove'
|
||||
} else {
|
||||
action = 'toggle'
|
||||
}
|
||||
@@ -106,23 +111,23 @@ export const useSortBy = props => {
|
||||
newSortBy = [
|
||||
{
|
||||
id: columnID,
|
||||
desc: hasDescDefined ? desc : resolvedDefaultSortDesc
|
||||
}
|
||||
desc: hasDescDefined ? desc : resolvedDefaultSortDesc,
|
||||
},
|
||||
]
|
||||
} else if (action === 'add') {
|
||||
newSortBy = [
|
||||
...sortBy,
|
||||
{
|
||||
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
|
||||
desc,
|
||||
}
|
||||
}
|
||||
return d
|
||||
@@ -132,7 +137,7 @@ export const useSortBy = props => {
|
||||
if (d.id === columnID) {
|
||||
return {
|
||||
...d,
|
||||
desc: !existingSortBy.desc
|
||||
desc: !existingSortBy.desc,
|
||||
}
|
||||
}
|
||||
return d
|
||||
@@ -143,7 +148,7 @@ export const useSortBy = props => {
|
||||
|
||||
return {
|
||||
...old,
|
||||
sortBy: newSortBy
|
||||
sortBy: newSortBy,
|
||||
}
|
||||
}, actions.sortByChange)
|
||||
}
|
||||
@@ -168,17 +173,17 @@ export const useSortBy = props => {
|
||||
{
|
||||
onClick: canSortBy
|
||||
? e => {
|
||||
e.persist()
|
||||
column.toggleSortBy(
|
||||
undefined,
|
||||
!api.disableMultiSort && e.shiftKey
|
||||
)
|
||||
}
|
||||
e.persist()
|
||||
column.toggleSortBy(
|
||||
undefined,
|
||||
!api.disableMultiSort && e.shiftKey
|
||||
)
|
||||
}
|
||||
: undefined,
|
||||
style: {
|
||||
cursor: canSortBy ? 'pointer' : undefined
|
||||
cursor: canSortBy ? 'pointer' : undefined,
|
||||
},
|
||||
title: 'Toggle SortBy'
|
||||
title: 'Toggle SortBy',
|
||||
},
|
||||
applyPropHooks(api.hooks.getSortByToggleProps, column, api),
|
||||
props
|
||||
@@ -247,10 +252,10 @@ export const useSortBy = props => {
|
||||
}
|
||||
|
||||
return sortData(rows)
|
||||
}, [rows, columns, sortBy, manualSorting])
|
||||
}, [manualSorting, sortBy, debug, columns, rows, orderByFn, sortByFn])
|
||||
|
||||
return {
|
||||
...props,
|
||||
rows: sortedRows
|
||||
rows: sortedRows,
|
||||
}
|
||||
}
|
||||
|
||||
+18
-12
@@ -10,7 +10,7 @@ const renderErr =
|
||||
const propTypes = {
|
||||
// General
|
||||
data: PropTypes.array.isRequired,
|
||||
debug: PropTypes.bool
|
||||
debug: PropTypes.bool,
|
||||
}
|
||||
|
||||
export const useTable = (props, ...plugins) => {
|
||||
@@ -41,7 +41,7 @@ export const useTable = (props, ...plugins) => {
|
||||
getRowProps: [],
|
||||
getHeaderRowProps: [],
|
||||
getHeaderProps: [],
|
||||
getCellProps: []
|
||||
getCellProps: [],
|
||||
}
|
||||
|
||||
// The initial api
|
||||
@@ -49,7 +49,7 @@ export const useTable = (props, ...plugins) => {
|
||||
...props,
|
||||
data,
|
||||
state,
|
||||
hooks
|
||||
hooks,
|
||||
}
|
||||
|
||||
if (debug) console.time('hooks')
|
||||
@@ -62,6 +62,11 @@ export const useTable = (props, ...plugins) => {
|
||||
applyHooks(api.hooks.beforeRender, undefined, api)
|
||||
if (debug) console.timeEnd('hooks.beforeRender')
|
||||
|
||||
api.columns.forEach(column => {
|
||||
column.visible =
|
||||
typeof column.show === 'function' ? column.show(api) : !!column.show
|
||||
})
|
||||
|
||||
if (debug) console.time('hooks.columns')
|
||||
api.columns = applyHooks(api.hooks.columns, api.columns, api)
|
||||
if (debug) console.timeEnd('hooks.columns')
|
||||
@@ -78,7 +83,7 @@ export const useTable = (props, ...plugins) => {
|
||||
return flexRender(column[type], {
|
||||
...api,
|
||||
...column,
|
||||
...userProps
|
||||
...userProps,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -86,7 +91,8 @@ export const useTable = (props, ...plugins) => {
|
||||
column.getHeaderProps = props =>
|
||||
mergeProps(
|
||||
{
|
||||
key: ['header', column.id].join('_')
|
||||
key: ['header', column.id].join('_'),
|
||||
colSpan: column.columns ? column.columns.length : 1,
|
||||
},
|
||||
applyPropHooks(api.hooks.getHeaderProps, column, api),
|
||||
props
|
||||
@@ -119,7 +125,7 @@ export const useTable = (props, ...plugins) => {
|
||||
headerGroup.getRowProps = (props = {}) =>
|
||||
mergeProps(
|
||||
{
|
||||
key: [`header${i}`].join('_')
|
||||
key: [`header${i}`].join('_'),
|
||||
},
|
||||
applyPropHooks(api.hooks.getHeaderRowProps, headerGroup, api),
|
||||
props
|
||||
@@ -143,8 +149,8 @@ export const useTable = (props, ...plugins) => {
|
||||
const { path } = row
|
||||
row.getRowProps = props =>
|
||||
mergeProps(
|
||||
{ key: ['row', path].join('_') },
|
||||
applyHooks(api.hooks.getRowProps, row, api),
|
||||
{ key: ['row', ...path].join('_') },
|
||||
applyPropHooks(api.hooks.getRowProps, row, api),
|
||||
props
|
||||
)
|
||||
|
||||
@@ -158,14 +164,14 @@ export const useTable = (props, ...plugins) => {
|
||||
const cell = {
|
||||
column,
|
||||
row,
|
||||
value: row.values[column.id]
|
||||
value: row.values[column.id],
|
||||
}
|
||||
|
||||
cell.getCellProps = props => {
|
||||
const columnPathStr = [path, column.id].join('_')
|
||||
return mergeProps(
|
||||
{
|
||||
key: ['cell', columnPathStr].join('_')
|
||||
key: ['cell', columnPathStr].join('_'),
|
||||
},
|
||||
applyPropHooks(api.hooks.getCellProps, cell, api),
|
||||
props
|
||||
@@ -181,7 +187,7 @@ export const useTable = (props, ...plugins) => {
|
||||
return flexRender(column[type], {
|
||||
...api,
|
||||
...cell,
|
||||
...userProps
|
||||
...userProps,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -193,7 +199,7 @@ export const useTable = (props, ...plugins) => {
|
||||
mergeProps(applyPropHooks(api.hooks.getTableProps, api), userProps)
|
||||
|
||||
api.getRowProps = userProps =>
|
||||
mergeProps(applyPropHooks(api.hooks.getRowProps, api), userProps)
|
||||
mergeProps(applyPropHooks(api.hooks.getRowProps, undefined, api), userProps)
|
||||
|
||||
return api
|
||||
}
|
||||
|
||||
+25
-16
@@ -1,4 +1,4 @@
|
||||
import { useState, useMemo } from 'react'
|
||||
import React from 'react'
|
||||
|
||||
export const defaultState = {}
|
||||
|
||||
@@ -6,29 +6,38 @@ const defaultReducer = (old, newState) => newState
|
||||
|
||||
export const useTableState = (
|
||||
initialState = {},
|
||||
overrides = {},
|
||||
{ reducer = defaultReducer, useState: userUseState = useState } = {}
|
||||
overrides,
|
||||
{ reducer = defaultReducer, useState: userUseState = React.useState } = {}
|
||||
) => {
|
||||
let [state, setState] = userUseState({
|
||||
...defaultState,
|
||||
...initialState
|
||||
...initialState,
|
||||
})
|
||||
|
||||
const overriddenState = useMemo(() => {
|
||||
const overriddenState = React.useMemo(() => {
|
||||
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
|
||||
}, [state, ...Object.values(overrides)])
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [overrides, state])
|
||||
|
||||
const reducedSetState = (updater, type) =>
|
||||
setState(old => {
|
||||
const newState = updater(old)
|
||||
return reducer(old, newState, type)
|
||||
})
|
||||
const reducedSetState = React.useCallback(
|
||||
(updater, type) =>
|
||||
setState(old => {
|
||||
const newState = updater(old)
|
||||
return reducer(old, newState, type)
|
||||
}),
|
||||
[reducer, setState]
|
||||
)
|
||||
|
||||
return [overriddenState, reducedSetState]
|
||||
return React.useMemo(() => [overriddenState, reducedSetState], [
|
||||
overriddenState,
|
||||
reducedSetState,
|
||||
])
|
||||
}
|
||||
|
||||
@@ -46,6 +46,6 @@ export const useTokenPagination = () => {
|
||||
nextPage,
|
||||
canPreviousPage,
|
||||
canNextPage,
|
||||
resetPagination
|
||||
resetPagination,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export { useTable } from './hooks/useTable'
|
||||
export { useColumns } from './hooks/useColumns'
|
||||
export { useRows } from './hooks/useRows'
|
||||
export { useSimpleLayout } from './hooks/useSimpleLayout'
|
||||
export { useExpanded } from './hooks/useExpanded'
|
||||
export { useFilters } from './hooks/useFilters'
|
||||
export { useGroupBy } from './hooks/useGroupBy'
|
||||
|
||||
+9
-9
@@ -70,8 +70,8 @@ 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())
|
||||
.toLowerCase()
|
||||
.includes(String(value).toLowerCase())
|
||||
: true
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ export function setBy(obj = {}, path, value) {
|
||||
depth === path.length - 1 ? value : recurse(target, depth + 1)
|
||||
return {
|
||||
...obj,
|
||||
[key]: subValue
|
||||
[key]: subValue,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,11 +95,11 @@ export function getElementDimensions(element) {
|
||||
const style = window.getComputedStyle(element)
|
||||
const margins = {
|
||||
left: parseInt(style.marginLeft),
|
||||
right: parseInt(style.marginRight)
|
||||
right: parseInt(style.marginRight),
|
||||
}
|
||||
const padding = {
|
||||
left: parseInt(style.paddingLeft),
|
||||
right: parseInt(style.paddingRight)
|
||||
right: parseInt(style.paddingRight),
|
||||
}
|
||||
return {
|
||||
left: Math.ceil(rect.left),
|
||||
@@ -111,7 +111,7 @@ export function getElementDimensions(element) {
|
||||
marginRight: margins.right,
|
||||
paddingLeft: padding.left,
|
||||
paddingRight: padding.right,
|
||||
scrollWidth: element.scrollWidth
|
||||
scrollWidth: element.scrollWidth,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,9 +134,9 @@ export const mergeProps = (...groups) => {
|
||||
...rest,
|
||||
style: {
|
||||
...(props.style || {}),
|
||||
...style
|
||||
...style,
|
||||
},
|
||||
className: [props.className, className].filter(Boolean).join(' ')
|
||||
className: [props.className, className].filter(Boolean).join(' '),
|
||||
}
|
||||
})
|
||||
return props
|
||||
@@ -152,7 +152,7 @@ export const warnUnknownProps = props => {
|
||||
if (Object.keys(props).length) {
|
||||
throw new Error(
|
||||
`Unknown options passed to useReactTable:
|
||||
|
||||
|
||||
${JSON.stringify(props, null, 2)}`
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user