Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataTable] TableBatchActions remains open when selected rows are removed from the tree #9586

Closed
1 task done
FrivalszkyP opened this issue Sep 2, 2021 · 2 comments · Fixed by #9679
Closed
1 task done

Comments

@FrivalszkyP
Copy link
Contributor

FrivalszkyP commented Sep 2, 2021

TableBatchActions remains open when selected rows are removed from the tree: it should take selectedRows.length into account

What package(s) are you using?

  • carbon-components-react

Detailed description

Related component: DataTable.

So there’s the TableBatchActions component which opens when you select a row with TableSelectRow. The whole logic is handled internally by DataTable via the shouldShowBatchActions state variable. It works beautifully.

I have a weird edge case, though: in some of our DataTables, we provide the ability to remove the selected rows. Because the rows get removed and not unselected, the TableBatchActions stays open with 0 rows selected.

Looking at how DataTable is implemented, I think it looks like this is a bug. Let me show you why I came to this conclusion.

For the onSelectRow handler, there is this line of code:

https://github.com/carbon-design-system/carbon/blob/main/packages/react/src/components/DataTable/DataTable.js#L573

This says that the TableBatchActions should remain open if the currently handled TableSelectRow is being unclicked BUT there are other TableSelectRows remaining clicked. So this tells me that the TableBatchActions is expected to be closed when the remaining selected rows count is zero.

So my proposed solution is this. The getBatchActionProps method here:

https://github.com/carbon-design-system/carbon/blob/main/packages/react/src/components/DataTable/DataTable.js#L394

Should be changed to this:


  getBatchActionProps = (props = {}) => {
    const { shouldShowBatchActions } = this.state;
    const totalSelected = this.getSelectedRows().length;
    return {
      ...props,
      shouldShowBatchActions: shouldShowBatchActions && !!totalSelected,
      totalSelected,
      onCancel: this.handleOnCancel,
    };
  };

Steps to reproduce the issue

  1. Open CodeSandbox here: https://codesandbox.io/s/still-wave-zv3kf?file=/src/index.js
  2. Select one row
  3. Click Remove

Expected result

The TableBatchActions closes automatically

Actual result

The TableBatchActions stays open with no elements selected.

@dakahn
Copy link
Contributor

dakahn commented Sep 8, 2021

@FrivalszkyP Excellent writeup. Makes sense to me! Did you have the bandwidth to submit a PR for this change?

@FrivalszkyP
Copy link
Contributor Author

@dakahn I can certainly do it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants