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

Enable functionality to limit maximum number of rows to select #825

Merged
merged 10 commits into from
Aug 22, 2019

Conversation

endrjuskr
Copy link
Contributor

@endrjuskr endrjuskr commented Aug 9, 2019

Closes #751.

Interface change:

Before:

isRowSelectable: (dataIndex) => boolean;

Now:

isRowSelectable: (dataIndex, selectedRows) => boolean;

@coveralls
Copy link

coveralls commented Aug 9, 2019

Coverage Status

Coverage increased (+0.07%) to 75.421% when pulling 80b2347 on endrjuskr:feature/issue-751 into a5ab0da on gregnb:master.

@endrjuskr endrjuskr marked this pull request as ready for review August 10, 2019 11:33
@endrjuskr
Copy link
Contributor Author

Question: How we would like to handle a situation (quite common) when a maximum number of selected rows is lower than a number of rows and a user clicks on the header (select all)?

Current behavior: Selects all and unclicking any of row disables this row to be selected again.

@gabrielliwerant
Copy link
Collaborator

gabrielliwerant commented Aug 13, 2019

Hello @endrjuskr. Thank you for taking an interest in contributing to the project!

I've been thinking about this feature, and I think what I'd like to do instead, is extend the functionality of isRowSelectable to include the selectedRows object. This way, devs can use that information to insert any custom logic that they want to enable/disable selecting a particular row. As of this moment, isRowSelectable only tracks the dataIndex of the current row about to be selected. Although all the selected rows could be tracked separately by the dev, adding this argument would make it easier.

The advantage this has is that it allows customization based on any logic the dev wants, and it extends an already existing API rather than create a new one. I'm a little wary of creating new APIs if we can use existing ones to do the same job, as I want to be careful about creating an overall API that is so big and complex that it's difficult to reason about and lacks discoverability for those new to the project.

How does that sound?

@endrjuskr
Copy link
Contributor Author

Sounds reasonable. To be honest, I was not fully convinced about the approach that I proposed, mostly due to selecting all rows from header.

@endrjuskr endrjuskr changed the title Introduce maxSelectedRows Enable functionality to limit maximum number of rows to select Aug 14, 2019
@gabrielliwerant gabrielliwerant added the needs review Useful to mark PRs for what's up next to review label Aug 14, 2019
Copy link
Collaborator

@gabrielliwerant gabrielliwerant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking this on. There are a couple things to add before we can merge it, including an additional change needed on line https://github.com/gregnb/mui-datatables/blob/master/src/MUIDataTable.js#L1033 to make sure we're passing in the argument when updating rows:

const selected = isRowSelectable ? isRowSelectable(displayData[i].dataIndex, prevState.selectedRows) : true;

src/MUIDataTable.js Outdated Show resolved Hide resolved
webpack.config.js Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
test/MUIDataTableBody.test.js Outdated Show resolved Hide resolved
@gabrielliwerant gabrielliwerant added needs work and removed needs review Useful to mark PRs for what's up next to review labels Aug 15, 2019
@gabrielliwerant
Copy link
Collaborator

@endrjuskr Almost there! We're still missing the selectedRows argument in what is now this line: https://github.com/gregnb/mui-datatables/blob/master/src/MUIDataTable.js#L1034.

README.md Outdated Show resolved Hide resolved
@gabrielliwerant gabrielliwerant added needs review Useful to mark PRs for what's up next to review and removed needs work labels Aug 16, 2019
@gabrielliwerant gabrielliwerant added needs work and removed needs review Useful to mark PRs for what's up next to review labels Aug 16, 2019
@gabrielliwerant
Copy link
Collaborator

Hey @endrjuskr, thanks for the update, however, if you look at the comment, I was actually referring to the test on line 274. :)

@endrjuskr
Copy link
Contributor Author

I hope this time I fixed correct test :)

Also split one test to check exactly if isRowSelectable actually determines selectability of row.

@@ -493,4 +533,69 @@ describe('<TableBody />', function() {

expect(html).to.contain('Test_Text');
});

it('should isRowSelectable be used to determine if row is selectable', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think we can eliminate this test, or recombine it. (or change it as I'll discuss below). It looks like it has a couple of issues once separated out. Mainly, it just seems to be testing whether or not the isRowSelectable function we create here in the test has the proper return values, which basically means that the test is testing itself. It doesn't test that the table works because it doesn't simulate an interaction with the table select rows.

So, if we just want to show that the function is called, the test below (on line 571) does that, so we could combine it back with that one.

If instead we want to show the positive case, where we demonstrate that a click on a selected row that is allowed by isRowSelectable works, then I think we should model it after the one on line 274 and simulate the clicks.

I leave it to you as for how you'd like to resolve this one.

Copy link
Collaborator

@gabrielliwerant gabrielliwerant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you so much for sticking through it and contributing.

@gabrielliwerant gabrielliwerant merged commit 06a369d into gregnb:master Aug 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Limit checkbox based selection for more than 'n' rows
3 participants