Skip to content

feat: New Actions #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions components/actions/ActionMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { EllipsisOutlined } from '@ant-design/icons';
import { Dropdown, MenuProps } from 'antd';
import React from 'react';
import { ActionsProps } from '.';
import { useXProviderContext } from '../x-provider';
import { ActionItem, ItemType } from './interface';

export const findItem = (keyPath: string[], items: ActionItem[]): ActionItem | null => {
const keyToFind = keyPath[0]; // Get the first key from the keyPath

for (const item of items) {
if (item.key === keyToFind) {
// If the item is found and this is the last key in the path
if (keyPath.length === 1) return item;

// If it is a SubItemType, recurse to find in its children
if ('children' in item) {
return findItem(keyPath.slice(1), item.children!);
}
}
}

return null;
};

const ActionMenu = (props: { item: ItemType } & Pick<ActionsProps, 'prefixCls' | 'onClick'>) => {
const { onClick: onMenuClick, item } = props;
const { children = [], triggerSubMenuAction = 'hover' } = item;
const { getPrefixCls } = useXProviderContext();
const prefixCls = getPrefixCls('actions', props.prefixCls);
const icon = item?.icon ?? <EllipsisOutlined />;

const menuProps: MenuProps = {
items: children,
onClick: ({ key, keyPath, domEvent }) => {
if (findItem(keyPath, children)?.onItemClick) {
findItem(keyPath, children)?.onItemClick?.(findItem(keyPath, children) as ActionItem);
return;
}
onMenuClick?.({

Check warning on line 40 in components/actions/ActionMenu.tsx

View check run for this annotation

Codecov / codecov/patch

components/actions/ActionMenu.tsx#L40

Added line #L40 was not covered by tests
key,
keyPath: [...keyPath, item.key],
domEvent,
item: findItem(keyPath, children)!,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The findItem function assumes that children is always defined when it exists in an item. Consider adding a check to ensure children is not undefined before accessing it to prevent potential runtime errors.

});
},
};

return (
<Dropdown
menu={menuProps}
overlayClassName={`${prefixCls}-sub-item`}
arrow
trigger={[triggerSubMenuAction]}
>
<div className={`${prefixCls}-list-item`}>
<div className={`${prefixCls}-list-item-icon`}>{icon}</div>
</div>
</Dropdown>
);
};

export default ActionMenu;
Loading