index.tsx
Overview
The index.tsx file defines a reusable React functional component named OperateDropdown. This component renders a dropdown menu that provides operation options, primarily focused on deletion actions, alongside any additional custom menu items passed via props. It integrates with the application's internationalization (i18n) system and leverages Ant Design's UI components and icons. The component also supports optional confirmation dialogs before executing delete operations, enhancing user safety against accidental deletions.
Component: OperateDropdown
Description
OperateDropdown is a UI component that displays a dropdown menu triggered by either its children elements or a default "more options" icon. It primarily facilitates deletion of an item, optionally requiring user confirmation before deletion, and supports additional custom menu items.
Props Interface: IProps
Prop Name | Type | Default | Description |
|---|---|---|---|
| [() => Promise \ | void](/projects/311/71659) | Required |
|
| Font size of the default dropdown trigger icon. | |
|
|
| Color of the default dropdown trigger icon. |
|
|
| Additional custom menu items to show in the dropdown. |
|
|
| Height style applied to the dropdown trigger icon container span. |
|
|
| Whether to show a delete confirmation dialog before executing |
|
|
| Whether to show delete-related menu items in the dropdown. |
Function Signature
const OperateDropdown: React.FC<React.PropsWithChildren<IProps>>;
Internal Hooks and Utilities Used
useTranslation(fromreact-i18next): For localization of UI text (e.g., delete label).useShowDeleteConfirm(custom hook): Used to trigger a confirmation dialog before deleting.
Internal Methods
handleDelete()
Description: Handles the deletion process. If
needsDeletionValidationis true, it triggers a confirmation dialog before callingdeleteItem. Otherwise, it immediately callsdeleteItem.Parameters: None
Returns:
voidUsage:
handleDelete();
handleDropdownMenuClick({ domEvent, key })
Description: Event handler for menu item clicks inside the dropdown.
Prevents event propagation and default browser behavior.
If the clicked menu item's key is
'1'(the delete action), it callshandleDelete.Parameters:
domEvent: The DOM event for the click.key: The key of the clicked menu item.
Returns:
voidUsage:
Triggered automatically when a menu item is clicked.
Memoized Variable: items
Description: Constructs the array of menu items shown in the dropdown. Includes a delete menu item (if
showDeleteItemsis true) and appends any additional custom items passed inotherItems.Dependencies:
[showDeleteItems, otherItems, t](translation function)Structure Example:
[
{
key: '1',
label: (
<Space>
{t('common.delete')}
<DeleteOutlined />
</Space>
),
},
...otherItems
]
Render Output
Uses Ant Design's
<Dropdown>component.Dropdown menu is configured with
itemsandhandleDropdownMenuClick.Trigger is either:
childrenelements passed toOperateDropdown, orA default
<MoreOutlined>icon rotated 90 degrees, styled with propsiconFontSize,iconFontColor, andheight.
Usage Example
import OperateDropdown from './index';
const MyComponent = () => {
const deleteHandler = async () => {
// delete logic here
};
const customItems = [
{ key: '2', label: 'Edit' },
{ key: '3', label: 'Duplicate' },
];
return (
<OperateDropdown
deleteItem={deleteHandler}
items={customItems}
iconFontSize={20}
iconFontColor="blue"
needsDeletionValidation={false}
>
<button>Options</button>
</OperateDropdown>
);
};
Important Implementation Details
Confirmation Dialog: The deletion confirmation is handled externally by the
useShowDeleteConfirmhook, which is invoked with anonOkcallback to execute the deletion. This decouples the confirmation UI from the dropdown component.Event Handling: The dropdown menu click handler prevents event bubbling to avoid unintended side effects in parent components.
Internationalization: The delete label text uses the translation key
'common.delete', allowing multilingual support.Performance Optimization: The dropdown menu items are memoized using
useMemoto avoid unnecessary re-renders when dependent props do not change.Flexible Trigger: By allowing
childrenas the dropdown trigger, the component supports custom triggers beyond the default icon.
Interaction with Other Parts of the Application
Hooks:
useShowDeleteConfirm: Integrates with the application's common hooks for showing a deletion confirmation modal.
UI Framework:
Ant Design components (
Dropdown,Space) and icons (DeleteOutlined,MoreOutlined) are used for consistent UI.
Localization:
Integrates with
react-i18nextfor multi-language support.
Styles:
Uses CSS modules (
index.less) for scoped styling, particularly for the default dropdown trigger span.
This component is designed to be used wherever an item-level operation dropdown is needed, typically in lists or tables where users can delete or perform other actions on items.
Visual Diagram: Component Structure and Key Interactions
componentDiagram
component OperateDropdown {
+deleteItem()
+iconFontSize: number
+iconFontColor: string
+items: MenuProps['items']
+height: number
+needsDeletionValidation: boolean
+showDeleteItems: boolean
}
component Dropdown
component Space
component DeleteOutlined
component MoreOutlined
component useShowDeleteConfirm
component useTranslation
OperateDropdown --> Dropdown : renders
Dropdown --> Space : uses for menu labels
Space --> DeleteOutlined : icon in delete menu item
OperateDropdown --> MoreOutlined : default trigger icon
OperateDropdown --> useShowDeleteConfirm : calls for delete confirmation
OperateDropdown --> useTranslation : gets localized text
Summary
The index.tsx file implements a versatile OperateDropdown React component that facilitates safe, localized, and customizable dropdown menus for operations such as deletion. It seamlessly integrates UI, hooks, and internationalization to provide a consistent user experience and flexible API for consuming components.