Some checks failed
CI Check / check (push) Has been cancelled
- 替换所有浏览器原生弹窗(confirm/alert/prompt) - 创建 ConfirmDialog、AlertDialog、PromptDialog 组件 - 创建 DialogProvider 和 useDialog Hook - 替换所有 admin 组件、首页、分享页面中的原生弹窗 - 素材预览面板统一 - 创建 MaterialPreviewModal 组件 - 使用统一弹窗组件,统一退出逻辑 - 首页UI统一到三层架构 - 迁移主要按钮和输入框样式 - 迁移筛选弹窗中的所有按钮样式 - 使用 useThemeStyles() Hook - 更新文档 - 更新优化记录、优化计划、后续修改、更新日志
102 lines
2.3 KiB
TypeScript
102 lines
2.3 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect, useRef } from 'react'
|
|
import Modal from './Modal'
|
|
import { useThemeStyles } 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<HTMLInputElement>(null)
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
setValue(defaultValue)
|
|
// 延迟聚焦,确保弹窗已渲染
|
|
setTimeout(() => {
|
|
inputRef.current?.focus()
|
|
inputRef.current?.select()
|
|
}, 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 (
|
|
<Modal
|
|
isOpen={isOpen}
|
|
onClose={onClose}
|
|
title={title}
|
|
size="small"
|
|
footer={
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className={`${styles.buttonStyles.secondary} ${styles.buttonModifiers.compact}`}
|
|
>
|
|
{cancelText}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={handleConfirm}
|
|
disabled={!value.trim()}
|
|
className={`${styles.buttonStyles.primary} ${styles.buttonModifiers.compact}`}
|
|
>
|
|
{confirmText}
|
|
</button>
|
|
</>
|
|
}
|
|
>
|
|
<div className="py-2 space-y-3">
|
|
<p className="text-slate-700 dark:text-slate-200 whitespace-pre-line">{message}</p>
|
|
<input
|
|
ref={inputRef}
|
|
type="text"
|
|
value={value}
|
|
onChange={(e) => setValue(e.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
placeholder={placeholder}
|
|
className={styles.inputStyles.base}
|
|
/>
|
|
</div>
|
|
</Modal>
|
|
)
|
|
}
|
|
|