Skip to content

Commit

Permalink
Merge f2681f8 into a5ab0da
Browse files Browse the repository at this point in the history
  • Loading branch information
endrjuskr committed Aug 10, 2019
2 parents a5ab0da + f2681f8 commit abb9889
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ The component accepts the following props:
|**`selectableRows`**|string|'multiple'|Numbers of rows that can be selected. Options are "multiple", "single", "none".
|**`selectableRowsOnClick`**|boolean|false|Enable/disable select toggle when row is clicked. When False, only checkbox will trigger this action.
|**`isRowSelectable`**|function||Enable/disable selection on certain rows with custom function. Returns true if not provided. `function(dataIndex) => bool`
|**`maxSelectedRows`**|number|Infinity|Maximum number of rows to select.
|**`expandableRows`**|boolean|false|Enable/disable expandable rows
|**`expandableRowsOnClick`**|boolean|false|Enable/disable expand trigger when row is clicked. When False, only expand icon will trigger this action.
|**`renderExpandableRow`**|function||Render expandable row. `function(rowData, rowMeta) => React Component`
Expand Down
1 change: 1 addition & 0 deletions examples/selectable-rows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Example extends React.Component {
selectableRowsOnClick: true,
filterType: 'dropdown',
responsive: 'stacked',
maxSelectedRows: 5,
rowsPerPage: 10,
rowsSelected: this.state.rowsSelected,
onRowsSelect: (rowsSelected, allRows) => {
Expand Down
2 changes: 2 additions & 0 deletions src/MUIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class MUIDataTable extends React.Component {
resizableColumns: PropTypes.bool,
selectableRows: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['none', 'single', 'multiple'])]),
selectableRowsOnClick: PropTypes.bool,
maxSelectedRows: PropTypes.number,
isRowSelectable: PropTypes.func,
serverSide: PropTypes.bool,
onTableChange: PropTypes.func,
Expand Down Expand Up @@ -263,6 +264,7 @@ class MUIDataTable extends React.Component {
resizableColumns: false,
selectableRows: 'multiple',
selectableRowsOnClick: false,
maxSelectedRows: Infinity,
caseSensitive: false,
serverSide: false,
rowHover: true,
Expand Down
11 changes: 7 additions & 4 deletions src/components/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ class TableBody extends React.Component {
}

isRowSelectable(dataIndex) {
const { options } = this.props;
if (options.isRowSelectable) {
return options.isRowSelectable(dataIndex);
const { options, selectedRows } = this.props;
const lookup = (selectedRows && selectedRows.lookup) || {};
if (lookup[dataIndex]) {
return (!options.isRowSelectable || options.isRowSelectable(dataIndex));
} else {
const numSelected = (selectedRows && selectedRows.data && selectedRows.data.length) || 0;
return (options.maxSelectedRows || Infinity) > numSelected && (!options.isRowSelectable || options.isRowSelectable(dataIndex));
}
return true;
}

handleRowSelect = data => {
Expand Down
28 changes: 28 additions & 0 deletions test/MUIDataTableBody.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,32 @@ describe('<TableBody />', function() {

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

it('should not call onRowClick when maxSelectedRows rows is reached', () => {
const options = { selectableRows: true, maxSelectedRows: 1 };
const selectedIndex = 0;
const selectedRows = { data: [selectedIndex], lookup: {[selectedIndex]: true }};

const mountWrapper = mount(
<TableBody
data={displayData}
count={displayData.length}
columns={columns}
page={0}
rowsPerPage={10}
selectedRows={selectedRows}
expandedRows={[]}
options={options}
searchText={''}
filterList={[]}
/>,
);

const tableSelectCellsProps = mountWrapper.find('TableSelectCell').map(t => t.props());

tableSelectCellsProps.forEach((props, i) => {
assert.equal(props.checked, i === selectedIndex);
assert.equal(props.isRowSelectable, i === selectedIndex);
});
});
});
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const webpack = require('webpack');

module.exports = {
entry: {
app: "./examples/customize-filter/index.js"
app: "./examples/selectable-rows/index.js"
},
stats: "verbose",
context: __dirname,
Expand Down

0 comments on commit abb9889

Please sign in to comment.