|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import React, { useEffect, useRef, useMemo } from 'react'; |
|
|
import { useEnhancedDialog } from './useEnhancedDialog'; |
|
|
import { EnhancedDialogProps } from './types'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const EnhancedDialog: React.FC<EnhancedDialogProps> = ({ |
|
|
isOpen, |
|
|
onCancel, |
|
|
onConfirm, |
|
|
children, |
|
|
className, |
|
|
backdropDismiss = true, |
|
|
showButtons = true, |
|
|
confirmLabel = '确定', |
|
|
cancelLabel = '取消', |
|
|
danger = false, |
|
|
disableKeyboardShortcuts = false, |
|
|
}) => { |
|
|
const modalPresentedRef = useRef(false); |
|
|
const presentRef = useRef<(() => void) | null>(null); |
|
|
const dismissRef = useRef<(() => void) | null>(null); |
|
|
|
|
|
|
|
|
const dialogOptions = useMemo(() => ({ |
|
|
children, |
|
|
className, |
|
|
backdropDismiss, |
|
|
showButtons, |
|
|
confirmLabel, |
|
|
cancelLabel, |
|
|
danger, |
|
|
disableKeyboardShortcuts, |
|
|
onCancel, |
|
|
onConfirm, |
|
|
}), [children, className, backdropDismiss, showButtons, confirmLabel, cancelLabel, danger, disableKeyboardShortcuts, onCancel, onConfirm]); |
|
|
|
|
|
const [present, dismiss] = useEnhancedDialog(dialogOptions); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
presentRef.current = present; |
|
|
dismissRef.current = dismiss; |
|
|
}, [present, dismiss]); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
if (isOpen && !modalPresentedRef.current) { |
|
|
presentRef.current?.(); |
|
|
modalPresentedRef.current = true; |
|
|
} else if (!isOpen && modalPresentedRef.current) { |
|
|
dismissRef.current?.(); |
|
|
modalPresentedRef.current = false; |
|
|
} |
|
|
}, [isOpen]); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
return () => { |
|
|
if (modalPresentedRef.current && dismissRef.current) { |
|
|
dismissRef.current(); |
|
|
} |
|
|
}; |
|
|
}, []); |
|
|
|
|
|
|
|
|
return null; |
|
|
}; |
|
|
|
|
|
|