confirm-delete-dialog.tsx


Overview

confirm-delete-dialog.tsx defines a reusable React component named ConfirmDeleteDialog that provides a standardized modal dialog for confirming deletion actions within a user interface. It leverages a pre-built AlertDialog UI component set to display a modal with customizable title, cancel, and confirm buttons.

This component is designed to wrap any child element(s), transforming them into a trigger for the confirmation dialog. It supports internationalization via the react-i18next library, enabling multi-language support for all UI text strings.


Detailed Explanation

ConfirmDeleteDialog Component

Purpose

To present a modal dialog that asks users to confirm or cancel a deletion action, preventing accidental data loss by requiring explicit user confirmation.

Props

Prop Name

Type

Default

Description

title

string (optional)

i18n default string 'common.deleteModalTitle'

The dialog title text. If not provided, a default localized string is used.

onOk

(...args: any[]) => any (optional)

undefined

Callback function invoked when the user confirms (clicks OK) the deletion.

onCancel

(...args: any[]) => any (optional)

undefined

Callback function invoked when the user cancels the dialog.

hidden

boolean

false

If true, the dialog is not rendered and only the children are returned, effectively disabling the confirmation dialog.

children

React.ReactNode

N/A

The child element(s) that will serve as the trigger for opening the dialog.

Signature

function ConfirmDeleteDialog({
  children,
  title,
  onOk,
  onCancel,
  hidden = false,
}: IProps & PropsWithChildren): JSX.Element

Usage Example

import { ConfirmDeleteDialog } from './confirm-delete-dialog';

function DeleteButton() {
  const handleConfirm = () => {
    // Perform delete operation
    console.log('Item deleted');
  };

  const handleCancel = () => {
    console.log('Delete canceled');
  };

  return (
    <ConfirmDeleteDialog
      title="Are you sure you want to delete this item?"
      onOk={handleConfirm}
      onCancel={handleCancel}
    >
      <button>Delete Item</button>
    </ConfirmDeleteDialog>
  );
}

Implementation Details


Interaction with Other Parts of the System


Mermaid Component Diagram

componentDiagram
    component ConfirmDeleteDialog {
      +children: ReactNode
      +title?: string
      +onOk?: function
      +onCancel?: function
      +hidden?: boolean
      +render()
    }

    component AlertDialog {
      +AlertDialogTrigger
      +AlertDialogContent
      +AlertDialogHeader
      +AlertDialogTitle
      +AlertDialogFooter
      +AlertDialogCancel
      +AlertDialogAction
    }

    ConfirmDeleteDialog --> AlertDialog : uses
    ConfirmDeleteDialog --> AlertDialogTrigger : wraps children
    ConfirmDeleteDialog --> AlertDialogContent : renders modal content
    ConfirmDeleteDialog --> AlertDialogCancel : cancel button
    ConfirmDeleteDialog --> AlertDialogAction : confirm button

Summary

The ConfirmDeleteDialog component encapsulates the common UI pattern of confirming destructive actions with a modal dialog. It is flexible, accessible, internationalized, and designed for easy integration by wrapping existing UI elements. It relies on a shared alert dialog UI library and i18n infrastructure, promoting consistency and localization across the application.