deleting questions and answers, selecting answers
This commit is contained in:
parent
5b2803e67a
commit
8c1bd2e3b8
10 changed files with 174 additions and 47 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import React, {useState} from "react";
|
||||
import React, { useState } from "react";
|
||||
import QuestionItem from "../QuestionItem/QuestionItem.tsx";
|
||||
import AddQuestionButton from "../AddQuestionButton/AddQuestionButton.tsx";
|
||||
|
||||
|
|
@ -6,12 +6,12 @@ interface QuestionsListProps {}
|
|||
|
||||
interface Question {
|
||||
id: number;
|
||||
text: string
|
||||
text: string;
|
||||
}
|
||||
|
||||
const QuestionsList: React.FC<QuestionsListProps> = () => {
|
||||
const [questions, setQuestions] = useState<Question[]>([
|
||||
{id: 1, text: ''},
|
||||
{ id: 1, text: '' },
|
||||
]);
|
||||
|
||||
const handleAddQuestion = () => {
|
||||
|
|
@ -23,21 +23,27 @@ const QuestionsList: React.FC<QuestionsListProps> = () => {
|
|||
setQuestions([...questions, newQuestion]);
|
||||
};
|
||||
|
||||
const handleQuestionChange = (index: number, value: string) => {
|
||||
const newQuestions = [...questions];
|
||||
newQuestions[index] = {...newQuestions[index], text: value};
|
||||
const handleQuestionChange = (id: number, value: string) => {
|
||||
const newQuestions = questions.map((question) =>
|
||||
question.id === id ? { ...question, text: value } : question
|
||||
);
|
||||
setQuestions(newQuestions);
|
||||
};
|
||||
|
||||
const handleDeleteQuestion = (id: number) => {
|
||||
const newQuestions = questions.filter((question) => question.id !== id);
|
||||
setQuestions(newQuestions);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{questions.map((question, index) => (
|
||||
{questions.map((question) => (
|
||||
<QuestionItem
|
||||
key={question.id}
|
||||
indexQuestion={index + 1}
|
||||
valueQuestion={''}
|
||||
onChangeQuestion={(value) => handleQuestionChange(index, value)}
|
||||
indexQuestion={question.id}
|
||||
valueQuestion={question.text}
|
||||
onDeleteQuestion={() => handleDeleteQuestion(question.id)}
|
||||
onChangeQuestion={(value) => handleQuestionChange(question.id, value)}
|
||||
/>
|
||||
))}
|
||||
<AddQuestionButton onClick={handleAddQuestion} />
|
||||
|
|
@ -45,4 +51,4 @@ const QuestionsList: React.FC<QuestionsListProps> = () => {
|
|||
);
|
||||
};
|
||||
|
||||
export default QuestionsList;
|
||||
export default QuestionsList;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue