import { useState } from 'react' import { Link } from 'wouter' interface CartItem { id: number name: string price: number quantity: number image: string } interface WizardStep { id: number title: string description: string completed: boolean } export default function ComplexTestPage() { const [currentStep, setCurrentStep] = useState(1) const [cartItems, setCartItems] = useState([ { id: 1, name: 'iPhone 15 Pro', price: 7999, quantity: 1, image: 'https://picsum.photos/100/100?random=1' }, { id: 2, name: 'MacBook Air', price: 8999, quantity: 1, image: 'https://picsum.photos/100/100?random=2' } ]) const [wizardData, setWizardData] = useState({ personalInfo: { name: '', email: '', phone: '' }, address: { street: '', city: '', zipCode: '' }, payment: { cardNumber: '', expiryDate: '', cvv: '' } }) const [wizardSteps, setWizardSteps] = useState([ { id: 1, title: '个人信息', description: '填写基本信息', completed: false }, { id: 2, title: '收货地址', description: '填写收货地址', completed: false }, { id: 3, title: '支付方式', description: '选择支付方式', completed: false }, { id: 4, title: '确认订单', description: '确认订单信息', completed: false } ]) const [showConfirmDialog, setShowConfirmDialog] = useState(false) const [isProcessing, setIsProcessing] = useState(false) const [orderComplete, setOrderComplete] = useState(false) // 购物车操作 const updateQuantity = (id: number, newQuantity: number) => { if (newQuantity <= 0) { removeItem(id) return } setCartItems(prev => prev.map(item => item.id === id ? { ...item, quantity: newQuantity } : item ) ) } const removeItem = (id: number) => { setCartItems(prev => prev.filter(item => item.id !== id)) } const addItem = () => { const newItem: CartItem = { id: Date.now(), name: `新产品 ${cartItems.length + 1}`, price: Math.floor(Math.random() * 5000) + 1000, quantity: 1, image: `https://picsum.photos/100/100?random=${Date.now()}` } setCartItems(prev => [...prev, newItem]) } const getTotalPrice = () => { return cartItems.reduce((total, item) => total + item.price * item.quantity, 0) } // 向导步骤验证 const validateStep = (step: number): boolean => { switch (step) { case 1: return !!(wizardData.personalInfo.name && wizardData.personalInfo.email && wizardData.personalInfo.phone) case 2: return !!(wizardData.address.street && wizardData.address.city && wizardData.address.zipCode) case 3: return !!(wizardData.payment.cardNumber && wizardData.payment.expiryDate && wizardData.payment.cvv) default: return true } } const goToStep = (step: number) => { // 验证当前步骤 if (step > currentStep && !validateStep(currentStep)) { alert('请完成当前步骤的必填信息') return } // 更新步骤完成状态 if (step > currentStep) { setWizardSteps(prev => prev.map(s => s.id === currentStep ? { ...s, completed: true } : s ) ) } setCurrentStep(step) } const handleInputChange = (section: string, field: string, value: string) => { setWizardData(prev => ({ ...prev, [section]: { ...prev[section as keyof typeof prev], [field]: value } })) } const handleSubmitOrder = async () => { setIsProcessing(true) // 模拟处理时间 await new Promise(resolve => setTimeout(resolve, 3000)) // 模拟随机失败 if (Math.random() < 0.2) { setIsProcessing(false) alert('订单提交失败,请重试') return } setIsProcessing(false) setOrderComplete(true) setShowConfirmDialog(false) } const resetWizard = () => { setCurrentStep(1) setWizardData({ personalInfo: { name: '', email: '', phone: '' }, address: { street: '', city: '', zipCode: '' }, payment: { cardNumber: '', expiryDate: '', cvv: '' } }) setWizardSteps(prev => prev.map(s => ({ ...s, completed: false }))) setOrderComplete(false) setShowConfirmDialog(false) } if (orderComplete) { return (
🎉

订单提交成功!

您的订单已成功提交,我们将尽快为您处理。

返回测试页面
) } return (

复杂交互测试

测试多步骤操作、状态管理和复杂用户交互

{/* 购物车区域 */}

购物车 ({cartItems.length})

{cartItems.map(item => (
{item.name}

{item.name}

¥{item.price.toLocaleString()}

{item.quantity}
))}
总计: ¥{getTotalPrice().toLocaleString()}
{/* 向导区域 */}
{/* 步骤指示器 */}
{wizardSteps.map((step, index) => (
{index < wizardSteps.length - 1 && (
)}
))}

{wizardSteps[currentStep - 1].title}

{wizardSteps[currentStep - 1].description}

{/* 步骤内容 */}
{currentStep === 1 && (
handleInputChange('personalInfo', 'name', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="请输入您的姓名" />
handleInputChange('personalInfo', 'email', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="请输入您的邮箱" />
handleInputChange('personalInfo', 'phone', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="请输入您的手机号" />
)} {currentStep === 2 && (
handleInputChange('address', 'street', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="请输入详细地址" />
handleInputChange('address', 'city', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="请输入城市" />
handleInputChange('address', 'zipCode', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="请输入邮政编码" />
)} {currentStep === 3 && (
handleInputChange('payment', 'cardNumber', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="请输入银行卡号" />
handleInputChange('payment', 'expiryDate', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="MM/YY" />
handleInputChange('payment', 'cvv', e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white" placeholder="CVV" />
)} {currentStep === 4 && (

订单确认

个人信息

{wizardData.personalInfo.name} | {wizardData.personalInfo.email} | {wizardData.personalInfo.phone}

收货地址

{wizardData.address.street}, {wizardData.address.city} {wizardData.address.zipCode}

支付方式

**** **** **** {wizardData.payment.cardNumber.slice(-4)}

)}
{/* 导航按钮 */}
{currentStep < 4 ? ( ) : ( )}
{/* 确认对话框 */} {showConfirmDialog && (

确认提交订单

您确定要提交这个订单吗?订单总金额为 ¥{getTotalPrice().toLocaleString()}

)} {/* 返回链接 */}
← 返回测试页面列表
) }