@lowdefy/blocks-antd
Version:
Lowdefy Ant Design Blocks
231 lines (227 loc) • 8.44 kB
JavaScript
/*
Copyright 2020-2026 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import React, { useCallback, useContext } from 'react';
import { Layout, Menu } from 'antd';
import { type, get } from '@lowdefy/helpers';
const SiderContext = Layout._InternalSiderContext;
import { withBlockDefaults } from '@lowdefy/block-utils';
import withTheme from '../withTheme.js';
import useItemShortcuts from '../useItemShortcuts.js';
const getDefaultMenu = (menus, menuId = 'default', links)=>{
if (type.isArray(links)) return links;
if (!type.isArray(menus)) return [];
const menu = menus.find((item)=>item.menuId === menuId) ?? menus[0] ?? {};
return menu.links ?? [];
};
const getTitle = ({ id, properties, pageId, url })=>properties?.title ?? pageId ?? url ?? id;
function collectLinkShortcuts(links) {
const result = [];
(links ?? []).forEach((link)=>{
if (link.type === 'MenuLink' || !link.type) {
if (link.properties?.shortcut) {
result.push({
key: link.pageId ?? link.id,
shortcut: link.properties.shortcut
});
}
}
if (link.links) {
result.push(...collectLinkShortcuts(link.links));
}
});
return result;
}
function buildMenuItems({ links, events, components: { Icon, Link, ShortcutBadge }, classNames, styles, isTopLevel = true }) {
return (links ?? []).map((link, i)=>{
if (link.type === 'MenuDivider') {
return {
type: 'divider',
key: link.id ?? i,
dashed: link.properties?.dashed,
style: link.style
};
}
if (link.type === 'MenuGroup') {
const groupItem = {
key: link.pageId ?? link.id,
label: /*#__PURE__*/ React.createElement(Link, {
id: link.pageId ?? link.id ?? i,
style: link.style,
...link
}, getTitle(link)),
children: buildMenuItems({
links: link.links,
events,
components: {
Icon,
Link,
ShortcutBadge
},
classNames,
styles,
isTopLevel: false
})
};
if (isTopLevel) {
// Top-level MenuGroup → collapsible submenu (with icon)
groupItem.icon = link.properties?.icon ? /*#__PURE__*/ React.createElement(Icon, {
blockId: `${link.id}_icon`,
classNames: {
element: classNames.icon
},
events: events,
properties: link.properties.icon,
styles: {
element: styles.icon
}
}) : undefined;
} else {
// Nested MenuGroup → non-collapsible group header
groupItem.type = 'group';
}
return groupItem;
}
// MenuLink (default)
return {
key: link.pageId ?? link.id,
danger: link.properties?.danger,
className: classNames.item,
icon: link.properties?.icon ? /*#__PURE__*/ React.createElement(Icon, {
blockId: `${link.id}_icon`,
classNames: {
element: classNames.icon
},
events: events,
properties: link.properties.icon,
styles: {
element: styles.icon
}
}) : undefined,
label: /*#__PURE__*/ React.createElement(Link, {
id: link.pageId ?? link.id ?? i,
style: link.style,
url: link.url ?? link.properties?.url,
newTab: link.newTab ?? link.properties?.newTab,
...link
}, getTitle(link), /*#__PURE__*/ React.createElement(ShortcutBadge, {
shortcut: link.properties?.shortcut
}))
};
});
}
function MenuComp({ blockId, classNames = {}, components: { Icon, Link, ShortcutBadge }, events, menus, methods, pageId, properties, rename, styles = {} }) {
const horizontalStyles = {
lineHeight: '64px',
width: '100%',
display: properties.mode === 'horizontal' ? 'inline-block' : undefined
};
const exProps = {};
if (properties.mode === 'inline') {
exProps.collapsed = properties.collapsed;
exProps.inlineIndent = properties.inlineIndent;
}
const menu = getDefaultMenu(menus, properties.menuId, properties.links);
const theme = properties.theme;
const { siderCollapsed } = useContext(SiderContext) ?? {};
const isCollapsed = properties.collapsed === true || siderCollapsed === true;
const items = buildMenuItems({
links: menu,
events,
components: {
Icon,
Link,
ShortcutBadge
},
classNames,
styles
});
const shortcutItems = collectLinkShortcuts(menu);
const onShortcutMatch = useCallback((key)=>{
methods.triggerEvent({
name: get(rename, 'events.onSelect', {
default: 'onSelect'
}),
event: {
key
}
});
}, [
methods,
rename
]);
useItemShortcuts({
items: shortcutItems,
onMatch: onShortcutMatch
});
return /*#__PURE__*/ React.createElement(Menu, {
id: blockId,
className: classNames.element,
style: {
...horizontalStyles,
...styles.element
},
items: items,
expandIcon: properties.expandIcon && /*#__PURE__*/ React.createElement(Icon, {
blockId: `${blockId}_expandIcon`,
classNames: {
element: classNames.expandIcon
},
events: events,
properties: properties.expandIcon,
styles: {
element: styles.expandIcon
}
}),
forceSubMenuRender: properties.forceSubMenuRender,
mode: properties.mode,
selectable: true,
theme: theme,
defaultOpenKeys: properties.defaultOpenKeys ?? (properties.mode === 'inline' && !isCollapsed && [
(menu.find((link)=>(link.links || []).map((subLink)=>subLink.links ? subLink.links.map((subSubLink)=>subSubLink.pageId) : [
subLink.pageId
]).flat().some((link)=>(properties.selectedKeys ?? [
pageId
]).indexOf(link) !== -1)) ?? {}).id
]) ?? [],
selectedKeys: properties.selectedKeys ?? [
pageId
],
subMenuCloseDelay: properties.subMenuCloseDelay,
subMenuOpenDelay: properties.subMenuOpenDelay,
onSelect: (item)=>methods.triggerEvent({
name: get(rename, 'events.onSelect', {
default: 'onSelect'
}),
event: {
key: item.key
}
}),
onClick: (item)=>methods.triggerEvent({
name: get(rename, 'events.onClick', {
default: 'onClick'
}),
event: {
key: item.key
}
}),
onOpenChange: (openKeys)=>methods.triggerEvent({
name: get(rename, 'events.onToggleMenuGroup', {
default: 'onToggleMenuGroup'
}),
event: {
openKeys
}
}),
...exProps
});
}
export default withTheme('Menu', withBlockDefaults(MenuComp));