Compare commits

...
Author SHA1 Message Date
tannerlinsley 07fb9529e4 v7.0.0-alpha.7 2019-07-15 11:29:50 -06:00
DomenuchandTanner Linsley b5d399efd6 Fixed grouped column Header that were not adjusting based on hidden child columns (#1381)
* added assertion to check for show property inside column

* reverted previous change

* added a filter to weed out non-visible columns when calculating the size for grouped Headers

* added another case to the previous filter to factor in blank grouped Headers
2019-07-15 11:28:22 -06:00
DomenuchandTanner Linsley 14e931548a added undefined to the unspecific getRowProps fn to retain the param order (#1392) 2019-07-15 11:27:13 -06:00
Larry BothaandTanner Linsley bc3ea07c3e Feature/add jest configs ref: #1383 (#1385)
* add deps for testing

* feat(tests): add jest configs

* uncomment module directories path
2019-07-15 11:25:32 -06:00
tannerlinsley 4a3929cd50 Merge branch 'master' of https://github.com/react-tools/react-table 2019-07-15 09:33:04 -06:00
Larry BothaandTanner Linsley e2bb09d8b2 Feature/add prettier config, ref #1383 (#1384)
* add prettier config

* write files with prettier

* install and configure lint-staged and husky - ref 1.2 in #1383

* feat(style): add prettier configs, ref 1.1 & 1.2 in #1383
2019-07-01 21:03:57 -06:00
ggascoigneandTanner Linsley 18f2dea247 Fix lint errors from react-hooks/exhaustive-deps (#1371)
They all seemed like reasonable warnings, this squashes them.
2019-07-01 09:43:46 -06:00
ggascoigneandTanner Linsley ead3599378 Fix pagination resetting to page zero with manualPagination (#1369)
* Fix pagination resetting to page zero with manualPagination

To be honest I'm not sure what this useLayoutEffect is there to do.
It has no visible effect if you don't use manualPagination, and if you
do it, simply jumps you back to the first page, defeating the point of
you having control of the pagination.

* Conditionally reset page on data change

Rather disable the whole page reset when filters, groupBy or sortBy
change, just because I wanted to disable the page reset on data change,
make that bit be conditional.

With that in mind, usePagination now accepts a
disablePageResetOnDataChange parameter.
2019-07-01 09:36:42 -06:00
Adrien DenatandTanner Linsley addf28a011 Fix useExpanded wrongly flattening subRows (#1372)
* Fix useExpanded wrongly flattening subRows

* Add new paginateSubRows option defaulting to true
2019-06-21 10:00:59 -06:00
Adrien DenatandTanner Linsley e4429b7143 Add support for es module build (#1374) 2019-06-21 09:43:05 -06:00
tannerlinsley 4b2d0eb187 v7.0.0-alpha.6 2019-06-20 15:24:02 -06:00
ggascoigneandTanner Linsley 5369051b05 Provide option to toggle sort like V6 (#1370)
V7 adds the option to remove a sort option, so it goes from asc -> desc
-> unset, then repeats.  V6 just went from asc -> desc then repeated.

Personally I much preferred this, I think that there's a case to be made
that this is the more expected behavior.

I'm not sure if this is really the best way to fix this since it adds
yet another api option and I completely understand that that is less
than desirable, but I also would rather add an option than have to
duplicate the whole useSortBy hook.
2019-06-20 12:50:36 -06:00
DomenuchandTanner Linsley 58d38b668b changed applyHook to applyPropHooks inside getRowProps mergeProps fn (#1367) 2019-06-20 12:49:04 -06:00
tannerlinsley 9de149cf3c v7.0.0-alpha.5 2019-06-11 11:41:38 -06:00
tannerlinsley 289dca1caf Merge branch 'master' of https://github.com/react-tools/react-table 2019-06-11 11:41:07 -06:00
HellycatandTanner Linsley c6167566da Fix misspelled in propTypes (#1353) 2019-05-31 07:18:26 -06:00
Dmitrii GaidarjiandTanner Linsley 5b0e68aead Fix defaultSortDesc blocking toggle action (#1341)
* Fix defaultSortDesc blocking toggle action

* Revert "Fix defaultSortDesc blocking toggle action"

This reverts commit 15e05cefd820e3f63e80a8e9d31b9431f295b55a.

* Fix defaultSortDesc blocking toggle action
2019-05-31 07:17:58 -06:00
27 changed files with 9884 additions and 4859 deletions
+20 -12
View File
@@ -1,14 +1,22 @@
{
"parser": "babel-eslint",
"extends": ["react-app", "prettier"],
"env": {
"es6": true
},
"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"
}
}
}
+5
View File
@@ -0,0 +1,5 @@
module.exports = {
hooks: {
'pre-commit': 'lint-staged',
},
}
+17
View File
@@ -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],
};
+13
View File
@@ -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'],
};
+15
View File
@@ -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',
},
};
View File
+16
View File
@@ -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',
],
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'*.js': ['prettier --write', 'git add'],
}
+6023 -4491
View File
File diff suppressed because it is too large Load Diff
+13 -5
View File
@@ -1,6 +1,6 @@
{
"name": "react-table",
"version": "7.0.0-alpha.4",
"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,6 +47,7 @@
"@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": "9.x",
"cross-env": "^5.1.4",
@@ -63,6 +65,12 @@
"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",
+6
View File
@@ -0,0 +1,6 @@
module.exports = {
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true,
}
+30 -21
View File
@@ -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
View File
@@ -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()
}
}
+13 -8
View File
@@ -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
View File
@@ -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,
}
}
+13 -12
View File
@@ -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,8 +18,8 @@ export const useFlexLayout = props => {
getRowProps,
getHeaderRowProps,
getHeaderProps,
getCellProps
}
getCellProps,
},
} = props
columnsHooks.push((columns, api) => {
@@ -47,8 +47,8 @@ export const useFlexLayout = props => {
const rowStyles = {
style: {
display: 'flex',
minWidth: `${sumWidth}px`
}
minWidth: `${sumWidth}px`,
},
}
api.rowStyles = rowStyles
@@ -59,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,
@@ -80,8 +80,8 @@ export const useFlexLayout = props => {
defaultFlex,
undefined,
api
)
}
),
},
// [refKey]: el => {
// renderedCellInfoRef.current[columnPathStr] = {
// column,
@@ -110,7 +110,7 @@ function getStylesForColumn(column, columnMeasurements, defaultFlex, api) {
return {
flex: `${flex} 0 auto`,
width: `${width}px`,
maxWidth: `${maxWidth}px`
maxWidth: `${maxWidth}px`,
}
}
@@ -122,6 +122,7 @@ function getSizesForColumn(
) {
if (columns) {
columns = columns
.filter(col => col.show || col.visible)
.map(column =>
getSizesForColumn(column, columnMeasurements, defaultFlex, api)
)
@@ -138,7 +139,7 @@ function getSizesForColumn(
return {
flex,
width,
maxWidth
maxWidth,
}
}
@@ -148,7 +149,7 @@ function getSizesForColumn(
width === 'auto'
? columnMeasurements[id] || defaultFlex
: getFirstDefined(width, minWidth, defaultFlex),
maxWidth
maxWidth,
}
}
+27 -19
View File
@@ -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,
}
}
+21 -14
View File
@@ -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
)
}, [rows, 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)
}
@@ -126,6 +133,6 @@ export const usePagination = props => {
nextPage,
setPageSize,
pageIndex,
pageSize
pageSize,
}
}
+4 -4
View File
@@ -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 => {
@@ -29,7 +29,7 @@ export const useRows = props => {
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)
@@ -57,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,
}
}
+5 -5
View File
@@ -1,14 +1,14 @@
export const useSimpleLayout = props => {
const {
hooks: { columns: columnsHooks, getHeaderProps, getCellProps }
hooks: { columns: columnsHooks, getHeaderProps, getCellProps },
} = props
columnsHooks.push(columns => {
getHeaderProps.push(column => ({
style: {
boxSizing: 'border-box',
width: column.width !== undefined ? `${column.width}px` : 'auto'
}
width: column.width !== undefined ? `${column.width}px` : 'auto',
},
}))
getCellProps.push(cell => {
@@ -16,8 +16,8 @@ export const useSimpleLayout = props => {
style: {
boxSizing: 'border-box',
width:
cell.column.width !== undefined ? `${cell.column.width}px` : 'auto'
}
cell.column.width !== undefined ? `${cell.column.width}px` : 'auto',
},
}
})
+22 -17
View File
@@ -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,8 +45,9 @@ export const useSortBy = props => {
manualSorting,
disableSorting,
defaultSortDesc,
disableSortRemove,
hooks,
state: [{ sortBy }, setState]
state: [{ sortBy }, setState],
} = props
columns.forEach(column => {
@@ -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)
}
@@ -176,9 +181,9 @@ export const useSortBy = props => {
}
: undefined,
style: {
cursor: canSortBy ? 'pointer' : undefined
cursor: canSortBy ? 'pointer' : undefined,
},
title: 'Toggle SortBy'
title: 'Toggle SortBy',
},
applyPropHooks(api.hooks.getSortByToggleProps, column, api),
props
@@ -251,6 +256,6 @@ export const useSortBy = props => {
return {
...props,
rows: sortedRows
rows: sortedRows,
}
}
+11 -11
View File
@@ -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')
@@ -83,7 +83,7 @@ export const useTable = (props, ...plugins) => {
return flexRender(column[type], {
...api,
...column,
...userProps
...userProps,
})
}
@@ -92,7 +92,7 @@ export const useTable = (props, ...plugins) => {
mergeProps(
{
key: ['header', column.id].join('_'),
colSpan: column.columns ? column.columns.length : 1
colSpan: column.columns ? column.columns.length : 1,
},
applyPropHooks(api.hooks.getHeaderProps, column, api),
props
@@ -125,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
@@ -150,7 +150,7 @@ export const useTable = (props, ...plugins) => {
row.getRowProps = props =>
mergeProps(
{ key: ['row', ...path].join('_') },
applyHooks(api.hooks.getRowProps, row, api),
applyPropHooks(api.hooks.getRowProps, row, api),
props
)
@@ -164,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
@@ -187,7 +187,7 @@ export const useTable = (props, ...plugins) => {
return flexRender(column[type], {
...api,
...cell,
...userProps
...userProps,
})
}
@@ -199,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
}
+3 -3
View File
@@ -11,12 +11,12 @@ export const useTableState = (
) => {
let [state, setState] = userUseState({
...defaultState,
...initialState
...initialState,
})
const overriddenState = React.useMemo(() => {
const newState = {
...state
...state,
}
if (overrides) {
Object.keys(overrides).forEach(key => {
@@ -38,6 +38,6 @@ export const useTableState = (
return React.useMemo(() => [overriddenState, reducedSetState], [
overriddenState,
reducedSetState
reducedSetState,
])
}
+1 -1
View File
@@ -46,6 +46,6 @@ export const useTokenPagination = () => {
nextPage,
canPreviousPage,
canNextPage,
resetPagination
resetPagination,
}
}
+9 -9
View File
@@ -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)}`
)
}
View File
+3440 -73
View File
File diff suppressed because it is too large Load Diff