Skip to content

Commit

Permalink
Merge pull request mui#1325 from hiddentao/v0.11-row-selection
Browse files Browse the repository at this point in the history
v0.11 - [Table] If `selected` key for some rows gets changed ensure table internal selection array gets updated
  • Loading branch information
Hai Nguyen committed Aug 13, 2015
2 parents 81f2d98 + 19107c9 commit 534b23d
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/table/table-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,23 @@ let TableBody = React.createClass({
},

getInitialState() {
// Determine what rows are 'pre-selected'.
let preSelectedRows = [];
if (this.props.selectable && this.props.preScanRows) {
let index = 0;
React.Children.forEach(this.props.children, (child) => {
if (React.isValidElement(child)) {
if (child.props.selected && (preSelectedRows.length === 0 || this.props.multiSelectable)) {
preSelectedRows.push(index);
}

index++;
}
});
}

return {
selectedRows: preSelectedRows,
selectedRows: this._calculatePreselectedRows(this.props),
};
},

componentWillReceiveProps(nextProps) {
let newState = {};

if (this.props.allRowsSelected && !nextProps.allRowsSelected) {
let lastSelectedRow = (this.state.selectedRows.length) ?
this.state.selectedRows[this.state.selectedRows.length - 1] : undefined;
let lastSelectedRow = this.state.selectedRows.length ? this.state.selectedRows[this.state.selectedRows.length - 1] : undefined;

this.setState({ selectedRows: [lastSelectedRow] });
newState.selectedRows = [lastSelectedRow];
} else {
newState.selectedRows = this._calculatePreselectedRows(nextProps);
}

this.setState(newState);
},

componentClickAway() {
Expand Down Expand Up @@ -151,6 +141,26 @@ let TableBody = React.createClass({
);
},

_calculatePreselectedRows (props) {
// Determine what rows are 'pre-selected'.
let preSelectedRows = [];

if (props.selectable && props.preScanRows) {
let index = 0;
React.Children.forEach(props.children, (child) => {
if (React.isValidElement(child)) {
if (child.props.selected && (preSelectedRows.length === 0 || props.multiSelectable)) {
preSelectedRows.push(index);
}

index++;
}
});
}

return preSelectedRows;
},

_isRowSelected(rowNumber) {
if (this.props.allRowsSelected) {
return true;
Expand Down

0 comments on commit 534b23d

Please sign in to comment.