Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tree data update error #41

Merged
merged 2 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' }]);
});
});