forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubMenu.tsx
347 lines (320 loc) · 11.5 KB
/
SubMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import PropTypes from '../../_util/vue-types';
import type { PropType, ExtractPropTypes } from 'vue';
import { computed, defineComponent, getCurrentInstance, ref, watch, onBeforeUnmount } from 'vue';
import useProvideKeyPath, { useInjectKeyPath, useMeasure } from './hooks/useKeyPath';
import {
useInjectMenu,
useProvideFirstLevel,
MenuContextProvider,
useProvideForceRender,
useInjectForceRender,
} from './hooks/useMenuContext';
import { getPropsSlot, isValidElement } from '../../_util/props-util';
import classNames from '../../_util/classNames';
import useDirectionStyle from './hooks/useDirectionStyle';
import PopupTrigger from './PopupTrigger';
import SubMenuList from './SubMenuList';
import InlineSubMenuList from './InlineSubMenuList';
import { cloneElement } from '../../_util/vnode';
import Overflow from '../../vc-overflow';
import devWarning from '../../vc-util/devWarning';
import isValid from '../../_util/isValid';
let indexGuid = 0;
export const subMenuProps = {
icon: PropTypes.any,
title: PropTypes.any,
disabled: Boolean,
level: Number,
popupClassName: String,
popupOffset: Array as unknown as PropType<[number, number]>,
internalPopupClose: Boolean,
eventKey: String,
expandIcon: Function as PropType<(p?: { isOpen: boolean; [key: string]: any }) => any>,
};
export type SubMenuProps = Partial<ExtractPropTypes<typeof subMenuProps>>;
export default defineComponent({
name: 'ASubMenu',
inheritAttrs: false,
props: subMenuProps,
slots: ['icon', 'title', 'expandIcon'],
emits: ['titleClick', 'mouseenter', 'mouseleave'],
setup(props, { slots, attrs, emit }) {
useProvideFirstLevel(false);
const isMeasure = useMeasure();
const instance = getCurrentInstance();
const vnodeKey =
typeof instance.vnode.key === 'symbol' ? String(instance.vnode.key) : instance.vnode.key;
devWarning(
typeof instance.vnode.key !== 'symbol',
'SubMenu',
`SubMenu \`:key="${String(vnodeKey)}"\` not support Symbol type`,
);
const key = isValid(vnodeKey) ? vnodeKey : `sub_menu_${++indexGuid}_$$_not_set_key`;
const eventKey =
props.eventKey ??
(isValid(vnodeKey) ? `sub_menu_${++indexGuid}_$$_${vnodeKey}` : (key as string));
const { parentEventKeys, parentInfo, parentKeys } = useInjectKeyPath();
const keysPath = computed(() => [...parentKeys.value, key]);
const childrenEventKeys = ref([]);
const menuInfo = {
eventKey,
key,
parentEventKeys,
childrenEventKeys,
parentKeys,
};
parentInfo.childrenEventKeys?.value.push(eventKey);
onBeforeUnmount(() => {
if (parentInfo.childrenEventKeys) {
parentInfo.childrenEventKeys.value = parentInfo.childrenEventKeys?.value.filter(
k => k != eventKey,
);
}
});
useProvideKeyPath(eventKey, key, menuInfo);
const {
prefixCls,
activeKeys,
disabled: contextDisabled,
changeActiveKeys,
mode,
inlineCollapsed,
antdMenuTheme,
openKeys,
overflowDisabled,
onOpenChange,
registerMenuInfo,
unRegisterMenuInfo,
selectedSubMenuKeys,
expandIcon: menuExpandIcon,
} = useInjectMenu();
const hasKey = vnodeKey !== undefined && vnodeKey !== null;
// If not set key, use forceRender = true for children
// 如果没有 key,强制 render 子元素
const forceRender = !isMeasure && (useInjectForceRender() || !hasKey);
useProvideForceRender(forceRender);
if ((isMeasure && hasKey) || (!isMeasure && !hasKey) || forceRender) {
registerMenuInfo(eventKey, menuInfo);
onBeforeUnmount(() => {
unRegisterMenuInfo(eventKey);
});
}
const subMenuPrefixCls = computed(() => `${prefixCls.value}-submenu`);
const mergedDisabled = computed(() => contextDisabled.value || props.disabled);
const elementRef = ref();
const popupRef = ref();
// // ================================ Icon ================================
// const mergedItemIcon = itemIcon || contextItemIcon;
// const mergedExpandIcon = expandIcon || contextExpandIcon;
// ================================ Open ================================
const originOpen = computed(() => openKeys.value.includes(key));
const open = computed(() => !overflowDisabled.value && originOpen.value);
// =============================== Select ===============================
const childrenSelected = computed(() => {
return selectedSubMenuKeys.value.includes(key);
});
const isActive = ref(false);
watch(
activeKeys,
() => {
isActive.value = !!activeKeys.value.find(val => val === key);
},
{ immediate: true },
);
// =============================== Events ===============================
// >>>> Title click
const onInternalTitleClick = (e: Event) => {
// Skip if disabled
if (mergedDisabled.value) {
return;
}
emit('titleClick', e, key);
// Trigger open by click when mode is `inline`
if (mode.value === 'inline') {
onOpenChange(key, !originOpen.value);
}
};
const onMouseEnter = (event: MouseEvent) => {
if (!mergedDisabled.value) {
changeActiveKeys(keysPath.value);
emit('mouseenter', event);
}
};
const onMouseLeave = (event: MouseEvent) => {
if (!mergedDisabled.value) {
changeActiveKeys([]);
emit('mouseleave', event);
}
};
// ========================== DirectionStyle ==========================
const directionStyle = useDirectionStyle(computed(() => keysPath.value.length));
// >>>>> Visible change
const onPopupVisibleChange = (newVisible: boolean) => {
if (mode.value !== 'inline') {
onOpenChange(key, newVisible);
}
};
/**
* Used for accessibility. Helper will focus element without key board.
* We should manually trigger an active
*/
const onInternalFocus = () => {
changeActiveKeys(keysPath.value);
};
// =============================== Render ===============================
const popupId = eventKey && `${eventKey}-popup`;
const popupClassName = computed(() =>
classNames(
prefixCls.value,
`${prefixCls.value}-${antdMenuTheme.value}`,
props.popupClassName,
),
);
const renderTitle = (title: any, icon: any) => {
if (!icon) {
return inlineCollapsed.value &&
!parentKeys.value.length &&
title &&
typeof title === 'string' ? (
<div class={`${prefixCls.value}-inline-collapsed-noicon`}>{title.charAt(0)}</div>
) : (
<span class={`${prefixCls.value}-title-content`}>{title}</span>
);
}
// inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span
// ref: https://github.com/ant-design/ant-design/pull/23456
const titleIsSpan = isValidElement(title) && title.type === 'span';
return (
<>
{cloneElement(
icon,
{
class: `${prefixCls.value}-item-icon`,
},
false,
)}
{titleIsSpan ? title : <span class={`${prefixCls.value}-title-content`}>{title}</span>}
</>
);
};
// Cache mode if it change to `inline` which do not have popup motion
const triggerModeRef = computed(() => {
return mode.value !== 'inline' && keysPath.value.length > 1 ? 'vertical' : mode.value;
});
const renderMode = computed(() => (mode.value === 'horizontal' ? 'vertical' : mode.value));
const subMenuTriggerModeRef = computed(() =>
triggerModeRef.value === 'horizontal' ? 'vertical' : triggerModeRef.value,
);
const baseTitleNode = () => {
const subMenuPrefixClsValue = subMenuPrefixCls.value;
const icon = getPropsSlot(slots, props, 'icon');
const expandIcon = props.expandIcon || slots.expandIcon || menuExpandIcon.value;
const title = renderTitle(getPropsSlot(slots, props, 'title'), icon);
return (
<div
style={directionStyle.value}
class={`${subMenuPrefixClsValue}-title`}
tabindex={mergedDisabled.value ? null : -1}
ref={elementRef}
title={typeof title === 'string' ? title : null}
data-menu-id={key}
aria-expanded={open.value}
aria-haspopup
aria-controls={popupId}
aria-disabled={mergedDisabled.value}
onClick={onInternalTitleClick}
onFocus={onInternalFocus}
>
{title}
{/* Only non-horizontal mode shows the icon */}
{mode.value !== 'horizontal' && expandIcon ? (
expandIcon({ ...props, isOpen: open.value })
) : (
<i class={`${subMenuPrefixClsValue}-arrow`} />
)}
</div>
);
};
return () => {
if (isMeasure) {
if (!hasKey) {
return null;
}
return slots.default?.();
}
const subMenuPrefixClsValue = subMenuPrefixCls.value;
let titleNode = () => null;
if (!overflowDisabled.value && mode.value !== 'inline') {
titleNode = () => (
<PopupTrigger
mode={triggerModeRef.value}
prefixCls={subMenuPrefixClsValue}
visible={!props.internalPopupClose && open.value}
popupClassName={popupClassName.value}
popupOffset={props.popupOffset}
disabled={mergedDisabled.value}
onVisibleChange={onPopupVisibleChange}
v-slots={{
popup: () => (
<MenuContextProvider mode={subMenuTriggerModeRef.value} isRootMenu={false}>
<SubMenuList
id={popupId}
ref={popupRef}
v-slots={{ default: slots.default }}
></SubMenuList>
</MenuContextProvider>
),
}}
>
{baseTitleNode()}
</PopupTrigger>
);
} else {
// 包裹一层,保持结构一致,防止动画丢失
// https://github.com/vueComponent/ant-design-vue/issues/4325
titleNode = () => <PopupTrigger v-slots={{ default: baseTitleNode }}></PopupTrigger>;
}
return (
<MenuContextProvider mode={renderMode.value}>
<Overflow.Item
component="li"
{...attrs}
role="none"
class={classNames(
subMenuPrefixClsValue,
`${subMenuPrefixClsValue}-${mode.value}`,
attrs.class,
{
[`${subMenuPrefixClsValue}-open`]: open.value,
[`${subMenuPrefixClsValue}-active`]: isActive.value,
[`${subMenuPrefixClsValue}-selected`]: childrenSelected.value,
[`${subMenuPrefixClsValue}-disabled`]: mergedDisabled.value,
},
)}
onMouseenter={onMouseEnter}
onMouseleave={onMouseLeave}
data-submenu-id={key}
v-slots={{
default: () => {
return (
<>
{titleNode()}
{/* Inline mode */}
{!overflowDisabled.value && (
<InlineSubMenuList
id={popupId}
open={open.value}
keyPath={keysPath.value}
v-slots={{ default: slots.default }}
></InlineSubMenuList>
)}
</>
);
},
}}
></Overflow.Item>
</MenuContextProvider>
);
};
},
});