From 98b3161b2ded0f07425ee4aeff98143a45dfd632 Mon Sep 17 00:00:00 2001 From: Guy Gascoigne-Piggford Date: Fri, 14 Feb 2020 12:14:10 -0800 Subject: [PATCH] Add option to allow toggleRowSelect to not select subRows (#1879) * Add option to allow toggleRowSelect to not select subRows Iif you are using manualGrouping, it's convenient to be able to select the group without selecting all of the children. This adds an instance option `selectChildRows`, which defaults to true, that can be set to false to allow selection of the group without it selecting all of the children. * fix lint error Co-authored-by: Tanner Linsley --- src/plugin-hooks/useRowSelect.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/plugin-hooks/useRowSelect.js b/src/plugin-hooks/useRowSelect.js index 84049cc..3e0a2eb 100644 --- a/src/plugin-hooks/useRowSelect.js +++ b/src/plugin-hooks/useRowSelect.js @@ -112,7 +112,7 @@ function reducer(state, action, previousState, instance) { if (action.type === actions.toggleRowSelected) { const { id, value: setSelected } = action - const { flatGroupedRowsById } = instance + const { flatGroupedRowsById, selectSubRows = true } = instance // Join the ids of deep rows // to make a key, then manage all of the keys @@ -139,7 +139,7 @@ function reducer(state, action, previousState, instance) { } } - if (row.subRows) { + if (selectSubRows && row.subRows) { return row.subRows.forEach(row => handleRowById(row.id)) } } @@ -162,6 +162,7 @@ function useInstance(instance) { flatRows, autoResetSelectedRows = true, state: { selectedRowIds }, + selectSubRows = true, dispatch, } = instance @@ -190,7 +191,9 @@ function useInstance(instance) { const selectedFlatRows = [] rows.forEach(row => { - const isSelected = getRowIsSelected(row, selectedRowIds) + const isSelected = selectSubRows + ? getRowIsSelected(row, selectedRowIds) + : !!selectedRowIds[row.id] row.isSelected = !!isSelected row.isSomeSelected = isSelected === null @@ -200,7 +203,7 @@ function useInstance(instance) { }) return selectedFlatRows - }, [rows, selectedRowIds]) + }, [rows, selectSubRows, selectedRowIds]) let isAllRowsSelected = Boolean( Object.keys(flatRowsById).length && Object.keys(selectedRowIds).length