|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { useCallback, useRef, useEffect } from 'react'; |
|
|
import ReactDOM from 'react-dom/client'; |
|
|
import { I18nextProvider } from 'react-i18next'; |
|
|
import i18n from '../../i18n/i18n'; |
|
|
import { EnhancedDialogContent } from './EnhancedDialogContent'; |
|
|
import { EnhancedDialogOptions, DismissRole } from './types'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useEnhancedDialog = (options: EnhancedDialogOptions) => { |
|
|
const { |
|
|
children, |
|
|
className, |
|
|
backdropDismiss = true, |
|
|
showButtons = true, |
|
|
confirmLabel = '确定', |
|
|
cancelLabel = '取消', |
|
|
danger = false, |
|
|
disableKeyboardShortcuts = false, |
|
|
onDismiss, |
|
|
onConfirm, |
|
|
onCancel, |
|
|
} = options; |
|
|
|
|
|
const containerRef = useRef<HTMLDivElement | null>(null); |
|
|
const rootRef = useRef<ReactDOM.Root | null>(null); |
|
|
const optionsRef = useRef(options); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
optionsRef.current = options; |
|
|
}, [options]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const dismiss = useCallback((role?: DismissRole) => { |
|
|
const currentRoot = rootRef.current; |
|
|
const currentContainer = containerRef.current; |
|
|
|
|
|
if (currentRoot && currentContainer) { |
|
|
|
|
|
onDismiss?.(role); |
|
|
if (role === 'confirm') { |
|
|
onConfirm?.(); |
|
|
} else if (role === 'cancel' || role === 'backdrop') { |
|
|
onCancel?.(); |
|
|
} |
|
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
currentRoot.unmount(); |
|
|
if (currentContainer.parentNode) { |
|
|
document.body.removeChild(currentContainer); |
|
|
} |
|
|
rootRef.current = null; |
|
|
containerRef.current = null; |
|
|
}, 0); |
|
|
} |
|
|
}, [onDismiss, onConfirm, onCancel]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const present = useCallback(() => { |
|
|
|
|
|
if (rootRef.current && containerRef.current) { |
|
|
rootRef.current.render( |
|
|
<I18nextProvider i18n={i18n}> |
|
|
<EnhancedDialogContent |
|
|
onDismiss={dismiss} |
|
|
children={children} |
|
|
showButtons={showButtons} |
|
|
confirmLabel={confirmLabel} |
|
|
cancelLabel={cancelLabel} |
|
|
danger={danger} |
|
|
className={className} |
|
|
backdropDismiss={backdropDismiss} |
|
|
disableKeyboardShortcuts={disableKeyboardShortcuts} |
|
|
/> |
|
|
</I18nextProvider> |
|
|
); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const newContainer = document.createElement('div'); |
|
|
newContainer.id = 'enhanced-dialog-container-' + Date.now(); |
|
|
document.body.appendChild(newContainer); |
|
|
|
|
|
|
|
|
|
|
|
const newRoot = ReactDOM.createRoot(newContainer); |
|
|
newRoot.render( |
|
|
<I18nextProvider i18n={i18n}> |
|
|
<EnhancedDialogContent |
|
|
onDismiss={dismiss} |
|
|
children={children} |
|
|
showButtons={showButtons} |
|
|
confirmLabel={confirmLabel} |
|
|
cancelLabel={cancelLabel} |
|
|
danger={danger} |
|
|
className={className} |
|
|
backdropDismiss={backdropDismiss} |
|
|
disableKeyboardShortcuts={disableKeyboardShortcuts} |
|
|
/> |
|
|
</I18nextProvider> |
|
|
); |
|
|
|
|
|
containerRef.current = newContainer; |
|
|
rootRef.current = newRoot; |
|
|
}, [children, className, backdropDismiss, showButtons, confirmLabel, cancelLabel, danger, disableKeyboardShortcuts, dismiss]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
return () => { |
|
|
const currentRoot = rootRef.current; |
|
|
const currentContainer = containerRef.current; |
|
|
|
|
|
if (currentRoot && currentContainer) { |
|
|
currentRoot.unmount(); |
|
|
if (currentContainer.parentNode) { |
|
|
document.body.removeChild(currentContainer); |
|
|
} |
|
|
rootRef.current = null; |
|
|
containerRef.current = null; |
|
|
} |
|
|
}; |
|
|
}, []); |
|
|
|
|
|
return [present, dismiss] as const; |
|
|
}; |
|
|
|
|
|
|