import React, { useState } from 'react'; import { PromptGroup } from '../../types'; import Input from '../common/Input'; import TextArea from '../common/TextArea'; import Button from '../common/Button'; import CategorySelector from '../Category/CategorySelector'; import { useApp } from '../../contexts/AppContext'; interface PromptGroupFormProps { initialPromptGroup?: Partial; onSubmit: (promptGroup: { name: string; description: string; category: string }) => void; onCancel: () => void; } const PromptGroupForm: React.FC = ({ initialPromptGroup = {}, onSubmit, onCancel }) => { const { categories } = useApp(); const [name, setName] = useState(initialPromptGroup.name || ''); const [description, setDescription] = useState(initialPromptGroup.description || ''); const [category, setCategory] = useState(initialPromptGroup.category || (categories.length > 0 ? categories[0]._id : '')); const [errors, setErrors] = useState({ name: '', category: '' }); const validate = (): boolean => { const newErrors = { name: '', category: '' }; if (!name.trim()) { newErrors.name = '请输入提示词组名称'; } if (!category) { newErrors.category = '请选择分类'; } setErrors(newErrors); return !newErrors.name && !newErrors.category; }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!validate()) return; // 确保 category 是字符串类型 const categoryId = typeof category === 'object' ? category._id : category; onSubmit({ name: name.trim(), description: description.trim(), category: categoryId }); }; const handleCategoryChange = (categoryId: string) => { setCategory(categoryId); if (errors.category) { setErrors(prev => ({ ...prev, category: '' })); } }; return (
setName(e.target.value)} error={errors.name} required />