From 18f2dea24747c469577b7795d68e3c19533b3eef Mon Sep 17 00:00:00 2001 From: ggascoigne Date: Mon, 1 Jul 2019 08:43:46 -0700 Subject: [PATCH] Fix lint errors from react-hooks/exhaustive-deps (#1371) They all seemed like reasonable warnings, this squashes them. --- .eslintrc | 31 +++-- package.json | 3 +- src/hooks/useColumns.js | 263 +++++++++++++++++++------------------ src/hooks/useFilters.js | 2 +- src/hooks/useGroupBy.js | 2 +- src/hooks/usePagination.js | 6 +- src/hooks/useRows.js | 2 +- 7 files changed, 159 insertions(+), 150 deletions(-) diff --git a/.eslintrc b/.eslintrc index 23d57a2..eaee486 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,14 +1,19 @@ { - "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"], + "env": { + "es6": true + }, + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "space-before-function-paren": 0, + "react/jsx-boolean-value": 0 + }, + "settings": { + "react": { + "version": "latest" + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index d9c125a..8455a73 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "start": "rollup -c -w", "prepare": "yarn build", "release": "yarn publish", - "releaseNext": "yarn publish --tag next" + "releaseNext": "yarn publish --tag next", + "lint": "eslint 'src/**/*.js'" }, "files": [ "src/", diff --git a/src/hooks/useColumns.js b/src/hooks/useColumns.js index 1a86b20..2074d71 100755 --- a/src/hooks/useColumns.js +++ b/src/hooks/useColumns.js @@ -13,6 +13,137 @@ const propTypes = { ) } +// 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, @@ -45,7 +176,7 @@ export const useColumns = props => { headerGroups, headers } - }, [groupBy, userColumns]) + }, [debug, groupBy, userColumns]) return { ...props, @@ -54,60 +185,6 @@ export const useColumns = props => { 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) { const flatColumns = [] @@ -126,80 +203,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() - } } diff --git a/src/hooks/useFilters.js b/src/hooks/useFilters.js index 1948afc..b6c9394 100755 --- a/src/hooks/useFilters.js +++ b/src/hooks/useFilters.js @@ -150,7 +150,7 @@ export const useFilters = props => { } return filterRows(rows) - }, [rows, filters, manualFilters]) + }, [manualFilters, filters, debug, rows, columns, filterFn]) return { ...props, diff --git a/src/hooks/useGroupBy.js b/src/hooks/useGroupBy.js index 0fe3693..3d36d90 100755 --- a/src/hooks/useGroupBy.js +++ b/src/hooks/useGroupBy.js @@ -180,7 +180,7 @@ 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, diff --git a/src/hooks/usePagination.js b/src/hooks/usePagination.js index 5e4a438..0cf6ab8 100755 --- a/src/hooks/usePagination.js +++ b/src/hooks/usePagination.js @@ -38,7 +38,10 @@ export const usePagination = props => { ] } = props + const pageOptions = useMemo(() => [...new Array(userPageCount)].map((d, i) => i), [userPageCount]) + const rowDep = disablePageResetOnDataChange ? null : rows + useLayoutEffect(() => { setState( old => ({ @@ -76,9 +79,8 @@ export const usePagination = props => { pageCount, 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 diff --git a/src/hooks/useRows.js b/src/hooks/useRows.js index e0a54dd..978fd9f 100755 --- a/src/hooks/useRows.js +++ b/src/hooks/useRows.js @@ -57,7 +57,7 @@ export const useRows = props => { // Use the resolved data return data.map((d, i) => accessRow(d, i)) - }, [data, columns]) + }, [debug, data, subRowsKey, columns]) return { ...props,