Skip to content

Commit

Permalink
fix: Menu defaultOpenKeys not work as expect (#15970)
Browse files Browse the repository at this point in the history
* Remove animation on first render

* update test cas
  • Loading branch information
zombieJ committed Apr 9, 2019
1 parent 6bd7614 commit bef491e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions components/_util/raf.ts
Expand Up @@ -34,3 +34,5 @@ wrapperRaf.cancel = function(pid?: number) {
raf.cancel(ids[pid]);
delete ids[pid];
};

wrapperRaf.ids = ids; // export this for test usage
5 changes: 5 additions & 0 deletions components/menu/__tests__/index.test.js
Expand Up @@ -3,6 +3,7 @@ import { mount } from 'enzyme';
import Menu from '..';
import Icon from '../../icon';
import Layout from '../../layout';
import raf from '../../_util/raf';

jest.mock('mutationobserver-shim', () => {
global.MutationObserver = function MutationObserver() {
Expand Down Expand Up @@ -73,6 +74,10 @@ describe('Menu', () => {
.at(0)
.hasClass('ant-menu-hidden'),
).not.toBe(true);

const rafCount = Object.keys(raf.ids).length;
wrapper.unmount();
expect(Object.keys(raf.ids).length).toBe(rafCount - 1);
});

it('should accept defaultOpenKeys in mode vertical', () => {
Expand Down
25 changes: 23 additions & 2 deletions components/menu/index.tsx
Expand Up @@ -10,6 +10,7 @@ import animation from '../_util/openAnimation';
import warning from '../_util/warning';
import { polyfill } from 'react-lifecycles-compat';
import { SiderContext, SiderContextProps } from '../layout/Sider';
import raf from '../_util/raf';

export interface SelectParam {
key: string;
Expand Down Expand Up @@ -69,6 +70,7 @@ export interface MenuState {
switchingModeFromInline: boolean;
inlineOpenKeys: string[];
prevProps: InternalMenuProps;
mounted: boolean;
}

export interface MenuContextProps {
Expand Down Expand Up @@ -122,6 +124,8 @@ class InternalMenu extends React.Component<InternalMenuProps, MenuState> {
return newState;
}

private mountRafId: number;

constructor(props: InternalMenuProps) {
super(props);

Expand Down Expand Up @@ -150,9 +154,25 @@ class InternalMenu extends React.Component<InternalMenuProps, MenuState> {
switchingModeFromInline: false,
inlineOpenKeys: [],
prevProps: props,
mounted: false,
};
}

// [Legacy] Origin code can render full defaultOpenKeys is caused by `rc-animate` bug.
// We have to workaround this to prevent animation on first render.
// https://github.com/ant-design/ant-design/issues/15966
componentDidMount() {
this.mountRafId = raf(() => {
this.setState({
mounted: true,
});
}, 10);
}

componentWillUnmount() {
raf.cancel(this.mountRafId);
}

restoreModeVerticalFromInline() {
const { switchingModeFromInline } = this.state;
if (switchingModeFromInline) {
Expand Down Expand Up @@ -262,6 +282,7 @@ class InternalMenu extends React.Component<InternalMenuProps, MenuState> {
}

renderMenu = ({ getPopupContainer, getPrefixCls }: ConfigConsumerProps) => {
const { mounted } = this.state;
const { prefixCls: customizePrefixCls, className, theme, collapsedWidth } = this.props;
const passProps = omit(this.props, ['collapsedWidth', 'siderCollapsed']);
const menuMode = this.getRealMenuMode();
Expand All @@ -282,9 +303,9 @@ class InternalMenu extends React.Component<InternalMenuProps, MenuState> {
if (menuMode !== 'inline') {
// closing vertical popup submenu after click it
menuProps.onClick = this.handleClick;
menuProps.openTransitionName = menuOpenAnimation;
menuProps.openTransitionName = mounted && menuOpenAnimation;
} else {
menuProps.openAnimation = menuOpenAnimation;
menuProps.openAnimation = mounted && menuOpenAnimation;
}

// https://github.com/ant-design/ant-design/issues/8587
Expand Down

0 comments on commit bef491e

Please sign in to comment.