diff --git a/src/plugin-hooks/useFilters.js b/src/plugin-hooks/useFilters.js index 53851dd..b7f5d0c 100755 --- a/src/plugin-hooks/useFilters.js +++ b/src/plugin-hooks/useFilters.js @@ -128,87 +128,83 @@ export const useFilters = props => { // cache for each row group (top-level rows, and each row's recursive subrows) // This would make multi-filtering a lot faster though. Too far? - const filteredRows = React.useMemo(() => { - if (manualFilters || !Object.keys(filters).length) { - return rows - } + const filteredRows = React.useMemo( + () => { + if (manualFilters || !Object.keys(filters).length) { + return rows + } - if (debug) console.info('getFilteredRows') + if (debug) console.info('getFilteredRows') - // Filters top level and nested rows - const filterRows = rows => { - let filteredRows = rows + // Filters top level and nested rows + const filterRows = rows => { + let filteredRows = rows - filteredRows = Object.entries(filters).reduce( - (filteredSoFar, [columnID, filterValue]) => { - // Find the filters column - const column = columns.find(d => d.id === columnID) + filteredRows = Object.entries(filters).reduce( + (filteredSoFar, [columnID, filterValue]) => { + // Find the filters column + const column = columns.find(d => d.id === columnID) - column.preFilteredRows = filteredSoFar + column.preFilteredRows = filteredSoFar - // Don't filter hidden columns or columns that have had their filters disabled - if (!column || column.filterable === false) { - return filteredSoFar - } + // Don't filter hidden columns or columns that have had their filters disabled + if (!column || column.filterable === false) { + return filteredSoFar + } - const columnFilter = column.filter || filterTypes.text + const columnFilter = column.filter || 'text' - // 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 = getFilterMethod( - columnFilter, - userFilterTypes || {}, - filterTypes - ) - - if (!filterMethod) { - console.warn( - `Could not find a valid 'column.filter' for column with the ID: ${ - column.id - }.` + const filterMethod = getFilterMethod( + columnFilter, + userFilterTypes || {}, + filterTypes ) - return filteredSoFar + + if (!filterMethod) { + console.warn( + `Could not find a valid 'column.filter' for column with the ID: ${ + column.id + }.` + ) + return filteredSoFar + } + + // Pass the rows, id, filterValue and column to the filterMethod + // to get the filtered rows back + return filterMethod(filteredSoFar, columnID, filterValue, column) + }, + rows + ) + + // Apply the filter to any subRows + // We technically could do this recursively in the above loop, + // but that would severely hinder the API for the user, since they + // would be required to do that recursion in some scenarios + filteredRows = filteredRows.map(row => { + if (!row.subRows) { + return row } + return { + ...row, + subRows: filterRows(row.subRows), + } + }) - // Pass the rows, id, filterValue and column to the filterMethod - // to get the filtered rows back - return filterMethod(filteredSoFar, columnID, filterValue, column) - }, - rows - ) + // then filter any rows without subcolumns because it would be strange to show + filteredRows = filteredRows.filter(row => { + if (!row.subRows) { + return true + } + return row.subRows.length > 0 + }) - // Apply the filter to any subRows - // We technically could do this recursively in the above loop, - // but that would severely hinder the API for the user, since they - // would be required to do that recursion in some scenarios - filteredRows = filteredRows.map(row => { - if (!row.subRows) { - return row - } - return { - ...row, - subRows: filterRows(row.subRows), - } - }) + return filteredRows + } - // then filter any rows without subcolumns because it would be strange to show - filteredRows = filteredRows.filter(row => { - if (!row.subRows) { - return true - } - return row.subRows.length > 0 - }) - - return filteredRows - } - - return filterRows(rows) - }, [manualFilters, filters, debug, rows, columns, userFilterTypes]) + return filterRows(rows) + }, + [manualFilters, filters, debug, rows, columns, userFilterTypes] + ) return { ...props,