Files
router-lists-ui/frontend/src/components/ConfirmDialog.jsx
T

42 lines
2.2 KiB
React

import { useEffect, useRef } from 'react'
export default function ConfirmDialog({ open, title, message, confirmText = 'Подтвердить', cancelText = 'Отмена', onConfirm, onCancel, destructive = false, size = 'md' }) {
const ref = useRef(null)
useEffect(() => {
if (open && ref.current) {
try { ref.current.querySelector('button[data-primary]')?.focus() } catch {}
}
}, [open])
if (!open) return null
return (
<div className="modal show d-block" role="dialog" aria-modal="true" aria-labelledby="confirm-title" aria-describedby="confirm-message" style={{ backgroundColor: 'rgba(0,0,0,0.5)' }} onKeyDown={(e) => { if (e.key === 'Escape') onCancel?.() }}>
<div className={`modal-dialog ${size === 'sm' ? 'modal-sm' : size === 'lg' ? 'modal-lg' : ''}`} role="document">
<div className="modal-content" ref={ref} tabIndex={-1} onKeyDown={(e) => {
if (e.key === 'Tab') {
const focusable = ref.current?.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])')
if (!focusable || focusable.length === 0) return
const first = focusable[0]
const last = focusable[focusable.length - 1]
if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); }
else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); }
}
}}>
<div className="modal-header">
<h5 id="confirm-title" className="modal-title">{title || 'Подтверждение'}</h5>
<button type="button" className="btn-close" aria-label="Close" onClick={onCancel}></button>
</div>
<div className="modal-body">
<p id="confirm-message" className="m-0">{message}</p>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" onClick={onCancel}>{cancelText}</button>
<button type="button" className={`btn ${destructive ? 'btn-danger' : 'btn-primary'}`} data-primary onClick={onConfirm}>{confirmText}</button>
</div>
</div>
</div>
</div>
)
}