-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TableBody] Fix row selection re-render bug
Non-selection related table re-rendering previously caused the user's selection to be lost because the state was being reset unnecessarily. I took the opportunity to remove the prescient `TODO` comment that did correctly predict that this might be a problem.
- Loading branch information
dchamb
committed
Jan 6, 2017
1 parent
78a28b2
commit 8bdc527
Showing
3 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
import { | ||
Table, | ||
TableBody, | ||
TableHeader, | ||
TableHeaderColumn, | ||
TableRow, | ||
TableRowColumn, | ||
} from 'src/Table'; | ||
|
||
const tableData = [ | ||
{name: 'John Smith'}, | ||
{name: 'Randal White'}, | ||
{name: 'Olivier'}, | ||
]; | ||
|
||
class SelectionObservingTable extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
selectionChangeCount: 0, | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
Selection Change Count: {this.state.selectionChangeCount} | ||
<Table | ||
selectable={true} | ||
onRowSelection={() => this.setState({selectionChangeCount: this.state.selectionChangeCount + 1})} | ||
> | ||
<TableHeader> | ||
<TableRow> | ||
<TableHeaderColumn> | ||
Name | ||
</TableHeaderColumn> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody displayRowCheckbox={true}> | ||
{tableData.map( (row, index) => ( | ||
<TableRow key={index}> | ||
<TableRowColumn> | ||
{row.name} | ||
</TableRowColumn> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default SelectionObservingTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* eslint-env mocha */ | ||
import React from 'react'; | ||
import {mount} from 'enzyme'; | ||
import {assert} from 'chai'; | ||
import getMuiTheme from 'src/styles/getMuiTheme'; | ||
import SelectionObservingTable from './SelectionObservingTable'; | ||
|
||
describe('<SelectionObservingTable />', () => { | ||
it('preserves selected change', () => { | ||
const muiTheme = getMuiTheme(); | ||
const mountWithContext = (node) => mount(node, { | ||
context: {muiTheme}, | ||
childContextTypes: {muiTheme: React.PropTypes.object}, | ||
}); | ||
|
||
const wrapper = mountWithContext( | ||
<SelectionObservingTable /> | ||
); | ||
|
||
assert.deepEqual( | ||
wrapper.find('Checkbox').map((checkbox) => checkbox.props().checked), | ||
[ | ||
false, | ||
false, | ||
false, | ||
false, | ||
], | ||
'should use the selected property for the initial value' | ||
); | ||
|
||
let input; | ||
input = wrapper.find('Checkbox').at(1).find('input'); | ||
input.node.checked = !input.node.checked; | ||
input.simulate('change'); | ||
|
||
assert.deepEqual( | ||
wrapper.find('Checkbox').map((checkbox) => checkbox.props().checked), | ||
[ | ||
false, | ||
true, | ||
false, | ||
false, | ||
], | ||
'should preserve initial selection' | ||
); | ||
|
||
input = wrapper.find('Checkbox').at(2).find('input'); | ||
input.node.checked = !input.node.checked; | ||
input.simulate('change'); | ||
|
||
assert.deepEqual( | ||
wrapper.find('Checkbox').map((checkbox) => checkbox.props().checked), | ||
[ | ||
false, | ||
false, | ||
true, | ||
false, | ||
], | ||
'should preserve updated selection' | ||
); | ||
}); | ||
}); |