Browse Source

Format menu

pull/13602/head
ztplz 6 years ago
committed by 偏右
parent
commit
05cf557ee1
  1. 3
      components/menu/MenuItem.tsx
  2. 3
      components/menu/SubMenu.tsx
  3. 16
      components/menu/index.tsx

3
components/menu/MenuItem.tsx

@ -21,12 +21,15 @@ class MenuItem extends React.Component<MenuItemProps, any> {
static isMenuItem = 1;
context: any;
private menuItem: any;
onKeyDown = (e: React.MouseEvent<HTMLElement>) => {
this.menuItem.onKeyDown(e);
};
saveMenuItem = (menuItem: any) => {
this.menuItem = menuItem;
};
render() {
const { inlineCollapsed } = this.context;
const { level, children, rootPrefixCls } = this.props;

3
components/menu/SubMenu.tsx

@ -11,12 +11,15 @@ class SubMenu extends React.Component<any, any> {
static isSubMenu = 1;
context: any;
private subMenu: any;
onKeyDown = (e: React.MouseEvent<HTMLElement>) => {
this.subMenu.onKeyDown(e);
};
saveSubMenu = (subMenu: any) => {
this.subMenu = subMenu;
};
render() {
const { rootPrefixCls, className } = this.props;
const theme = this.context.antdMenuTheme;

16
components/menu/index.tsx

@ -78,9 +78,11 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
siderCollapsed: PropTypes.bool,
collapsedWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
};
context: any;
switchingModeFromInline: boolean;
inlineOpenKeys: string[] = [];
constructor(props: MenuProps) {
super(props);
@ -106,20 +108,24 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
openKeys: openKeys || [],
};
}
getChildContext() {
return {
inlineCollapsed: this.getInlineCollapsed(),
antdMenuTheme: this.props.theme,
};
}
componentWillReceiveProps(nextProps: MenuProps, nextContext: SiderContext) {
if (this.props.mode === 'inline' && nextProps.mode !== 'inline') {
this.switchingModeFromInline = true;
}
if ('openKeys' in nextProps) {
this.setState({ openKeys: nextProps.openKeys! });
return;
}
if (
(nextProps.inlineCollapsed && !this.props.inlineCollapsed) ||
(nextContext.siderCollapsed && !this.context.siderCollapsed)
@ -128,6 +134,7 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
this.inlineOpenKeys = this.state.openKeys;
this.setState({ openKeys: [] });
}
if (
(!nextProps.inlineCollapsed && this.props.inlineCollapsed) ||
(!nextContext.siderCollapsed && this.context.siderCollapsed)
@ -136,12 +143,14 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
this.inlineOpenKeys = [];
}
}
restoreModeVerticalFromInline() {
if (this.switchingModeFromInline) {
this.switchingModeFromInline = false;
this.setState({});
}
}
// Restore vertical mode when menu is collapsed responsively when mounted
// https://github.com/ant-design/ant-design/issues/13104
// TODO: not a perfect solution, looking a new way to avoid setting switchingModeFromInline in this situation
@ -152,6 +161,7 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
onMouseEnter(e);
}
};
handleTransitionEnd = (e: TransitionEvent) => {
// when inlineCollapsed menu width animation finished
// https://github.com/ant-design/ant-design/issues/12864
@ -164,6 +174,7 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
this.restoreModeVerticalFromInline();
}
};
handleClick = (e: ClickParam) => {
this.handleOpenChange([]);
@ -172,6 +183,7 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
onClick(e);
}
};
handleOpenChange = (openKeys: string[]) => {
this.setOpenKeys(openKeys);
@ -180,11 +192,13 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
onOpenChange(openKeys);
}
};
setOpenKeys(openKeys: string[]) {
if (!('openKeys' in this.props)) {
this.setState({ openKeys });
}
}
getRealMenuMode() {
const inlineCollapsed = this.getInlineCollapsed();
if (this.switchingModeFromInline && inlineCollapsed) {
@ -193,6 +207,7 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
const { mode } = this.props;
return inlineCollapsed ? 'vertical' : mode;
}
getInlineCollapsed() {
const { inlineCollapsed } = this.props;
if (this.context.siderCollapsed !== undefined) {
@ -200,6 +215,7 @@ export default class Menu extends React.Component<MenuProps, MenuState> {
}
return inlineCollapsed;
}
getMenuOpenAnimation(menuMode: MenuMode) {
const { openAnimation, openTransitionName } = this.props;
let menuOpenAnimation = openAnimation || openTransitionName;

Loading…
Cancel
Save