import { useState, useEffect, useRef } from 'react' import type { InputButtonsQuestion, TestButton } from '@/types/questions' import { QuestionType } from '@/types/questions' import { Label } from '@/components/ui/label' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Button } from '@/components/ui/button' import { ImageUpload } from '@/components/ui/image-upload' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Plus, Trash2 } from 'lucide-react' import { Card, CardContent } from '@/components/ui/card' interface InputButtonsQuestionFormProps { question: Partial onChange: (question: Partial) => void } export function InputButtonsQuestionForm({ question, onChange, }: InputButtonsQuestionFormProps) { const [word, setWord] = useState(question.word || '') const [text, setText] = useState(question.text || '') const [image, setImage] = useState(question.image || '') const [audio, setAudio] = useState(question.audio || '') const [buttons, setButtons] = useState(question.options || []) const [answer, setAnswer] = useState(question.answer || '') const [template, setTemplate] = useState(question.template || '') const onChangeRef = useRef(onChange) // Keep onChange ref up to date useEffect(() => { onChangeRef.current = onChange }, [onChange]) // Notify parent of changes useEffect(() => { onChangeRef.current({ questionType: QuestionType.INPUT_BUTTONS, word, text: text || undefined, image: image || undefined, audio: audio || undefined, options: buttons, answer, template, }) }, [word, text, image, audio, buttons, answer, template]) const addButton = () => { const newButton: TestButton = { id: `btn_${Date.now()}`, text: '', } setButtons([...buttons, newButton]) } const removeButton = (index: number) => { setButtons(buttons.filter((_, i) => i !== index)) if (answer === buttons[index]?.id) { setAnswer('') } } const updateButton = (index: number, updates: Partial) => { const newButtons = [...buttons] newButtons[index] = { ...newButtons[index], ...updates } setButtons(newButtons) } // Генерация template из answer const generateTemplate = () => { if (answer) { setTemplate(answer.replace(/[^ ]/g, '_')) } } return (
setWord(e.target.value)} placeholder="Word or phrase to learn" required />