index.tsx
Overview
The index.tsx file defines a React functional component named RenameModal. This component presents a modal dialog that allows users to rename an entity by entering a new name. It uses Ant Design (antd) components such as Modal, Form, and Input to build the UI, and integrates with a translation hook to support internationalization.
The modal provides form validation to ensure the new name is entered before submission. It exposes callbacks to notify the parent component when the user confirms the rename operation (onOk) or cancels the modal. It also supports loading state control while the rename operation is processing.
Detailed Explanation
Component: RenameModal
Purpose
RenameModal is a reusable modal dialog component for renaming operations. It displays an input field pre-filled with the current name and allows users to submit a new name. It handles UI state, form validation, and user interaction.
Props (IProps)
Prop Name | Type | Required | Description |
|---|---|---|---|
|
| Yes | Controls whether the modal is visible or hidden. |
| Yes | Function to hide/close the modal. | |
|
| Yes | Indicates if the modal's confirmation action is in a loading state. |
|
| Yes | The current name to be shown in the input field when the modal opens. |
| Yes | Callback executed when the user confirms the new name after validation. | |
| No | Optional function to show the modal (not used internally). |
Note: The component extends
IModalManagerChildrenPropsbut omitsshowModal.
Internal Types
FieldType: Type representing form fields with optionalnameproperty.
Internal Methods and Handlers
handleOk
const handleOk = async (): Promise<void> => {
const ret = await form.validateFields();
return onOk(ret.name);
};
Description: Called when the user clicks the modal's OK button.
Functionality: Validates the form fields asynchronously. If validation passes, calls the parent
onOkcallback with the new name.Returns: A Promise that resolves after validation and callback invocation.
handleCancel
const handleCancel = (): void => {
hideModal();
};
Description: Called when the user cancels or closes the modal.
Functionality: Invokes the
hideModalfunction to close the modal.
onFinish
const onFinish = (values: any): void => {
console.log('Success:', values);
};
Description: Handler for successful form submission.
Functionality: Logs form values to the console. (Primarily for debugging; actual submission handled by
handleOk.)
onFinishFailed
const onFinishFailed = (errorInfo: any): void => {
console.log('Failed:', errorInfo);
};
Description: Handler for failed form submission due to validation errors.
Functionality: Logs error information to the console.
React Hook: useEffect
useEffect(() => {
if (visible) {
form.setFieldValue('name', initialName);
}
}, [initialName, form, visible]);
Purpose: When the modal becomes visible or the initial name changes, this hook sets the form's
namefield to the currentinitialName.Effect: Ensures the input field is always initialized with the correct current name when the modal opens.
Rendered JSX Structure
Modal(fromantd):Props:
title: Translated string for "rename".open: Controlled byvisibleprop.onOk: CallshandleOk.onCancel: CallshandleCancel.okButtonProps: Passes loading state to disable button and show spinner.confirmLoading: Controls loading spinner on OK button.
Form(fromantd):Controlled by
forminstance.Layout with label and wrapper columns.
Calls
onFinishandonFinishFailedon submit.Auto-completion disabled.
Contains one
Form.Item:Label: Translated "name".
Name:
"name".Validation: Required with a custom translated placeholder message.
Input: Single-line text input.
Usage Example
import React, { useState } from 'react';
import RenameModal from './index';
function ExampleParentComponent() {
const [modalVisible, setModalVisible] = useState(false);
const [loading, setLoading] = useState(false);
const [entityName, setEntityName] = useState('Initial Name');
const handleRename = (newName: string) => {
setLoading(true);
// Simulate async rename operation
setTimeout(() => {
setEntityName(newName);
setLoading(false);
setModalVisible(false);
}, 1000);
};
return (
<>
<button onClick={() => setModalVisible(true)}>Rename</button>
<RenameModal
visible={modalVisible}
hideModal={() => setModalVisible(false)}
loading={loading}
initialName={entityName}
onOk={handleRename}
/>
</>
);
}
Important Implementation Details
Form Validation: Uses Ant Design's
form.validateFields()to enforce that the name is not empty before submission.Internationalization: Text content is dynamically translated via the
useTranslatehook scoped to the'common'namespace.Loading State: The OK button shows a spinner and disables itself based on the
loadingprop, improving UX during asynchronous rename operations.Modal Visibility Control: The component itself does not control visibility; it relies on external props
visibleandhideModalfor showing/hiding.
Interaction with Other Parts of the System
IModalManagerChildrenProps: The component expects to receive modal management props from a modal manager system, except for theshowModalfunction which is omitted.useTranslatehook: This hook is imported from a common hooks module (@/hooks/common-hooks) and provides translation capabilities, indicating that the app supports localization.Parent Components: The parent component controls when to show/hide the modal, handles the rename logic (e.g., updating state or making API calls), and passes down the current name and loading status.
Ant Design Library: Relies heavily on Ant Design's UI components for modal and form handling.
Mermaid Component Diagram
componentDiagram
RenameModal <|-- IProps
RenameModal : +visible: boolean
RenameModal : +hideModal(): void
RenameModal : +loading: boolean
RenameModal : +initialName: string
RenameModal : +onOk(name: string): void
RenameModal : -handleOk(): Promise<void>
RenameModal : -handleCancel(): void
RenameModal : -onFinish(values): void
RenameModal : -onFinishFailed(errorInfo): void
RenameModal --> "antd Modal"
RenameModal --> "antd Form"
RenameModal --> "antd Input"
RenameModal --> useTranslate()
Summary
The index.tsx file provides a well-structured, internationalized React modal component for renaming operations. It leverages Ant Design components for UI and validation, supports asynchronous loading states, and cleanly separates modal visibility control and rename logic to the parent component. Its simple yet effective design makes it a reusable modal for any entity renaming requirement within the application.