Skip to content

Commit cf18305

Browse files
authored
chore: optimize logic of default motion; (#328)
1 parent a530b15 commit cf18305

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

src/Menu.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export interface MenuProps
4848
overflowedIndicator?: React.ReactNode;
4949
/** Menu motion define */
5050
motion?: MotionType;
51-
/** Default menu motion */
52-
defaultMotion?: MotionType;
51+
52+
/** Default menu motion of each mode */
53+
defaultMotions?: Partial<{ [key in MenuMode | 'other']: MotionType }>;
5354

5455
/** @deprecated Please use `motion` instead */
5556
openTransitionName?: string;
@@ -395,7 +396,7 @@ class Menu extends React.Component<MenuProps, MenuState> {
395396
...omit(this.props, [
396397
'collapsedWidth',
397398
'siderCollapsed',
398-
'defaultMotion',
399+
'defaultMotions',
399400
]),
400401
};
401402
const mode = this.getRealMenuMode();
@@ -413,7 +414,7 @@ class Menu extends React.Component<MenuProps, MenuState> {
413414
onMouseEnter: this.onMouseEnter,
414415
onTransitionEnd: this.onTransitionEnd,
415416
parentMenu: this,
416-
motion: getMotion(this.props, this.state),
417+
motion: getMotion(this.props, this.state, mode),
417418
};
418419

419420
delete props.openAnimation;

src/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import Menu from './Menu';
1+
import Menu, { MenuProps } from './Menu';
22
import SubMenu from './SubMenu';
33
import MenuItem from './MenuItem';
4-
import MenuItemGroup from './MenuItemGroup';
4+
import MenuItemGroup, { MenuItemGroupProps } from './MenuItemGroup';
55
import Divider from './Divider';
66

77
export {
@@ -11,6 +11,8 @@ export {
1111
MenuItemGroup,
1212
MenuItemGroup as ItemGroup,
1313
Divider,
14+
MenuProps,
15+
MenuItemGroupProps,
1416
};
1517

1618
export default Menu;

src/utils/legacyUtil.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import warning from 'rc-util/lib/warning';
2-
import { MotionType, TransitionNameType, OpenAnimation } from '../interface';
2+
import {
3+
MotionType,
4+
TransitionNameType,
5+
OpenAnimation,
6+
MenuMode,
7+
} from '../interface';
38

49
interface GetMotionProps {
510
motion?: MotionType;
6-
defaultMotion?: MotionType;
11+
defaultMotions?: Partial<{ [key in MenuMode | 'other']: MotionType }>;
712
openAnimation?: OpenAnimation;
813
openTransitionName?: TransitionNameType;
914
prefixCls?: string;
@@ -17,11 +22,12 @@ export function getMotion(
1722
{
1823
prefixCls,
1924
motion,
20-
defaultMotion,
25+
defaultMotions = {},
2126
openAnimation,
2227
openTransitionName,
2328
}: GetMotionProps,
2429
{ switchingModeFromInline }: GetMotionState,
30+
menuMode: MenuMode,
2531
): MotionType {
2632
if (motion) {
2733
return motion;
@@ -43,8 +49,14 @@ export function getMotion(
4349
motionName: openTransitionName,
4450
};
4551
}
52+
// Default logic
53+
const defaultMotion = defaultMotions[menuMode];
54+
55+
if (defaultMotion) {
56+
return defaultMotion;
57+
}
4658

4759
// When mode switch from inline
4860
// submenu should hide without animation
49-
return switchingModeFromInline ? null : defaultMotion;
61+
return switchingModeFromInline ? null : defaultMotions.other;
5062
}

tests/Menu.spec.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,25 @@ describe('Menu', () => {
643643
});
644644

645645
describe('motion', () => {
646-
it('defaultMotion should work correctly', () => {
646+
it('defaultMotions should work correctly', () => {
647+
const defaultMotions = {
648+
inline: { motionName: 'inlineMotion' },
649+
horizontal: { motionName: 'horizontalMotion' },
650+
other: { motionName: 'defaultMotion' },
651+
};
647652
const wrapper = mount(
648-
<Menu mode="inline" defaultMotion={{ motionName: 'defaultMotion1' }} />,
653+
<Menu mode="inline" defaultMotions={defaultMotions} />,
649654
);
650-
expect(getMotion(wrapper.props(), wrapper.state())).toEqual({
651-
motionName: 'defaultMotion1',
655+
expect(getMotion(wrapper.props(), wrapper.state(), 'inline')).toEqual({
656+
motionName: 'inlineMotion',
657+
});
658+
expect(getMotion(wrapper.props(), wrapper.state(), 'horizontal')).toEqual(
659+
{
660+
motionName: 'horizontalMotion',
661+
},
662+
);
663+
expect(getMotion(wrapper.props(), wrapper.state(), 'vertical')).toEqual({
664+
motionName: 'defaultMotion',
652665
});
653666
});
654667

0 commit comments

Comments
 (0)