/** * PromptDialog 组件 * * 输入对话框组件 * 基于 Modal 组件构建,用于获取用户输入 * 支持默认值、占位符、键盘导航等 * * @created 2025-11-22 * @component */ 'use client' import { useState, useEffect, useRef } from 'react' import Modal from './Modal' import { useThemeStyles, cn } from '@/lib/ui-styles' interface PromptDialogProps { isOpen: boolean onClose: () => void onConfirm: (value: string) => void title: string message: string defaultValue?: string placeholder?: string confirmText?: string cancelText?: string } export default function PromptDialog({ isOpen, onClose, onConfirm, title, message, defaultValue = '', placeholder = '', confirmText = '确认', cancelText = '取消', }: PromptDialogProps) { const styles = useThemeStyles() const [value, setValue] = useState(defaultValue) const inputRef = useRef(null) useEffect(() => { if (isOpen) { setValue(defaultValue) // 延迟聚焦,确保弹窗已渲染 setTimeout(() => { const input = inputRef.current if (input) { input.focus() const length = input.value.length input.setSelectionRange(length, length) } }, 100) } }, [isOpen, defaultValue]) const handleConfirm = () => { if (value.trim()) { onConfirm(value.trim()) onClose() } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleConfirm() } else if (e.key === 'Escape') { onClose() } } return ( } >

{message}

setValue(e.target.value)} onKeyDown={handleKeyDown} placeholder={placeholder} className={styles.inputStyles.base} />
) }