Skip to content

Commit 2234028

Browse files
committed
Active previous item when children changes
1 parent 84d53c3 commit 2234028

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/MenuMixin.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ const MenuMixin = {
8989
},
9090

9191
componentWillReceiveProps(nextProps) {
92-
let activeKey;
93-
const originalActiveKey = this.getStore().getState().activeKey[this.getEventKey()];
94-
activeKey = getActiveKey(nextProps, originalActiveKey);
95-
// fix: this.setState(), parent.render(),
92+
const originalActiveKey = 'activeKey' in nextProps ? nextProps.activeKey :
93+
this.getStore().getState().activeKey[this.getEventKey()];
94+
const activeKey = getActiveKey(nextProps, originalActiveKey);
9695
if (activeKey !== originalActiveKey) {
9796
updateActiveKey(this.getStore(), this.getEventKey(), activeKey);
9897
}

tests/Menu.spec.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-undef */
1+
/* eslint-disable no-undef, react/no-multi-comp */
22
import React from 'react';
33
import { render, mount } from 'enzyme';
44
import { renderToJson } from 'enzyme-to-json';
@@ -215,4 +215,30 @@ describe('Menu', () => {
215215
wrapper.find('Menu').simulate('keyDown', { keyCode: KeyCode.DOWN });
216216
expect(wrapper.find('MenuItem').at(1).props().active).toBe(true);
217217
});
218+
219+
it('active first item when children changes', () => {
220+
class App extends React.Component {
221+
state = {
222+
items: ['foo'],
223+
}
224+
225+
render() {
226+
return (
227+
<Menu defaultActiveFirst activeKey="" selectedKeys={['foo']}>
228+
{this.state.items.map(item =>
229+
<MenuItem key={item}>{item}</MenuItem>
230+
)}
231+
</Menu>
232+
);
233+
}
234+
}
235+
236+
const wrapper = mount(<App />);
237+
238+
wrapper.setState({ items: ['bar', 'foo'] });
239+
240+
expect(
241+
wrapper.find('li').first().hasClass('rc-menu-item-active')
242+
).toBe(true);
243+
});
218244
});

0 commit comments

Comments
 (0)