All files / components/dialogs FormDialog.tsx

82.35% Statements 14/17
68.42% Branches 13/19
75% Functions 3/4
82.35% Lines 14/17

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                                                            5x   5x 1x 1x 1x 1x       1x       5x 1x 1x     5x           5x 5x   5x                                                                                                                                                    
/**
 * FormDialog - 表单弹窗组件
 * 用于收集用户输入(添加、编辑等操作)
 */
 
import React, { useState } from 'react';
import { Keyboard, ScrollView, TouchableWithoutFeedback } from 'react-native';
import { Dialog, YStack } from 'tamagui';
import { DialogHeader } from './DialogHeader';
import { DialogFooter } from './DialogFooter';
import { DIALOG_COLORS, DIALOG_SIZES, BORDER_RADIUS, SPACING } from './constants';
import type { FormDialogProps } from './types';
 
export function FormDialog({
  open,
  onOpenChange,
  title = '表单',
  description,
  type = 'default',
  size = 'medium',
  children,
  submitText = '提交',
  cancelText = '取消',
  onSubmit,
  onCancel,
  isSubmitting = false,
  submitDisabled = false,
  showCloseButton = true,
  closeOnOverlayClick = false,
}: FormDialogProps) {
  const [isProcessing, setIsProcessing] = useState(false);
 
  const handleSubmit = async () => {
    try {
      setIsProcessing(true);
      await onSubmit();
      onOpenChange(false);
    } catch (error) {
      console.error('Form submission failed:', error);
    } finally {
      setIsProcessing(false);
    }
  };
 
  const handleCancel = () => {
    onCancel?.();
    onOpenChange(false);
  };
 
  const handleClose = () => {
    if (!isProcessing && !isSubmitting) {
      onOpenChange(false);
    }
  };
 
  const sizeConfig = DIALOG_SIZES[size];
  const loading = isSubmitting || isProcessing;
 
  return (
    <Dialog modal open={open} onOpenChange={onOpenChange}>
      <Dialog.Portal>
        {/* 遮罩层 */}
        <Dialog.Overlay
          testID="dialog-overlay"
          key="overlay"
          animation="quick"
          opacity={0.5}
          enterStyle={{ opacity: 0 }}
          exitStyle={{ opacity: 0 }}
          backgroundColor="black"
          style={{ pointerEvents: 'auto' }}
          onPress={closeOnOverlayClick ? handleClose : Keyboard.dismiss}
        />
 
        {/* 对话框内容 */}
        <Dialog.Content
          testID="dialog-root"
          bordered
          key="content"
          animateOnly={['transform', 'opacity']}
          animation="quick"
          enterStyle={{ x: 0, y: -20, opacity: 0, scale: 0.9 }}
          exitStyle={{ x: 0, y: 10, opacity: 0, scale: 0.95 }}
          backgroundColor={DIALOG_COLORS.background}
          width={sizeConfig.width}
          maxWidth={sizeConfig.maxWidth}
          maxHeight="85%"
          padding="$0"
          pointerEvents="auto"
          borderRadius={BORDER_RADIUS.large}
          overflow="hidden"
        >
          {/* 头部 */}
          <DialogHeader
            title={title}
            description={description}
            type={type}
            layout="left"
            gradient={true}
            onClose={showCloseButton ? handleClose : undefined}
          />
 
          {/* 表单内容区域 */}
          <ScrollView
            style={{ maxHeight: 450 }}
            showsVerticalScrollIndicator={false}
            keyboardShouldPersistTaps="handled"
            nestedScrollEnabled={true}
            scrollEnabled={true}
            contentContainerStyle={{ pointerEvents: 'auto' }}
          >
            <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
              <YStack padding={SPACING.lg} gap={SPACING.md}>
                {children}
              </YStack>
            </TouchableWithoutFeedback>
          </ScrollView>
 
          {/* 底部操作按钮 */}
          <DialogFooter
            cancelText={cancelText}
            confirmText={submitText}
            onCancel={handleCancel}
            onConfirm={handleSubmit}
            confirmDisabled={submitDisabled}
            confirmLoading={loading}
          />
        </Dialog.Content>
      </Dialog.Portal>
    </Dialog>
  );
}