change QuestionItem
This commit is contained in:
parent
71e9f52e70
commit
b3132da636
1 changed files with 54 additions and 10 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import React, {useState} from "react";
|
import React, {useState, useRef, useEffect} from "react";
|
||||||
import AnswerOption from '../AnswerOption/AnswerOption';
|
import AnswerOption from '../AnswerOption/AnswerOption';
|
||||||
import AddAnswerButton from "../AddAnswerButton/AddAnswerButton.tsx";
|
import AddAnswerButton from "../AddAnswerButton/AddAnswerButton.tsx";
|
||||||
import TypeDropdown from "../TypeDropdown/TypeDropdown.tsx";
|
import TypeDropdown from "../TypeDropdown/TypeDropdown.tsx";
|
||||||
|
|
@ -8,13 +8,18 @@ import styles from './QuestionItem.module.css'
|
||||||
interface QuestionItemProps {
|
interface QuestionItemProps {
|
||||||
indexQuestion: number;
|
indexQuestion: number;
|
||||||
initialTextQuestion?: string;
|
initialTextQuestion?: string;
|
||||||
|
valueQuestion: string;
|
||||||
|
onChangeQuestion: (valueQuestion: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QuestionItem: React.FC<QuestionItemProps> = ({indexQuestion, initialTextQuestion = `Вопрос ${indexQuestion}`}) => {
|
const QuestionItem: React.FC<QuestionItemProps> = ({indexQuestion, initialTextQuestion = `Вопрос ${indexQuestion}`,
|
||||||
|
valueQuestion, onChangeQuestion}) => {
|
||||||
const [selectedType, setSelectedType] = useState<'single' | 'multiply'>('single');
|
const [selectedType, setSelectedType] = useState<'single' | 'multiply'>('single');
|
||||||
|
|
||||||
const [answerOption, setAnswerOption] = useState(['']);
|
const [answerOption, setAnswerOption] = useState(['']);
|
||||||
const [textQuestion, setTextQuestion] = useState(initialTextQuestion);
|
const [textQuestion, setTextQuestion] = useState(initialTextQuestion);
|
||||||
|
const [isEditingQuestion, setIsEditingQuestion] = useState(false);
|
||||||
|
|
||||||
|
const textareaQuestionRef = useRef<HTMLTextAreaElement>(null);
|
||||||
|
|
||||||
const handleTypeChange = (type: 'single' | 'multiply') => {
|
const handleTypeChange = (type: 'single' | 'multiply') => {
|
||||||
setSelectedType(type);
|
setSelectedType(type);
|
||||||
|
|
@ -24,25 +29,64 @@ const QuestionItem: React.FC<QuestionItemProps> = ({indexQuestion, initialTextQu
|
||||||
setAnswerOption([...answerOption, '']);
|
setAnswerOption([...answerOption, '']);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleQuestionClick = () => {
|
||||||
|
setIsEditingQuestion(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleTextareaQuestionChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||||
|
setTextQuestion(event.target.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleSaveQuestion = () => {
|
||||||
|
setIsEditingQuestion(false);
|
||||||
|
onChangeQuestion(textQuestion);
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleQuestionKeyDown = (keyDownEvent: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||||
|
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 handleAnswerChange = (index: number, value: string) => {
|
||||||
const newAnswerOption = [...answerOption];
|
const newAnswerOption = [...answerOption];
|
||||||
newAnswerOption[index] = value;
|
newAnswerOption[index] = value;
|
||||||
setAnswerOption(newAnswerOption);
|
setAnswerOption(newAnswerOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleQuestionChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
useEffect(() => {
|
||||||
setTextQuestion(event.target.value);
|
setTextQuestion(valueQuestion);
|
||||||
};
|
}, [valueQuestion]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.questionCard}>
|
<div className={styles.questionCard}>
|
||||||
<div className={styles.questionContainer}>
|
<div className={styles.questionContainer}>
|
||||||
<h2>
|
{isEditingQuestion ? (
|
||||||
<textarea
|
<textarea
|
||||||
value={textQuestion}
|
className={styles.questionTextarea}
|
||||||
onChange={handleQuestionChange}
|
ref={textareaQuestionRef}
|
||||||
|
value={textQuestion === initialTextQuestion ? '' : textQuestion}
|
||||||
|
onChange={handleTextareaQuestionChange}
|
||||||
|
onKeyDown={handleQuestionKeyDown}
|
||||||
|
onBlur={handleQuestionBlur}
|
||||||
|
placeholder={initialTextQuestion}
|
||||||
/>
|
/>
|
||||||
</h2>
|
) : (
|
||||||
|
<button className={styles.textQuestion} onClick={handleQuestionClick}>
|
||||||
|
<h2>{textQuestion || initialTextQuestion}</h2>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
{answerOption.map((answerText, index) => (
|
{answerOption.map((answerText, index) => (
|
||||||
<AnswerOption
|
<AnswerOption
|
||||||
key={index}
|
key={index}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue