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

[DataGrid] Emit onSelectionModelChange when selection state changes #1966

Conversation

DanailH
Copy link
Member

@DanailH DanailH commented Jun 25, 2021

Fixes #1782

@DanailH DanailH added the bug 🐛 Something doesn't work label Jun 25, 2021
@DanailH DanailH self-assigned this Jun 25, 2021
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

👍 for this behavior, it also fixes the uncontrolled behavior.

packages/grid/x-grid/src/tests/selection.XGrid.test.tsx Outdated Show resolved Hide resolved
packages/grid/x-grid/src/tests/selection.XGrid.test.tsx Outdated Show resolved Hide resolved
@oliviertassinari oliviertassinari added the component: data grid This is the name of the generic UI component, not the React module! label Jun 25, 2021
@DanailH DanailH requested a review from oliviertassinari June 28, 2021 10:59
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

Looks good

packages/grid/x-grid/src/tests/selection.XGrid.test.tsx Outdated Show resolved Hide resolved
@dtassone
Copy link
Member

not sure how this fix is going to fit with my control state PR, it seems redundant, and we will have to clean these changes

@DanailH
Copy link
Member Author

DanailH commented Jun 30, 2021

@dtassone It's not a fix per se but rather a missed interaction that should emit the onSelectionModelChange.

@DanailH
Copy link
Member Author

DanailH commented Jun 30, 2021

Well if that is the case then why is the linked ticket assigned to me, we are essentially doing double work here...

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 30, 2021

The PR looks solid, I would personally 👍 for a merge: to encourage moving one step at a time. The rebase in #1823 will likely be easy, and if the logic is correct, the test will still pass. If the test doesn't pass it should likely be #1823 responsibility to fix it. I would actually hope that an extra constraint from here will help #1823 tailor the solution to reality.

@DanailH
Copy link
Member Author

DanailH commented Jul 1, 2021

@oliviertassinari @dtassone Ok, I applied the test to the control state PR #1823. This is what the test should look like taking into account the changes in the linked PR. The only part that is failing is the part of clearing the selection after loading rows with different IDs.

it('should call onSelectionModelChange when selection state changes', () => {
    const handleSelectionModelChange = spy();
    const { setProps } = render(
      <div style={{ width: 300, height: 300 }}>
        <XGrid 
          columns={[{ field: 'brand' }]}
          rows={[
            {
              id: 0,
              brand: 'Nike',
            },
          ]}
          selectionModel={[0]} 
          onSelectionModelChange={handleSelectionModelChange} />
      </div>
    );
    expect(getSelectedRows(apiRef)).to.deep.equal([0]);
    setProps({
      rows: [
        {
          id: 1,
          brand: 'Nike',
        },
      ],
    });
    expect(handleSelectionModelChange.callCount).to.equal(1);
    expect(handleSelectionModelChange.getCall(0).args[0]).to.deep.equal([]);
    // TODO: Fix - it should clear the selection because the newly loaded rows have different IDs
    expect(getSelectedRows(apiRef)).to.deep.equal([]);
  });

I can either push this directly to the control state PR or @dtassone you can add it manually.

@dtassone
Copy link
Member

dtassone commented Jul 5, 2021

IMO we could merge #1823 and then reapply/fix this issue on top of it

@DanailH DanailH merged commit a7417a5 into mui:master Jul 7, 2021
@oliviertassinari
Copy link
Member

Great 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Prevent the grid from cleaning up the selectionModel when selectionModel prop is provided
3 participants