Repository: gregnb/mui-datatables
Pull request #768 (2019-07-14 04:43:58)

Fix issue with selecting the proper row after sorting
Fixes https://github.com/gregnb/mui-datatables/issues/767.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/24554764/badge)](https://coveralls.io/builds/24554764)

Coverage remained the same at 75.76% when pulling **8678a9ae756f86a673f1bcde95033a73c3273ce8 on bugfix/issue-767-sorting-and-row-selection** into **d09ed0c73257b9b4846b5ac6fb6c0da469b766fe on master**.


Patch:
diff --git a/src/MUIDataTable.js b/src/MUIDataTable.js
index c6a04e7..72e80c6 100644
--- a/src/MUIDataTable.js
+++ b/src/MUIDataTable.js
@@ -990,7 +990,7 @@ class MUIDataTable extends React.Component {
           let rowPos = -1;
 
           for (let cIndex = 0; cIndex < selectedRows.length; cIndex++) {
-            if (selectedRows[cIndex].index === index) {
+            if (selectedRows[cIndex].dataIndex === dataIndex) {
               rowPos = cIndex;
               break;
             }
diff --git a/src/components/TableBody.js b/src/components/TableBody.js
index eee66db..d6202f3 100644
--- a/src/components/TableBody.js
+++ b/src/components/TableBody.js
@@ -122,7 +122,7 @@ class TableBody extends React.Component {
     }
 
     // Check if we should toggle row select when row is clicked anywhere
-    if (this.props.options.selectableRowsOnClick && this.props.options.selectableRows !== 'none') {
+    if (this.props.options.selectableRowsOnClick && this.props.options.selectableRows !== 'none' && this.isRowSelectable(data.dataIndex)) {
       const selectRow = { index: data.rowIndex, dataIndex: data.dataIndex };
       this.handleRowSelect(selectRow);
     }


Test patch:
diff --git a/test/MUIDataTableBody.test.js b/test/MUIDataTableBody.test.js
index 2fb80ac..af6f3e2 100644
--- a/test/MUIDataTableBody.test.js
+++ b/test/MUIDataTableBody.test.js
@@ -235,6 +235,38 @@ describe('<TableBody />', function() {
     assert.strictEqual(toggleExpandRow.callCount, 0);
   });
 
+  it('should not gather selected row data when clicking row with selectableRowsOnClick=true when it is disabled with isRowSelectable.', () => {
+    let selectedRowData;
+    const options = { selectableRows: true, selectableRowsOnClick: true, isRowSelectable: dataIndex => dataIndex === 2 ? false : true };
+    const selectRowUpdate = (type, data) => (selectedRowData = data);
+    const toggleExpandRow = spy();
+
+    const mountWrapper = mount(
+      <TableBody
+        data={displayData}
+        count={displayData.length}
+        columns={columns}
+        page={0}
+        rowsPerPage={10}
+        selectedRows={[]}
+        selectRowUpdate={selectRowUpdate}
+        expandedRows={[]}
+        toggleExpandRow={toggleExpandRow}
+        options={options}
+        searchText={''}
+        filterList={[]}
+      />,
+    );
+
+    mountWrapper
+      .find('#MUIDataTableBodyRow-2')
+      .first()
+      .simulate('click');
+
+    assert.isUndefined(selectedRowData);
+    assert.strictEqual(toggleExpandRow.callCount, 0);
+  });
+
   it('should gather expanded row data when clicking row with expandableRows=true and expandableRowsOnClick=true.', () => {
     let expandedRowData;
     const options = { selectableRows: true, expandableRows: true, expandableRowsOnClick: true };