File size: 3,567 Bytes
07bbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * EnhancedDialogContent Component
 * 
 * Internal dialog content component that renders the actual dialog UI
 */

import React, { useEffect, useCallback, useRef } from 'react';
import { IonButton, IonFooter, IonToolbar } from '@ionic/react';
import { EnhancedDialogContentProps } from './types';
import styles from './EnhancedDialog.module.css';

/**
 * Internal dialog content component
 * Handles keyboard interactions, backdrop clicks, and rendering
 */
export const EnhancedDialogContent: React.FC<EnhancedDialogContentProps> = ({
  onDismiss,
  children,
  showButtons = true,
  confirmLabel = '确定',
  cancelLabel = '取消',
  danger = false,
  className,
  backdropDismiss = true,
  disableKeyboardShortcuts = false,
}) => {
  const dialogRef = useRef<HTMLDivElement>(null);

  /**
   * Handle keyboard shortcuts
   * - Enter: Confirm (unless in textarea or contenteditable)
   * - Escape: Cancel
   */
  const handleKeyDown = useCallback((event: KeyboardEvent) => {
    // Skip if keyboard shortcuts are disabled
    if (disableKeyboardShortcuts) return;

    const target = event.target as HTMLElement;
    const isInputElement = target.tagName === 'INPUT' || 
                          target.tagName === 'TEXTAREA' ||
                          target.isContentEditable;

    switch (event.key) {
      case 'Enter':
        // Only handle Enter if focus is not in a textarea or contenteditable
        // Single-line inputs can submit on Enter
        if (!isInputElement || target.tagName === 'INPUT') {
          event.preventDefault();
          event.stopPropagation();
          onDismiss('confirm');
        }
        break;
      case 'Escape':
        event.preventDefault();
        event.stopPropagation();
        onDismiss('cancel');
        break;
      default:
        break;
    }
  }, [onDismiss, disableKeyboardShortcuts]);

  // Add global keydown listener
  useEffect(() => {
    document.addEventListener('keydown', handleKeyDown);
    return () => {
      document.removeEventListener('keydown', handleKeyDown);
    };
  }, [handleKeyDown]);

  /**
   * Handle backdrop click to dismiss
   */
  const handleBackdropClick = (e: React.MouseEvent) => {
    // Only trigger if clicking directly on backdrop (not its children)
    if (backdropDismiss && e.target === e.currentTarget) {
      onDismiss('backdrop');
    }
  };

  const handleConfirmClick = () => {
    onDismiss('confirm');
  };

  const handleCancelClick = () => {
    onDismiss('cancel');
  };

  // Combine CSS module class with custom className
  const dialogClassName = className 
    ? `${styles.dialog} ${className}` 
    : styles.dialog;

  return (
    <div 
      className={styles.overlay}
      onClick={handleBackdropClick}
    >
      <div 
        ref={dialogRef}
        className={dialogClassName}
        onClick={(e) => e.stopPropagation()}
      >
        <div className={styles.content}>
          {children}
        </div>
        
        {showButtons && (
          <IonFooter className={styles.footer}>
            <IonToolbar>
              <div className={styles.buttonContainer}>
                <IonButton fill="clear" onClick={handleCancelClick}>
                  {cancelLabel}
                </IonButton>
                <IonButton 
                  color={danger ? 'danger' : 'primary'} 
                  onClick={handleConfirmClick}
                >
                  {confirmLabel}
                </IonButton>
              </div>
            </IonToolbar>
          </IonFooter>
        )}
      </div>
    </div>
  );
};