// audit:allow extension-dist-source-literal-sync 미등록(components.json 부재)·미참조 컴포넌트 — 배럴(modals/index.ts)만 재수출할 뿐 소비처가 없어 tree-shake 된다 import React, { useEffect, useRef } from 'react'; import { Div } from '../basic/Div'; import { Button } from '../basic/Button'; import { P } from '../basic/P'; import { Span } from '../basic/Span'; import { Icon } from '../basic/Icon'; import { IconName } from '../basic/IconTypes'; // G7Core.t() 번역 함수 참조 const t = (key: string, params?: Record) => (window as any).G7Core?.t?.(key, params) ?? key; /** Alert 박스 타입 */ export type ConfirmModalAlertType = 'warning' | 'danger' | 'info'; /** Alert 박스 설정 */ export interface ConfirmModalAlert { /** Alert 타입 (warning, danger, info) - 기본값: warning */ type?: ConfirmModalAlertType; /** Alert 제목 (예: "경고") */ title?: string; /** Alert 내용 (문자열 배열은 리스트로, ReactNode는 그대로 렌더링) */ content: string[] | React.ReactNode; } export interface ConfirmModalProps { /** 모달 열림 상태 */ isOpen: boolean; /** 모달 닫기 핸들러 */ onClose: () => void; /** 모달 제목 (기본값: common.confirm) */ title?: string; /** 확인 메시지 */ message: string | React.ReactNode; /** 확인 버튼 텍스트 (기본값: common.confirm) */ confirmText?: string; /** 취소 버튼 텍스트 (기본값: common.cancel) */ cancelText?: string; /** 확인 버튼 클릭 핸들러 */ onConfirm: () => void; /** 취소 버튼 클릭 핸들러 */ onCancel?: () => void; /** 확인 버튼 variant */ confirmButtonVariant?: 'primary' | 'secondary' | 'danger' | 'ghost'; /** 로딩 중 여부 (확인 버튼 비활성화 및 텍스트 변경) */ isLoading?: boolean; /** 로딩 중 확인 버튼에 표시할 텍스트 */ loadingText?: string; /** 모달 너비 (기본값: 400px) */ width?: string; /** 경고/추가 정보 영역 (Alert 박스) */ alert?: ConfirmModalAlert; /** 추가 경고 메시지 (빨간색 텍스트로 표시) */ warningText?: string; } /** * ConfirmModal 컴포넌트 * * 사용자에게 확인을 요청하는 모달 다이얼로그 컴포넌트. * 확인/취소 버튼을 제공하며, 로딩 상태를 지원합니다. * * @example * // 레이아웃 JSON 사용 예시 * { * "name": "ConfirmModal", * "props": { * "isOpen": "{{modals.saveConfirm}}", * "title": "확인", * "message": "변경사항을 저장하시겠습니까?", * "confirmText": "$t:common.confirm", * "cancelText": "$t:common.cancel" * } * } */ export const ConfirmModal: React.FC = ({ isOpen, onClose, title, message, confirmText, cancelText, onConfirm, onCancel, confirmButtonVariant = 'primary', isLoading = false, loadingText, width = '400px', alert, warningText, }) => { const modalRef = useRef(null); const previousFocusRef = useRef(null); // props로 전달된 값이 없으면 다국어 키 사용 const resolvedTitle = title ?? t('common.confirm'); const resolvedConfirmText = confirmText ?? t('common.confirm'); const resolvedCancelText = cancelText ?? t('common.cancel'); const resolvedLoadingText = loadingText ?? t('common.loading'); // ESC 키로 모달 닫기 useEffect(() => { const handleEscape = (event: KeyboardEvent) => { if (event.key === 'Escape' && isOpen && !isLoading) { onClose(); } }; if (isOpen) { document.addEventListener('keydown', handleEscape); return () => document.removeEventListener('keydown', handleEscape); } }, [isOpen, isLoading, onClose]); // 포커스 트랩 useEffect(() => { if (isOpen && modalRef.current) { // 모달 열릴 때 현재 포커스 저장 previousFocusRef.current = document.activeElement as HTMLElement; // 모달 내부 포커스 가능한 요소들 const focusableElements = modalRef.current.querySelectorAll( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' ); const firstElement = focusableElements[0] as HTMLElement; const lastElement = focusableElements[focusableElements.length - 1] as HTMLElement; // 첫 번째 요소에 포커스 firstElement?.focus(); // Tab 키 핸들러 (포커스 트랩) const handleTab = (event: KeyboardEvent) => { if (event.key !== 'Tab') return; if (event.shiftKey) { // Shift + Tab if (document.activeElement === firstElement) { event.preventDefault(); lastElement?.focus(); } } else { // Tab if (document.activeElement === lastElement) { event.preventDefault(); firstElement?.focus(); } } }; document.addEventListener('keydown', handleTab); return () => { document.removeEventListener('keydown', handleTab); // 모달 닫힐 때 이전 포커스 복원 previousFocusRef.current?.focus(); }; } }, [isOpen]); // body 스크롤 방지 useEffect(() => { if (isOpen) { document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = ''; }; } }, [isOpen]); const handleConfirm = () => { onConfirm(); }; const handleCancel = () => { // 로딩 중에는 취소 불가 if (isLoading) return; onCancel?.(); onClose(); }; const handleClose = () => { // 로딩 중에는 닫기 불가 if (isLoading) return; onClose(); }; const handleOverlayClick = () => { // 로딩 중에는 오버레이 클릭으로 닫기 불가 if (isLoading) return; onClose(); }; // 버튼 variant별 스타일 매핑 (스크린샷 기준: 어두운 확인 버튼) const confirmVariantClassMap: Record<'primary' | 'secondary' | 'danger' | 'ghost', string> = { primary: 'bg-gray-900 dark:bg-gray-700 text-white hover:bg-gray-800 dark:hover:bg-gray-600', secondary: 'bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-600', danger: 'bg-red-600 text-white hover:bg-red-700', ghost: 'bg-transparent text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700', }; // Alert 타입별 스타일 매핑 const alertStyleMap: Record = { warning: { container: 'bg-yellow-50 dark:bg-yellow-900/20 border-l-4 border-yellow-400 dark:border-yellow-500', icon: 'text-yellow-600 dark:text-yellow-400', title: 'text-yellow-800 dark:text-yellow-300', text: 'text-yellow-700 dark:text-yellow-300', }, danger: { container: 'bg-red-50 dark:bg-red-900/20 border-l-4 border-red-400 dark:border-red-500', icon: 'text-red-600 dark:text-red-400', title: 'text-red-800 dark:text-red-300', text: 'text-red-700 dark:text-red-300', }, info: { container: 'bg-blue-50 dark:bg-blue-900/20 border-l-4 border-blue-400 dark:border-blue-500', icon: 'text-blue-600 dark:text-blue-400', title: 'text-blue-800 dark:text-blue-300', text: 'text-blue-700 dark:text-blue-300', }, }; // Alert 렌더링 함수 const renderAlert = () => { if (!alert) return null; const alertType = alert.type ?? 'warning'; const styles = alertStyleMap[alertType]; return (
{/* Alert 헤더 (아이콘 + 제목) */} {alert.title && (
{alert.title}
)} {/* Alert 내용 */}
{Array.isArray(alert.content) ? (
    {alert.content.map((item, index) => (
  • {item}
  • ))}
) : ( alert.content )}
); }; if (!isOpen) return null; return (
{/* Overlay */} ); };