Files
ServerKit/frontend/src/components/ConfirmDialog.jsx
T
Juan Denis 0adb6bf752 Tie form labels to their fields
156 labels named no field: no htmlFor and no control inside, so
clicking one did not focus the input and screen readers announced the
input without a name. Each now points at its field through a useId()
id. The change is additive only (ids and htmlFor), checked by comparing
tokens with the previous version.
2026-10-10 22:25:37 -04:00

96 lines
3.4 KiB
React

import { useState, useEffect, useId } from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { AlertTriangle, Info, AlertCircle } from 'lucide-react';
import {
AlertDialog,
AlertDialogContent,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogCancel,
AlertDialogAction,
} from '@/components/ui/alert-dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
// Styling lives in styles/components/_ui.scss (.sk-confirm*).
const iconMap = { danger: AlertTriangle, warning: AlertCircle, info: Info };
export function ConfirmDialog({
isOpen,
title,
message,
details,
confirmText,
cancelText,
variant = 'danger',
requireConfirmation,
confirmationPlaceholder,
onConfirm,
onCancel,
}) {
const { t } = useTranslation();
const fieldId = useId();
const [inputValue, setInputValue] = useState('');
useEffect(() => { if (isOpen) setInputValue(''); }, [isOpen]);
const Icon = iconMap[variant] || AlertTriangle;
const isConfirmDisabled = requireConfirmation && inputValue !== requireConfirmation;
return (
<AlertDialog open={isOpen} onOpenChange={(v) => !v && onCancel()}>
<AlertDialogContent className="sk-confirm">
<AlertDialogHeader>
<div className="sk-confirm__head">
<div className={`sk-confirm__icon sk-confirm__icon--${variant}`}>
<Icon size={24} />
</div>
<div className="sk-confirm__body">
<AlertDialogTitle>{title}</AlertDialogTitle>
{message && <AlertDialogDescription>{message}</AlertDialogDescription>}
{details && <p className="sk-confirm__details">{details}</p>}
</div>
</div>
{requireConfirmation && (
<div className="sk-confirm__confirm-field">
<Label htmlFor={`${fieldId}-confirm`}>
{/* One key, not a prefix/suffix pair: word order around the
typed value differs by language, and split sentences cannot
be reordered by a translator. */}
<Trans
i18nKey="common.confirm.typeToConfirm"
defaults="Type <0>{{value}}</0> to confirm:"
values={{ value: requireConfirmation }}
components={[<strong key="value" className="text-foreground" />]}
/>
</Label>
<Input
id={`${fieldId}-confirm`}
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && !isConfirmDisabled && onConfirm()}
placeholder={confirmationPlaceholder || requireConfirmation}
autoFocus
/>
</div>
)}
</AlertDialogHeader>
<AlertDialogFooter className="sk-confirm__footer">
<AlertDialogCancel onClick={onCancel}>{cancelText ?? t('common.actions.cancel', 'Cancel')}</AlertDialogCancel>
<AlertDialogAction
onClick={onConfirm}
disabled={isConfirmDisabled}
variant={variant === 'danger' ? 'destructive' : 'primary'}
>
{confirmText ?? t('common.actions.confirm', 'Confirm')}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
export default ConfirmDialog;