Skip to content

Commit 3b14396

Browse files
committed
refactor: menu to ts
1 parent 873ecf0 commit 3b14396

File tree

9 files changed

+72
-51
lines changed

9 files changed

+72
-51
lines changed

components/layout/Sider.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ export const SiderProps = {
4646
// belowShow?: boolean;
4747
// }
4848

49-
// export interface SiderContext {
50-
// siderCollapsed: boolean;
51-
// }
49+
export interface SiderContextProps {
50+
sCollapsed?: boolean;
51+
collapsedWidth?: string | number;
52+
}
5253

5354
const generateId = (() => {
5455
let i = 0;

components/menu/MenuItem.jsx renamed to components/menu/MenuItem.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { defineComponent, inject } from 'vue';
22
import { Item, itemProps } from '../vc-menu';
33
import { getOptionProps, getSlot } from '../_util/props-util';
4-
import Tooltip from '../tooltip';
5-
function noop() {}
4+
import Tooltip, { TooltipProps } from '../tooltip';
5+
import { SiderContextProps } from '../layout/Sider';
6+
67
export default defineComponent({
78
name: 'MenuItem',
89
inheritAttrs: false,
910
props: itemProps,
1011
isMenuItem: true,
1112
setup() {
1213
return {
13-
getInlineCollapsed: inject('getInlineCollapsed', noop),
14-
layoutSiderContext: inject('layoutSiderContext', {}),
14+
getInlineCollapsed: inject<() => boolean>('getInlineCollapsed', () => false),
15+
layoutSiderContext: inject<SiderContextProps>('layoutSiderContext', {}),
1516
};
1617
},
1718
methods: {
18-
onKeyDown(e) {
19-
this.$refs.menuItem.onKeyDown(e);
19+
onKeyDown(e: HTMLElement) {
20+
(this.$refs.menuItem as any).onKeyDown(e);
2021
},
2122
},
2223
render() {
@@ -31,7 +32,7 @@ export default defineComponent({
3132
} else if (title === false) {
3233
tooltipTitle = '';
3334
}
34-
const tooltipProps = {
35+
const tooltipProps: TooltipProps = {
3536
title: tooltipTitle,
3637
};
3738
const siderCollapsed = this.layoutSiderContext.sCollapsed;
@@ -48,7 +49,7 @@ export default defineComponent({
4849
...attrs,
4950
ref: 'menuItem',
5051
};
51-
const toolTipProps = {
52+
const toolTipProps: TooltipProps = {
5253
...tooltipProps,
5354
placement: 'right',
5455
overlayClassName: `${rootPrefixCls}-inline-collapsed-tooltip`,

components/menu/SubMenu.jsx renamed to components/menu/SubMenu.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import { inject } from 'vue';
1+
import { defineComponent, inject } from 'vue';
22
import { SubMenu as VcSubMenu } from '../vc-menu';
33
import classNames from '../_util/classNames';
44
import Omit from 'omit.js';
55
import { getSlot } from '../_util/props-util';
66

7-
export default {
7+
export type MenuTheme = 'light' | 'dark';
8+
9+
export interface MenuContextProps {
10+
inlineCollapsed?: boolean;
11+
theme?: MenuTheme;
12+
}
13+
14+
export default defineComponent({
815
name: 'ASubMenu',
916
isSubMenu: true,
1017
inheritAttrs: false,
1118
props: { ...VcSubMenu.props },
1219
setup() {
1320
return {
14-
menuPropsContext: inject('menuPropsContext', {}),
21+
menuPropsContext: inject<MenuContextProps>('menuPropsContext', {}),
1522
};
1623
},
1724
methods: {
1825
onKeyDown(e) {
19-
this.$refs.subMenu.onKeyDown(e);
26+
(this.$refs.subMenu as any).onKeyDown(e);
2027
},
2128
},
2229

@@ -33,4 +40,4 @@ export default {
3340
};
3441
return <VcSubMenu {...props}>{getSlot(this)}</VcSubMenu>;
3542
},
36-
};
43+
});

components/menu/index.jsx renamed to components/menu/index.tsx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, inject, provide, toRef } from 'vue';
1+
import { defineComponent, inject, provide, toRef, App, ExtractPropTypes } from 'vue';
22
import omit from 'omit.js';
33
import VcMenu, { Divider, ItemGroup } from '../vc-menu';
44
import SubMenu from './SubMenu';
@@ -10,6 +10,8 @@ import { hasProp, getOptionProps, getSlot } from '../_util/props-util';
1010
import BaseMixin from '../_util/BaseMixin';
1111
import commonPropsType from '../vc-menu/commonPropsType';
1212
import { defaultConfigProvider } from '../config-provider';
13+
import { SiderContextProps } from '../layout/Sider';
14+
import { tuple } from '../_util/type';
1315
// import raf from '../_util/raf';
1416

1517
export const MenuMode = PropTypes.oneOf([
@@ -22,7 +24,7 @@ export const MenuMode = PropTypes.oneOf([
2224

2325
export const menuProps = {
2426
...commonPropsType,
25-
theme: PropTypes.oneOf(['light', 'dark']).def('light'),
27+
theme: PropTypes.oneOf(tuple('light', 'dark')).def('light'),
2628
mode: MenuMode.def('vertical'),
2729
selectable: PropTypes.looseBool,
2830
selectedKeys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
@@ -43,10 +45,10 @@ export const menuProps = {
4345
onClick: PropTypes.func,
4446
onMouseenter: PropTypes.func,
4547
onSelectChange: PropTypes.func,
46-
'onUpdate:selectedKeys': PropTypes.func,
47-
'onUpdate:openKeys': PropTypes.func,
4848
};
4949

50+
export type MenuProps = Partial<ExtractPropTypes<typeof menuProps>>;
51+
5052
const Menu = defineComponent({
5153
name: 'AMenu',
5254
inheritAttrs: false,
@@ -56,23 +58,33 @@ const Menu = defineComponent({
5658
SubMenu: { ...SubMenu, name: 'ASubMenu' },
5759
ItemGroup: { ...ItemGroup, name: 'AMenuItemGroup' },
5860
mixins: [BaseMixin],
61+
emits: [
62+
'update:selectedKeys',
63+
'update:openKeys',
64+
'mouseenter',
65+
'openChange',
66+
'click',
67+
'selectChange',
68+
'select',
69+
'deselect',
70+
],
5971
created() {
6072
provide('getInlineCollapsed', this.getInlineCollapsed);
6173
provide('menuPropsContext', this.$props);
6274
},
6375
setup() {
64-
const layoutSiderContext = inject('layoutSiderContext', {});
76+
const layoutSiderContext = inject<SiderContextProps>('layoutSiderContext', {});
6577
const layoutSiderCollapsed = toRef(layoutSiderContext, 'sCollapsed');
6678
return {
6779
configProvider: inject('configProvider', defaultConfigProvider),
6880
layoutSiderContext,
6981
layoutSiderCollapsed,
82+
propsUpdating: false,
83+
switchingModeFromInline: false,
84+
leaveAnimationExecutedWhenInlineCollapsed: false,
85+
inlineOpenKeys: [],
7086
};
7187
},
72-
// model: {
73-
// prop: 'selectedKeys',
74-
// event: 'selectChange',
75-
// },
7688
updated() {
7789
this.propsUpdating = false;
7890
},
@@ -96,15 +108,12 @@ const Menu = defineComponent({
96108
},
97109
},
98110
data() {
99-
const props = getOptionProps(this);
111+
const props: MenuProps = getOptionProps(this);
100112
warning(
101113
!('inlineCollapsed' in props && props.mode !== 'inline'),
102114
'Menu',
103115
"`inlineCollapsed` should only be used when Menu's `mode` is inline.",
104116
);
105-
this.switchingModeFromInline = false;
106-
this.leaveAnimationExecutedWhenInlineCollapsed = false;
107-
this.inlineOpenKeys = [];
108117
let sOpenKeys;
109118

110119
if ('openKeys' in props) {
@@ -244,7 +253,7 @@ const Menu = defineComponent({
244253
const menuOpenAnimation = this.getMenuOpenAnimation(menuMode);
245254
const { class: className, ...otherAttrs } = this.$attrs;
246255
const menuClassName = {
247-
[className]: className,
256+
[className as string]: className,
248257
[`${prefixCls}-${theme}`]: true,
249258
[`${prefixCls}-inline-collapsed`]: this.getInlineCollapsed(),
250259
};
@@ -297,7 +306,7 @@ const Menu = defineComponent({
297306
});
298307

299308
/* istanbul ignore next */
300-
Menu.install = function(app) {
309+
Menu.install = function(app: App) {
301310
app.component(Menu.name, Menu);
302311
app.component(Menu.Item.name, Menu.Item);
303312
app.component(Menu.SubMenu.name, Menu.SubMenu);
File renamed without changes.

components/tooltip/Tooltip.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, inject } from 'vue';
1+
import { defineComponent, ExtractPropTypes, inject } from 'vue';
22
import VcTooltip from '../vc-tooltip';
33
import classNames from '../_util/classNames';
44
import getPlacements from './placements';
@@ -27,13 +27,18 @@ const splitObject = (obj: any, keys: string[]) => {
2727
return { picked, omitted };
2828
};
2929
const props = abstractTooltipProps();
30+
31+
const tooltipProps = {
32+
...props,
33+
title: PropTypes.VNodeChild,
34+
};
35+
36+
export type TooltipProps = Partial<ExtractPropTypes<typeof tooltipProps>>;
37+
3038
export default defineComponent({
3139
name: 'ATooltip',
3240
inheritAttrs: false,
33-
props: {
34-
...props,
35-
title: PropTypes.VNodeChild,
36-
},
41+
props: tooltipProps,
3742
emits: ['update:visible', 'visibleChange'],
3843
setup() {
3944
return {
@@ -192,7 +197,7 @@ export default defineComponent({
192197
[openClassName || `${prefixCls}-open`]: sVisible,
193198
[child.props && child.props.class]: child.props && child.props.class,
194199
});
195-
const tooltipProps = {
200+
const vcTooltipProps = {
196201
...$attrs,
197202
...$props,
198203
prefixCls,
@@ -205,7 +210,7 @@ export default defineComponent({
205210
onPopupAlign: this.onPopupAlign,
206211
};
207212
return (
208-
<VcTooltip {...tooltipProps}>
213+
<VcTooltip {...vcTooltipProps}>
209214
{sVisible ? cloneElement(child, { class: childCls }) : child}
210215
</VcTooltip>
211216
);

components/tooltip/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { App } from 'vue';
22
import ToolTip from './Tooltip';
33

4+
export { TooltipProps } from './Tooltip';
5+
46
/* istanbul ignore next */
57
ToolTip.install = function(app: App) {
68
app.component(ToolTip.name, ToolTip);

components/vc-mentions/src/mentionsProps.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PropType } from 'vue';
22
import PropTypes from '../../_util/vue-types';
3-
import initDefaultProps from '../../_util/props-util/initDefaultProps';
3+
import { initDefaultProps } from '../../_util/props-util';
44
import {
55
filterOption as defaultFilterOption,
66
validateSearch as defaultValidateSearch,
@@ -20,12 +20,8 @@ export const mentionsProps = {
2020
placement: PropTypes.oneOf(PlaceMent),
2121
character: PropTypes.any,
2222
characterRender: PropTypes.func,
23-
filterOption: {
24-
type: [Boolean, Function] as PropType<false | typeof defaultFilterOption>,
25-
},
26-
validateSearch: {
27-
type: Function as PropType<typeof defaultValidateSearch>,
28-
},
23+
filterOption: PropTypes.func,
24+
validateSearch: PropTypes.func,
2925
getPopupContainer: {
3026
type: Function as PropType<() => HTMLElement>,
3127
},

components/vc-menu/Menu.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const Menu = {
4747
this.updateMiniStore();
4848
},
4949
methods: {
50-
onSelect(selectInfo) {
50+
handleSelect(selectInfo) {
5151
const props = this.$props;
5252
if (props.selectable) {
5353
// root menu
@@ -70,7 +70,7 @@ const Menu = {
7070
}
7171
},
7272

73-
onClick(e) {
73+
handleClick(e) {
7474
this.__emit('click', e);
7575
},
7676
// onKeyDown needs to be exposed as a instance method
@@ -112,7 +112,7 @@ const Menu = {
112112
}
113113
},
114114

115-
onDeselect(selectInfo) {
115+
handleDeselect(selectInfo) {
116116
const props = this.$props;
117117
if (props.selectable) {
118118
const selectedKeys = this.store.getState().selectedKeys.concat();
@@ -172,10 +172,10 @@ const Menu = {
172172
overflowedIndicator: getComponent(this, 'overflowedIndicator', props) || <span>···</span>,
173173
openTransitionName: this.getOpenTransitionName(),
174174
children: filterEmpty(props.children),
175-
onClick: this.onClick,
175+
onClick: this.handleClick,
176176
onOpenChange: this.onOpenChange,
177-
onDeselect: this.onDeselect,
178-
onSelect: this.onSelect,
177+
onDeselect: this.handleDeselect,
178+
onSelect: this.handleSelect,
179179
ref: this.saveInnerMenu,
180180
};
181181

0 commit comments

Comments
 (0)