frontPanel/src/pages/Questions/AnswerDraggableList/AnswerItem.tsx

182 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-09-25 13:43:15 +00:00
import { PointsIcon } from "@icons/questionsPage/PointsIcon";
import { DeleteIcon } from "@icons/questionsPage/deleteIcon";
2023-11-15 18:38:02 +00:00
import {
2023-12-19 23:08:33 +00:00
Box,
FormControl,
IconButton,
InputAdornment,
TextField as MuiTextField,
TextFieldProps,
2023-12-19 23:08:33 +00:00
useMediaQuery,
useTheme,
2023-11-15 18:38:02 +00:00
} from "@mui/material";
2023-12-29 10:17:43 +00:00
import {
addQuestionVariant,
deleteQuestionVariant,
setQuestionVariantField,
} from "@root/questions/actions";
import { enqueueSnackbar } from "notistack";
import type { ChangeEvent, FC, KeyboardEvent, ReactNode } from "react";
2023-11-15 18:38:02 +00:00
import { Draggable } from "react-beautiful-dnd";
2023-11-28 19:16:00 +00:00
import type { QuestionVariant } from "../../../model/questionTypes/shared";
2023-11-15 18:38:02 +00:00
const TextField = MuiTextField as unknown as FC<TextFieldProps>;
2023-08-18 11:16:56 +00:00
type AnswerItemProps = {
2023-12-19 23:08:33 +00:00
index: number;
questionId: string;
variant: QuestionVariant;
largeCheck: boolean;
disableKeyDown?: boolean;
additionalContent?: ReactNode;
additionalMobile?: ReactNode;
2023-08-18 11:16:56 +00:00
};
2023-09-18 09:07:13 +00:00
export const AnswerItem = ({
2023-12-19 23:08:33 +00:00
index,
variant,
questionId,
largeCheck,
additionalContent,
additionalMobile,
disableKeyDown,
2023-09-18 09:07:13 +00:00
}: AnswerItemProps) => {
2023-12-19 23:08:33 +00:00
const theme = useTheme();
const isTablet = useMediaQuery(theme.breakpoints.down(790));
2023-08-18 11:16:56 +00:00
2023-12-19 23:08:33 +00:00
return (
<Draggable draggableId={String(index)} index={index}>
{(provided) => (
<Box ref={provided.innerRef} {...provided.draggableProps}>
<FormControl
key={index}
fullWidth
variant="standard"
sx={{
margin: isTablet ? " 15px 0 20px 0" : "0 0 15px 0",
borderRadius: "10px",
border: "1px solid rgba(0, 0, 0, 0.23)",
background: "white",
}}
>
<TextField
value={variant.answer}
2023-12-19 23:08:33 +00:00
fullWidth
focused={false}
placeholder={"Добавьте ответ"}
multiline={largeCheck}
2023-12-29 10:17:43 +00:00
onChange={({ target }: ChangeEvent<HTMLInputElement>) => {
if (target.value.length <= 1000) {
setQuestionVariantField(
questionId,
variant.id,
"answer",
target.value || " ",
);
}
2023-12-19 23:08:33 +00:00
}}
onKeyDown={(event: KeyboardEvent<HTMLInputElement>) => {
2023-12-29 10:17:43 +00:00
if (disableKeyDown) {
enqueueSnackbar("100 максимальное количество вопросов");
} else if (event.code === "Enter" && !largeCheck) {
2023-12-19 23:08:33 +00:00
addQuestionVariant(questionId);
2023-12-29 10:17:43 +00:00
}
2023-12-19 23:08:33 +00:00
}}
InputProps={{
startAdornment: (
<>
2023-12-29 10:17:43 +00:00
<InputAdornment
{...provided.dragHandleProps}
position="start"
>
<PointsIcon
style={{ color: "#9A9AAF", fontSize: "30px" }}
/>
2023-12-19 23:08:33 +00:00
</InputAdornment>
{additionalContent}
</>
),
endAdornment: (
<InputAdornment position="end">
{/*<IconButton*/}
{/* sx={{ padding: "0" }}*/}
{/* aria-describedby="my-popover-id"*/}
{/* onClick={handleClick}*/}
{/*>*/}
{/* <MessageIcon*/}
{/* style={{*/}
{/* color: "#9A9AAF",*/}
{/* fontSize: "30px",*/}
{/* marginRight: "6.5px",*/}
{/* }}*/}
{/* />*/}
{/*</IconButton>*/}
{/*<Popover*/}
{/* id="my-popover-id"*/}
{/* open={isOpen}*/}
{/* anchorEl={anchorEl}*/}
{/* onClose={handleClose}*/}
{/* anchorOrigin={{ vertical: "bottom", horizontal: "left" }}*/}
{/*>*/}
{/* <TextareaAutosize*/}
{/* style={{ margin: "10px" }}*/}
{/* placeholder="Подсказка для этого ответа"*/}
{/* value={variant.hints}*/}
{/* onChange={(e) =>*/}
{/* setQuestionVariantAnswer(e.target.value || " ")*/}
{/* }*/}
{/* onKeyDown={(*/}
{/* event: KeyboardEvent<HTMLTextAreaElement>,*/}
{/* ) => event.stopPropagation()}*/}
{/* />*/}
{/*</Popover>*/}
2023-12-29 10:17:43 +00:00
<IconButton
sx={{ padding: "0" }}
onClick={() =>
deleteQuestionVariant(questionId, variant.id)
}
>
2023-12-19 23:08:33 +00:00
<DeleteIcon
style={{
color: theme.palette.grey2.main,
marginRight: "-1px",
}}
/>
</IconButton>
</InputAdornment>
),
}}
sx={{
"& .MuiInputBase-root": {
padding: additionalContent ? "5px 13px" : "13px",
borderRadius: "10px",
background: "#ffffff",
"& input.MuiInputBase-input": {
height: "22px",
},
"& textarea.MuiInputBase-input": {
marginTop: "1px",
},
"& .MuiOutlinedInput-notchedOutline": {
border: "none",
},
},
}}
inputProps={{
2024-02-15 12:45:14 +00:00
sx: {
fontSize: "18px",
lineHeight: "21px",
py: 0,
ml: "13px",
},
2023-12-19 23:08:33 +00:00
"data-cy": "quiz-variant-question-answer",
}}
/>
{additionalMobile}
</FormControl>
</Box>
)}
</Draggable>
);
2023-08-18 11:16:56 +00:00
};