All files / components/dialogs ContentDialog.tsx

100% Statements 7/7
70.58% Branches 12/17
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                    4x 4x   4x 1x       4x 1x                                                                                                                       3x                                                                                                                                              
/**
 * ContentDialog - 大内容展示弹窗
 * 用于展示详细信息、报告、条款等大量内容
 */
 
import React from 'react';
import { Keyboard, ScrollView } from 'react-native';
import { Dialog, YStack } from 'tamagui';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { DialogHeader } from './DialogHeader';
import { DIALOG_COLORS, DIALOG_SIZES, BORDER_RADIUS, SPACING } from './constants';
import type { ContentDialogProps } from './types';
 
export function ContentDialog({
  open,
  onOpenChange,
  title = '详情',
  description,
  type = 'default',
  size = 'large',
  children,
  headerActions,
  footerActions,
  showCloseButton = true,
  closeOnOverlayClick = true,
}: ContentDialogProps) {
  const insets = useSafeAreaInsets();
  const sizeConfig = DIALOG_SIZES[size];
 
  const handleClose = () => {
    onOpenChange(false);
  };
 
  // 全屏模式使用 Modal 样式
  if (size === 'fullscreen') {
    return (
      <Dialog modal open={open} onOpenChange={onOpenChange}>
        <Dialog.Portal>
          <Dialog.Content
            key="content"
            animation="quick"
            enterStyle={{ y: '100%', opacity: 0 }}
            exitStyle={{ y: '100%', opacity: 0 }}
            backgroundColor={DIALOG_COLORS.background}
            width="100%"
            height="100%"
            padding="$0"
            pointerEvents="auto"
            borderRadius={0}
          >
            <YStack flex={1}>
              {/* 头部 */}
              <DialogHeader
                title={title}
                description={description}
                type={type}
                layout="left"
                gradient={true}
                onClose={showCloseButton ? handleClose : undefined}
                actions={headerActions}
                includeSafeArea={true}
              />
 
              {/* 内容区域 */}
              <ScrollView
                showsVerticalScrollIndicator={false}
                contentContainerStyle={{
                  padding: SPACING.lg,
                  paddingBottom: Math.max(insets.bottom + SPACING.lg, SPACING.xl),
                }}
              >
                {children}
              </ScrollView>
 
              {/* 底部操作区(可选) */}
              {footerActions && (
                <YStack
                  paddingHorizontal={SPACING.lg}
                  paddingVertical={SPACING.md}
                  paddingBottom={Math.max(insets.bottom, SPACING.md)}
                  borderTopWidth={1}
                  borderTopColor={DIALOG_COLORS.neutral[200]}
                  backgroundColor={DIALOG_COLORS.background}
                >
                  {footerActions}
                </YStack>
              )}
            </YStack>
          </Dialog.Content>
        </Dialog.Portal>
      </Dialog>
    );
  }
 
  // 标准大尺寸弹窗
  return (
    <Dialog modal open={open} onOpenChange={onOpenChange}>
      <Dialog.Portal>
        {/* 遮罩层 */}
        <Dialog.Overlay
          key="overlay"
          animation="quick"
          opacity={0.5}
          enterStyle={{ opacity: 0 }}
          exitStyle={{ opacity: 0 }}
          backgroundColor="black"
          onPress={closeOnOverlayClick ? handleClose : Keyboard.dismiss}
        />
 
        {/* 对话框内容 */}
        <Dialog.Content
          key="content"
          animateOnly={['transform', 'opacity']}
          animation={['quick', { opacity: { overshootClamping: true } }]}
          enterStyle={{ y: -20, opacity: 0, scale: 0.95 }}
          exitStyle={{ y: 10, opacity: 0, scale: 0.95 }}
          backgroundColor={DIALOG_COLORS.background}
          width={sizeConfig.width}
          maxWidth={sizeConfig.maxWidth}
          maxHeight="90%"
          padding="$0"
          pointerEvents="auto"
          borderRadius={BORDER_RADIUS.large}
          overflow="hidden"
        >
          <YStack flex={1}>
            {/* 头部 */}
            <DialogHeader
              title={title}
              description={description}
              type={type}
              layout="left"
              gradient={true}
              onClose={showCloseButton ? handleClose : undefined}
              actions={headerActions}
            />
 
            {/* 内容区域 */}
            <ScrollView
              style={{ flex: 1 }}
              showsVerticalScrollIndicator={false}
              contentContainerStyle={{
                padding: SPACING.lg,
              }}
            >
              {children}
            </ScrollView>
 
            {/* 底部操作区(可选) */}
            {footerActions && (
              <YStack
                paddingHorizontal={SPACING.lg}
                paddingVertical={SPACING.md}
                borderTopWidth={1}
                borderTopColor={DIALOG_COLORS.neutral[200]}
                backgroundColor={DIALOG_COLORS.background}
              >
                {footerActions}
              </YStack>
            )}
          </YStack>
        </Dialog.Content>
      </Dialog.Portal>
    </Dialog>
  );
}