From 4842bc061d3421f297c6ab30809d17ba6bd7c642 Mon Sep 17 00:00:00 2001 From: tannerlinsley Date: Mon, 7 Oct 2019 10:27:18 -0600 Subject: [PATCH] fix: fix includeAll filter type, add filteredRows --- docs/api.md | 5 ++++- src/filterTypes.js | 6 +++++- src/plugin-hooks/useFilters.js | 10 +++++++++- src/plugin-hooks/useRowSelect.js | 31 ++++++++++++++++++++++++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/docs/api.md b/docs/api.md index 384a127..4087440 100644 --- a/docs/api.md +++ b/docs/api.md @@ -527,9 +527,12 @@ The following properties are available on every `Column` object returned by the - An column-level function used to update the filter value for this column - `filterValue: any` - The current filter value for this column, resolved from the table state's `filters` object -- `preFilteredColumnRows: Array` +- `preFilteredRows: Array` - The array of rows that were originally passed to this columns filter **before** they were filtered. - This array of rows can be useful if building faceted filter options. +- `filteredRows: Array` + - The resulting array of rows received from this columns filter **after** they were filtered. + - This array of rows can be useful if building faceted filter options. ### Example diff --git a/src/filterTypes.js b/src/filterTypes.js index ad785b5..e409b62 100644 --- a/src/filterTypes.js +++ b/src/filterTypes.js @@ -44,7 +44,11 @@ includes.autoRemove = val => !val || !val.length export const includesAll = (rows, id, filterValue) => { return rows.filter(row => { const rowValue = row.values[id] - return filterValue.every(val => rowValue.includes(val)) + return ( + rowValue && + rowValue.length && + filterValue.every(val => rowValue.includes(val)) + ) }) } diff --git a/src/plugin-hooks/useFilters.js b/src/plugin-hooks/useFilters.js index 0e130b2..5a22f9a 100755 --- a/src/plugin-hooks/useFilters.js +++ b/src/plugin-hooks/useFilters.js @@ -177,7 +177,14 @@ function useMain(instance) { // Pass the rows, id, filterValue and column to the filterMethod // to get the filtered rows back - return filterMethod(filteredSoFar, columnID, filterValue, column) + column.filteredRows = filterMethod( + filteredSoFar, + columnID, + filterValue, + column + ) + + return column.filteredRows }, rows ) @@ -228,6 +235,7 @@ function useMain(instance) { // using every column's preFilteredRows value nonFilteredColumns.forEach(column => { column.preFilteredRows = filteredRows + column.filteredRows = filteredRows }) }, [filteredRows, filters, flatColumns]) diff --git a/src/plugin-hooks/useRowSelect.js b/src/plugin-hooks/useRowSelect.js index f9ab9ed..334a44d 100644 --- a/src/plugin-hooks/useRowSelect.js +++ b/src/plugin-hooks/useRowSelect.js @@ -1,7 +1,12 @@ import React from 'react' import PropTypes from 'prop-types' -import { mergeProps, applyPropHooks, ensurePluginOrder } from '../utils' +import { + mergeProps, + applyPropHooks, + ensurePluginOrder, + safeUseLayoutEffect, +} from '../utils' import { addActions, actions } from '../actions' import { defaultState } from '../hooks/useTable' @@ -57,8 +62,10 @@ function useMain(instance) { const { hooks, manualRowSelectedKey = 'isSelected', + disableSelectedRowsResetOnDataChange, plugins, flatRows, + data, state: { selectedRowPaths }, setState, } = instance @@ -80,6 +87,28 @@ function useMain(instance) { } } + const isRowSelectedMountedRef = React.useRef() + + // Bypass any effects from firing when this changes + const disableSelectedRowsResetOnDataChangeRef = React.useRef() + disableSelectedRowsResetOnDataChangeRef.current = disableSelectedRowsResetOnDataChange + + safeUseLayoutEffect(() => { + if ( + isRowSelectedMountedRef.current && + !disableSelectedRowsResetOnDataChangeRef.current + ) { + setState( + old => ({ + ...old, + selectedRowPaths: [], + }), + actions.pageChange + ) + } + isRowSelectedMountedRef.current = true + }, [setState, data]) + const toggleRowSelectedAll = set => { setState(old => { const selectAll = typeof set !== 'undefined' ? set : !isAllRowsSelected