Skip to content

Commit

Permalink
Merge pull request #41 from react-component/fix-treeData-update-error
Browse files Browse the repository at this point in the history
Fix tree data update error
  • Loading branch information
warmhug committed Jan 11, 2017
2 parents e998769 + 9cab83b commit 6e07ec4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,20 @@ const Select = React.createClass({
componentWillReceiveProps(nextProps) {
// save parsed treeData, for performance (treeData may be very big)
this.renderedTreeData = this.renderTreeData(nextProps);
// Detecting whether the object of `onChange`'s argument is old ref.
// Better to do a deep equal later.
this._cacheTreeNodesStates = this._cacheTreeNodesStates !== 'no' &&
this._savedValue &&
nextProps.value === this._savedValue;
if (this.props.treeData !== nextProps.treeData ||
this.props.children !== nextProps.children) {
// refresh this._treeNodesStates cache
this._treeNodesStates = getTreeNodesStates(
this.renderedTreeData || nextProps.children,
this.state.value.map(item => item.value)
);
}
if ('value' in nextProps) {
if (this._cacheTreeNodesStates !== 'no' &&
this._savedValue && nextProps.value === this._savedValue) {
// Detecting whether the object of `onChange`'s argument is old ref.
// Better to do a deep equal later.
this._cacheTreeNodesStates = true;
} else {
this._cacheTreeNodesStates = false;
}
let value = toArray(nextProps.value);
value = this.addLabelToValue(nextProps, value);
value = this.getValue(nextProps, value);
Expand Down
11 changes: 11 additions & 0 deletions tests/Select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,15 @@ describe('TreeSelect', () => {
jest.runAllTimers();
expect(wrapper.state('open')).toBe(false);
});

// https://github.com/ant-design/ant-design/issues/4084
it('checks node correctly after treeData updated', () => {
const wrapper = mount(
<TreeSelect treeCheckable treeData={[]} />
);
wrapper.setProps({ treeData: [{ key: '0', value: '0', label: 'label0' }] });
const treeWrapper = mount(wrapper.find('Trigger').node.getComponent());
treeWrapper.find('.rc-tree-select-tree-checkbox').simulate('click');
expect(wrapper.state().value).toEqual([{ value: '0', label: 'label0' }]);
});
});

0 comments on commit 6e07ec4

Please sign in to comment.