From c7064de5be480840775039e2e4bdad00fbf8d4ce Mon Sep 17 00:00:00 2001 From: Sangeetha Babu Date: Tue, 24 Sep 2024 14:11:25 +0530 Subject: [PATCH] fix(Datagrid): select all row count updated to exclude disabled rows (#6085) * fix(Datagrid): select all row count updates to exclude disabled rows * fix(datagrid): test fail fix --------- Co-authored-by: Anamika T S <47971732+anamikaanu96@users.noreply.github.com> --- .../src/components/Datagrid/Datagrid.test.js | 3 +-- .../Datagrid/Datagrid/DatagridToolbar.tsx | 13 +++++++++++-- .../src/components/Datagrid/types/index.ts | 5 +++++ .../src/components/Datagrid/useDisableSelectRows.ts | 4 ++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/ibm-products/src/components/Datagrid/Datagrid.test.js b/packages/ibm-products/src/components/Datagrid/Datagrid.test.js index 141b610b6d..27522e2811 100644 --- a/packages/ibm-products/src/components/Datagrid/Datagrid.test.js +++ b/packages/ibm-products/src/components/Datagrid/Datagrid.test.js @@ -1738,8 +1738,7 @@ describe(componentName, () => { .getElementsByTagName('div')[0] .getElementsByTagName('div')[0] .getElementsByTagName('button')[0].textContent - ).toEqual('Select all (100)'); - // ).toEqual('Select all (93)'); (7 rows are disabled in entire table) switch to this after #5937 issue fixes + ).toEqual('Select all (93)'); // click select all button in toolbar fireEvent.click( diff --git a/packages/ibm-products/src/components/Datagrid/Datagrid/DatagridToolbar.tsx b/packages/ibm-products/src/components/Datagrid/Datagrid/DatagridToolbar.tsx index 62015e035d..50b90ac572 100644 --- a/packages/ibm-products/src/components/Datagrid/Datagrid/DatagridToolbar.tsx +++ b/packages/ibm-products/src/components/Datagrid/Datagrid/DatagridToolbar.tsx @@ -18,7 +18,7 @@ import { useResizeObserver } from '../../../global/js/hooks/useResizeObserver'; import { pkg, carbon } from '../../../settings'; import cx from 'classnames'; import { handleSelectAllRowData } from './addons/stateReducer'; -import { DataGridState } from '../types'; +import { DataGridState, DatagridRowProps } from '../types'; const blockClass = `${pkg.prefix}--datagrid__table-toolbar`; @@ -47,10 +47,19 @@ const DatagridBatchActionsToolbar = ( batchActionMenuButtonLabel, translateWithIdBatchActions, } = datagridState; + const [availableRowsCount, setAvailableRowsCount] = useState(rows.length); + const batchActionMenuButtonLabelText = batchActionMenuButtonLabel ?? 'More'; const selectedKeys = Object.keys(selectedRowIds || {}); const totalSelected = selectedKeys.length; + useEffect(() => { + const countDisabledRows = + (rows.find((row) => row.getRowProps)?.getRowProps?.() as DatagridRowProps) + ?.nonselectablerows?.length || 0; + rows && setAvailableRowsCount(rows.length - countDisabledRows); + }, [rows]); + // Get initial width of batch actions container, // used to measure when all items are put inside // the ButtonMenu @@ -183,7 +192,7 @@ const DatagridBatchActionsToolbar = ( totalSelected={totalSelected} onCancel={onCancelHandler} onSelectAll={onSelectAllHandler} - totalCount={rows && rows.length} + totalCount={availableRowsCount} translateWithId={translateWithIdBatchActions} > {!displayAllInMenu && diff --git a/packages/ibm-products/src/components/Datagrid/types/index.ts b/packages/ibm-products/src/components/Datagrid/types/index.ts index f046bd8ad6..8dc68c789c 100644 --- a/packages/ibm-products/src/components/Datagrid/types/index.ts +++ b/packages/ibm-products/src/components/Datagrid/types/index.ts @@ -11,6 +11,7 @@ import { RadioButtonProps } from '@carbon/react/lib/components/RadioButton/Radio import { RadioButtonGroupProps } from '@carbon/react/lib/components/RadioButtonGroup/RadioButtonGroup'; import { CheckboxProps } from '@carbon/react/lib/components/Checkbox'; import { NumberInputProps } from '@carbon/react/lib/components/NumberInput/NumberInput'; +import { TableRowProps } from 'react-table'; import React, { CSSProperties, @@ -373,3 +374,7 @@ export interface PropGetterMeta { instance?: DataGridTableInstance; row?: Partial & DatagridRow>; } + +export interface DatagridRowProps extends TableRowProps { + nonselectablerows: Array; +} diff --git a/packages/ibm-products/src/components/Datagrid/useDisableSelectRows.ts b/packages/ibm-products/src/components/Datagrid/useDisableSelectRows.ts index 23e47a6f28..f9d95bc674 100644 --- a/packages/ibm-products/src/components/Datagrid/useDisableSelectRows.ts +++ b/packages/ibm-products/src/components/Datagrid/useDisableSelectRows.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ import { Hooks, Row, RowPropGetter, TableRowProps } from 'react-table'; -import { DatagridRow, PropGetterMeta } from './types'; +import { DatagridRow, PropGetterMeta, DatagridRowProps } from './types'; const nonselectablerowsList = (instance) => { const nonselectablerows: number[] = @@ -39,7 +39,7 @@ const useDisableSelectRows = (hooks: Hooks) => { disabled: instance?.shouldDisableSelectRow?.(row), nonselectablerows, }, - ] as Partial[]; + ] as Partial[]; }; hooks.getRowProps.push(getRowProps); };