import * as React from "react"; import { Modal, View, Text, Pressable, type ViewStyle, type TextStyle, StyleSheet, StatusBar, Platform, } from "react-native"; import { BlurView } from "expo-blur"; import { cn } from "../../lib/utils"; import { Button, buttonVariants } from "./button"; interface AlertDialogContextValue { open: boolean; setOpen: (open: boolean) => void; } const AlertDialogContext = React.createContext( null ); const useAlertDialogContext = () => { const context = React.useContext(AlertDialogContext); if (!context) { throw new Error( "AlertDialog components must be used within AlertDialog provider" ); } return context; }; interface AlertDialogProps { children: React.ReactNode; open?: boolean; onOpenChange?: (open: boolean) => void; } function AlertDialog({ children, open, onOpenChange }: AlertDialogProps) { const [internalOpen, setInternalOpen] = React.useState(false); const isControlled = open !== undefined; const isOpen = isControlled ? open : internalOpen; const handleSetOpen = React.useCallback( (value: boolean) => { if (!isControlled) { setInternalOpen(value); } onOpenChange?.(value); }, [isControlled, onOpenChange] ); const value: AlertDialogContextValue = { open: isOpen, setOpen: handleSetOpen, }; return ( {children} ); } interface AlertDialogTriggerProps { children: React.ReactNode; asChild?: boolean; style?: ViewStyle; } function AlertDialogTrigger({ children, asChild, style, }: AlertDialogTriggerProps) { const { setOpen } = useAlertDialogContext(); if (asChild && React.isValidElement(children)) { return React.cloneElement(children as React.ReactElement, { onPress: () => setOpen(true), }); } return ( setOpen(true)} style={style}> {children} ); } interface AlertDialogPortalProps { children: React.ReactNode; } function AlertDialogPortal({ children }: AlertDialogPortalProps) { return <>{children}; } interface AlertDialogOverlayProps { className?: string; style?: ViewStyle; } function AlertDialogOverlay({ className, style }: AlertDialogOverlayProps) { return ( {Platform.OS === 'ios' ? ( ) : null} ); } interface AlertDialogContentProps { children: React.ReactNode; className?: string; style?: ViewStyle; } function AlertDialogContent({ children, className, style, }: AlertDialogContentProps) { const { open, setOpen } = useAlertDialogContext(); React.useEffect(() => { if (Platform.OS === "android") { if (open) { StatusBar.setBackgroundColor("rgba(0,0,0,0.5)", true); } else { StatusBar.setBackgroundColor("transparent", true); } } }, [open]); if (!open) return null; return ( setOpen(false)} hardwareAccelerated > setOpen(false)} android_ripple={null} > {children} ); } interface AlertDialogHeaderProps { children: React.ReactNode; className?: string; style?: ViewStyle; } function AlertDialogHeader({ children, className, style, }: AlertDialogHeaderProps) { return ( {children} ); } interface AlertDialogFooterProps { children: React.ReactNode; className?: string; style?: ViewStyle; } function AlertDialogFooter({ children, className, style, }: AlertDialogFooterProps) { return ( {children} ); } interface AlertDialogTitleProps { children: React.ReactNode; className?: string; style?: TextStyle; } function AlertDialogTitle({ children, className, style, }: AlertDialogTitleProps) { return ( {children} ); } interface AlertDialogDescriptionProps { children: React.ReactNode; className?: string; style?: TextStyle; } function AlertDialogDescription({ children, className, style, }: AlertDialogDescriptionProps) { return ( {children} ); } interface AlertDialogActionProps { children: React.ReactNode; className?: string; onPress?: () => void; style?: ViewStyle; } function AlertDialogAction({ children, className, onPress, style, }: AlertDialogActionProps) { const { setOpen } = useAlertDialogContext(); const handlePress = () => { onPress?.(); setOpen(false); }; return ( ); } interface AlertDialogCancelProps { children: React.ReactNode; className?: string; onPress?: () => void; style?: ViewStyle; } function AlertDialogCancel({ children, className, onPress, style, }: AlertDialogCancelProps) { const { setOpen } = useAlertDialogContext(); const handlePress = () => { onPress?.(); setOpen(false); }; return ( ); } interface DeleteAlertDialogActionProps { onPress?: () => void; style?: ViewStyle; className?: string; } function DeleteAlertDialogAction({ onPress, style, className, }: DeleteAlertDialogActionProps) { return ( 确认删除 ); } function CancelAlertDialogAction({ onPress, style, className, }: DeleteAlertDialogActionProps) { return ( 暂不 ); } const styles = StyleSheet.create({ modalContainer: { flex: 1, justifyContent: "center", alignItems: "center", ...Platform.select({ android: { backgroundColor: 'transparent', }, }), }, backdrop: { ...StyleSheet.absoluteFillObject, }, overlay: { zIndex: 1, }, content: { zIndex: 2, }, deleteActionButton: { backgroundColor: "#FA3F2C", alignItems: 'center' }, deleteActionText: { color: "#FFFFFF", fontSize: 14, fontWeight: "500", }, cancelActionButton: { backgroundColor: '#262A31', alignItems: 'center' }, cancelActionText: { color: "#CCCCCC", fontSize: 14, } }); export { AlertDialog, AlertDialogPortal, AlertDialogOverlay, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel, DeleteAlertDialogAction, CancelAlertDialogAction };