add-user-modal.tsx
Overview
add-user-modal.tsx defines a React functional component named AddingUserModal that renders a modal dialog for adding a user by inputting their email address. It leverages the Ant Design (antd) UI library for the modal, form, and input components, and integrates internationalization support with the react-i18next library for localized text display.
This component is designed to be reusable and controlled externally via props for visibility, loading state, and callbacks. It handles user input validation and submits the email address entered by the user to the parent component via a callback once the form is successfully validated.
Component: AddingUserModal
Purpose
Provides a modal dialog user interface to input and submit an email address for adding a user.
Props
The component expects props conforming to the generic interface IModalProps<string>, which includes:
Prop | Type | Description |
|---|---|---|
|
| Controls whether the modal is visible or hidden. |
| Callback to hide/close the modal. | |
|
| Indicates a loading state, typically when submitting. |
| (email: string) => any (optional) | Callback invoked upon successful form submission with the entered email. |
Internal Types
FieldType(local type alias): Defines the shape of the form fields used in the modal. Currently only includes:{ email?: string; }
Methods
handleOk
Description:
Asynchronous function that triggers form validation. If validation passes, it extracts theemailfrom the form values and calls theonOkcallback with the email as argument.Parameters: None
Returns:
Returns the result of theonOkcallback, orundefinedifonOkis not provided.Usage:
Bound to the modal's "OK" button (onOkevent). Called when the user clicks "OK" to submit the form.
Rendering
The returned JSX renders:
An Ant Design
<Modal>component configured with:title: Localized string for "Add" (t('setting.add')).open: Controlled byvisibleprop.onOk: CallshandleOk.onCancel: CallshideModal.okButtonProps: Passesloadingto disable the button and show spinner.confirmLoading: Shows loading spinner on the modal's OK button.
Inside the modal, an Ant Design
<Form>with:Controlled by the
forminstance returned fromForm.useForm().Layout settings for label and input width.
A single form item for
emailwith:Label localized as "Email" (
t('setting.email')).Required validation rule (
rules={[{ required: true }]}).An
<Input>component for user entry.
Usage Example
import AddingUserModal from './add-user-modal';
const ParentComponent = () => {
const [modalVisible, setModalVisible] = React.useState(false);
const [loading, setLoading] = React.useState(false);
const handleAddUser = async (email: string) => {
setLoading(true);
try {
await apiAddUserByEmail(email); // hypothetical API call
setModalVisible(false);
} finally {
setLoading(false);
}
};
return (
<>
<button onClick={() => setModalVisible(true)}>Add User</button>
<AddingUserModal
visible={modalVisible}
hideModal={() => setModalVisible(false)}
loading={loading}
onOk={handleAddUser}
/>
</>
);
};
Implementation Details and Algorithms
Form Validation:
Uses Ant Design'sFormcomponent'svalidateFields()method to trigger synchronous validation of the form fields before submission.Localization:
UtilizesuseTranslationhook fromreact-i18nextto provide translation keys for the modal title and form labels, enabling multilingual UI support.Async Handling:
ThehandleOkfunction is asynchronous to accommodate any asynchronous validation or submission logic encapsulated in theonOkcallback.State Management:
The modal visibility and loading states are controlled externally via props, making this component a controlled component without its own state for these aspects.
Interaction with Other Parts of the System
IModalPropsInterface:
The component expects props shaped by theIModalPropsinterface imported from@/interfaces/common, which likely defines common modal props across the application.Internationalization Setup:
The file depends on the application's i18n setup and translation files containing keys such assetting.addandsetting.email.Parent Components:
AddingUserModalis intended to be used by parent components that manage modal visibility and handle the submitted email (e.g., to add a new user in the system).API Integration:
The actual user addition logic is expected to be implemented by the parent component in theonOkhandler, which receives the validated email.
Mermaid Component Diagram
componentDiagram
component AddingUserModal {
+props: IModalProps<string>
+handleOk(): Promise<void>
}
component Modal {
+title: string
+open: boolean
+onOk: function
+onCancel: function
+okButtonProps: object
+confirmLoading: boolean
}
component Form {
+form: FormInstance
+validateFields(): Promise
}
component Input
AddingUserModal --> Modal : renders
Modal o-- Form : contains
Form o-- Input : contains
Summary
add-user-modal.tsx provides a reusable, localized modal dialog component for adding a user by entering an email address. It leverages Ant Design components and React hooks for form handling and i18n. The component is controlled externally and communicates with parent components through callbacks, making it flexible for integration in various parts of the application that require user addition functionality.