fix question type

This commit is contained in:
Tatiana Nikolaeva 2025-05-25 22:46:52 +05:00
parent 6eb03dc112
commit 8182bc1c43
7 changed files with 46 additions and 34 deletions

View file

@ -19,35 +19,41 @@ interface QuestionItemProps {
onChangeQuestion: (valueQuestion: string) => void;
onAnswerVariantsChange: (variants: {id?: number, text: string}[]) => void;
onDeleteQuestion: (index: number) => Promise<void>;
selectedType: 'single' | 'multiply';
setSelectedType: (type: 'single' | 'multiply') => void;
initialQuestionType: 'SingleAnswerQuestion' | 'MultipleAnswerQuestion';
onQuestionTypeChange: (type: 'SingleAnswerQuestion' | 'MultipleAnswerQuestion') => void;
surveyId?: number;
}
const QuestionItem: React.FC<QuestionItemProps> = ({
questionId,
// initialTextQuestion = `Вопрос ${questionId}`,
initialTextQuestion = `Вопрос`,
valueQuestion,
answerVariants: initialAnswerVariants,
onChangeQuestion,
onAnswerVariantsChange,
onDeleteQuestion,
setSelectedType,
selectedType,
initialQuestionType,
onQuestionTypeChange,
surveyId
}) => {
const [textQuestion, setTextQuestion] = useState(initialTextQuestion);
const [isEditingQuestion, setIsEditingQuestion] = useState(false);
const [selectedAnswers, setSelectedAnswers] = useState<number[]>([]);
const [questionType, setQuestionType] = useState<'SingleAnswerQuestion' | 'MultipleAnswerQuestion'>(initialQuestionType);
const textareaQuestionRef = useRef<HTMLTextAreaElement>(null);
useEffect(() => {
setTextQuestion(valueQuestion);
}, [valueQuestion]);
const handleTypeChange = (type: 'single' | 'multiply') => {
setSelectedType(type);
useEffect(() => {
setQuestionType(initialQuestionType);
}, [initialQuestionType]);
const handleTypeChange = (type: 'SingleAnswerQuestion' | 'MultipleAnswerQuestion') => {
setQuestionType(type);
onQuestionTypeChange(type);
};
const handleAddAnswer = async () => {
@ -156,7 +162,7 @@ const QuestionItem: React.FC<QuestionItemProps> = ({
};
const toggleSelect = (index: number) => {
if (selectedType === 'single') {
if (questionType === 'SingleAnswerQuestion') {
setSelectedAnswers([index]);
} else {
setSelectedAnswers((prev) => {
@ -189,13 +195,13 @@ const QuestionItem: React.FC<QuestionItemProps> = ({
<h2 className={styles.textQuestion}>{textQuestion || initialTextQuestion}</h2>
</button>
)}
<TypeDropdown selectedType={selectedType} onTypeChange={handleTypeChange}/>
<TypeDropdown selectedType={questionType} onTypeChange={handleTypeChange}/>
</div>
{initialAnswerVariants.map((answer, index) => (
<AnswerOption
key={answer.id || index}
selectedType={selectedType}
selectedType={questionType}
index={index + 1}
value={answer.text}
onChange={(value) => handleAnswerChange(index, value)}