import React, {useState, useRef, useEffect} from "react"; import AnswerOption from '../AnswerOption/AnswerOption'; import AddAnswerButton from "../AddAnswerButton/AddAnswerButton.tsx"; import TypeDropdown from "../TypeDropdown/TypeDropdown.tsx"; import styles from './QuestionItem.module.css' interface QuestionItemProps { indexQuestion: number; initialTextQuestion?: string; valueQuestion: string; onChangeQuestion: (valueQuestion: string) => void; } const QuestionItem: React.FC = ({indexQuestion, initialTextQuestion = `Вопрос ${indexQuestion}`, valueQuestion, onChangeQuestion}) => { const [selectedType, setSelectedType] = useState<'single' | 'multiply'>('single'); const [answerOption, setAnswerOption] = useState(['']); const [textQuestion, setTextQuestion] = useState(initialTextQuestion); const [isEditingQuestion, setIsEditingQuestion] = useState(false); const textareaQuestionRef = useRef(null); const handleTypeChange = (type: 'single' | 'multiply') => { setSelectedType(type); } const handleAddAnswer = () => { setAnswerOption([...answerOption, '']); }; const handleQuestionClick = () => { setIsEditingQuestion(true); } const handleTextareaQuestionChange = (event: React.ChangeEvent) => { setTextQuestion(event.target.value); } const handleSaveQuestion = () => { setIsEditingQuestion(false); onChangeQuestion(textQuestion); } const handleQuestionKeyDown = (keyDownEvent: React.KeyboardEvent) => { if (keyDownEvent.key === 'Enter') { keyDownEvent.preventDefault(); handleSaveQuestion() } } const handleQuestionBlur = () => { handleSaveQuestion() } useEffect(() => { if (isEditingQuestion && textareaQuestionRef.current) { textareaQuestionRef.current.focus(); } }, [isEditingQuestion]); const handleAnswerChange = (index: number, value: string) => { const newAnswerOption = [...answerOption]; newAnswerOption[index] = value; setAnswerOption(newAnswerOption); } useEffect(() => { setTextQuestion(valueQuestion); }, [valueQuestion]); return (
{isEditingQuestion ? (