2023-12-19 11:23:09 +00:00
|
|
|
|
import { useEffect, useState } from "react";
|
2023-09-15 12:37:12 +00:00
|
|
|
|
import { Box, Typography, useMediaQuery, useTheme } from "@mui/material";
|
2023-03-31 15:48:49 +00:00
|
|
|
|
import ButtonsOptions from "../ButtonsOptions";
|
2023-09-12 14:36:22 +00:00
|
|
|
|
import CustomNumberField from "@ui_kit/CustomNumberField";
|
2023-03-31 15:48:49 +00:00
|
|
|
|
import SwitchSlider from "./switchSlider";
|
2023-10-03 14:03:57 +00:00
|
|
|
|
import type { QuizQuestionNumber } from "../../../model/questionTypes/number";
|
2023-11-27 23:07:24 +00:00
|
|
|
|
import { updateQuestion } from "@root/questions/actions";
|
2023-11-16 16:41:25 +00:00
|
|
|
|
|
2023-08-24 11:09:47 +00:00
|
|
|
|
interface Props {
|
2023-12-15 20:05:16 +00:00
|
|
|
|
question: QuizQuestionNumber;
|
2023-06-30 14:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 16:41:25 +00:00
|
|
|
|
export default function SliderOptions({ question }: Props) {
|
2023-12-15 20:05:16 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const isTablet = useMediaQuery(theme.breakpoints.down(980));
|
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(790));
|
|
|
|
|
const [switchState, setSwitchState] = useState("setting");
|
|
|
|
|
const [stepError, setStepError] = useState("");
|
2023-12-19 11:23:09 +00:00
|
|
|
|
const [startError, setStartError] = useState<boolean>(false);
|
2023-12-19 12:31:01 +00:00
|
|
|
|
const [minError, setMinError] = useState<boolean>(false);
|
|
|
|
|
const [maxError, setMaxError] = useState<boolean>(false);
|
2023-12-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const min = Number(question.content.range.split("—")[0]);
|
|
|
|
|
const max = Number(question.content.range.split("—")[1]);
|
|
|
|
|
const start = Number(question.content.start);
|
|
|
|
|
|
|
|
|
|
if (start < min || start > max) {
|
|
|
|
|
setStartError(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (start >= min && start <= max) {
|
|
|
|
|
setStartError(false);
|
|
|
|
|
}
|
|
|
|
|
}, [question.content.range, question.content.start]);
|
2023-12-15 20:05:16 +00:00
|
|
|
|
|
|
|
|
|
const SSHC = (data: string) => {
|
|
|
|
|
setSwitchState(data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
width: isTablet ? "auto" : "100%",
|
|
|
|
|
maxWidth: "720.8px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
pl: "20px",
|
|
|
|
|
pr: isMobile ? "13px" : "20px",
|
|
|
|
|
pb: isMobile ? "30px" : "20px",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: isMobile ? "25px" : "20px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
gap: isMobile ? "10px" : "14px",
|
|
|
|
|
mt: isMobile ? "25px" : "0px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
marginRight: isMobile ? "10px" : "0px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-12-19 11:23:09 +00:00
|
|
|
|
<Typography
|
|
|
|
|
sx={{ fontWeight: "500", fontSize: "18px", color: "#4D4D4D" }}
|
|
|
|
|
>
|
2023-12-15 20:05:16 +00:00
|
|
|
|
Выбор значения из диапазона
|
|
|
|
|
</Typography>
|
2023-12-19 11:23:09 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
gap: isMobile ? "9px" : "20px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-12-15 20:05:16 +00:00
|
|
|
|
<CustomNumberField
|
|
|
|
|
sx={{ maxWidth: "310px", width: "100%" }}
|
|
|
|
|
placeholder={"0"}
|
|
|
|
|
min={0}
|
|
|
|
|
max={99999999999}
|
|
|
|
|
value={question.content.range.split("—")[0]}
|
2023-12-19 12:31:01 +00:00
|
|
|
|
emptyError={minError}
|
2023-12-15 20:05:16 +00:00
|
|
|
|
onChange={({ target }) => {
|
2023-12-19 12:31:01 +00:00
|
|
|
|
const min = Number(target.value);
|
|
|
|
|
const max = Number(question.content.range.split("—")[1]);
|
|
|
|
|
|
2023-12-15 20:05:16 +00:00
|
|
|
|
updateQuestion(question.id, (question) => {
|
|
|
|
|
if (question.type !== "number") return;
|
|
|
|
|
|
2023-12-19 11:23:09 +00:00
|
|
|
|
question.content.range = `${target.value}—${
|
|
|
|
|
question.content.range.split("—")[1]
|
|
|
|
|
}`;
|
2023-12-15 20:05:16 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (min >= max) {
|
2023-12-19 12:31:01 +00:00
|
|
|
|
setMinError(true);
|
|
|
|
|
} else {
|
|
|
|
|
setMinError(false);
|
|
|
|
|
setMaxError(false);
|
2023-12-15 20:05:16 +00:00
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<Typography>—</Typography>
|
|
|
|
|
<CustomNumberField
|
|
|
|
|
sx={{ maxWidth: "310px", width: "100%" }}
|
|
|
|
|
placeholder={"100"}
|
|
|
|
|
min={0}
|
|
|
|
|
max={100000000000}
|
|
|
|
|
value={question.content.range.split("—")[1]}
|
2023-12-19 12:31:01 +00:00
|
|
|
|
emptyError={maxError}
|
2023-12-15 20:05:16 +00:00
|
|
|
|
onChange={({ target }) => {
|
2023-12-19 12:31:01 +00:00
|
|
|
|
const min = Number(question.content.range.split("—")[0]);
|
|
|
|
|
const max = Number(target.value);
|
|
|
|
|
|
2023-12-15 20:05:16 +00:00
|
|
|
|
updateQuestion(question.id, (question) => {
|
|
|
|
|
if (question.type !== "number") return;
|
|
|
|
|
|
2023-12-19 11:23:09 +00:00
|
|
|
|
question.content.range = `${
|
|
|
|
|
question.content.range.split("—")[0]
|
|
|
|
|
}—${target.value}`;
|
2023-12-15 20:05:16 +00:00
|
|
|
|
});
|
2023-12-19 12:31:01 +00:00
|
|
|
|
|
|
|
|
|
if (max <= min) {
|
|
|
|
|
setMaxError(true);
|
|
|
|
|
} else {
|
|
|
|
|
setMaxError(false);
|
|
|
|
|
setMinError(false);
|
|
|
|
|
}
|
2023-12-15 20:05:16 +00:00
|
|
|
|
}}
|
|
|
|
|
onBlur={({ target }) => {
|
|
|
|
|
const step = question.content.step;
|
|
|
|
|
const min = Number(question.content.range.split("—")[0]);
|
|
|
|
|
const max = Number(target.value);
|
|
|
|
|
const range = max - min;
|
|
|
|
|
|
|
|
|
|
if (step > max) {
|
|
|
|
|
updateQuestion(question.id, (question) => {
|
|
|
|
|
if (question.type !== "number") return;
|
|
|
|
|
|
|
|
|
|
question.content.step = min;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (range % step) {
|
2023-12-19 11:23:09 +00:00
|
|
|
|
setStepError(
|
|
|
|
|
`Шаг должен делить без остатка диапазон ${max} - ${min} = ${
|
|
|
|
|
max - min
|
|
|
|
|
}`
|
|
|
|
|
);
|
2023-12-15 20:05:16 +00:00
|
|
|
|
} else {
|
|
|
|
|
setStepError("");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
flexDirection: isMobile ? "column-reverse" : "",
|
|
|
|
|
gap: isMobile ? "15px" : "50px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box sx={{ width: "100%" }}>
|
2023-12-19 11:23:09 +00:00
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
fontWeight: "500",
|
|
|
|
|
fontSize: "18px",
|
|
|
|
|
color: "#4D4D4D",
|
|
|
|
|
mb: isMobile ? "10px" : "14px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-12-15 20:05:16 +00:00
|
|
|
|
Начальное значение
|
|
|
|
|
</Typography>
|
|
|
|
|
<CustomNumberField
|
|
|
|
|
sx={{ maxWidth: "310px", width: "100%" }}
|
|
|
|
|
placeholder={"50"}
|
|
|
|
|
value={String(question.content.start)}
|
2023-12-19 11:23:09 +00:00
|
|
|
|
emptyError={startError}
|
2023-12-15 20:05:16 +00:00
|
|
|
|
onChange={({ target }) => {
|
|
|
|
|
updateQuestion(question.id, (question) => {
|
|
|
|
|
if (question.type !== "number") return;
|
|
|
|
|
|
|
|
|
|
question.content.start = Number(target.value);
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box sx={{ width: "100%" }}>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
fontWeight: "500",
|
|
|
|
|
fontSize: "18px",
|
|
|
|
|
color: "#4D4D4D",
|
|
|
|
|
mb: "10px",
|
|
|
|
|
}}
|
2023-09-22 07:26:07 +00:00
|
|
|
|
>
|
2023-12-15 20:05:16 +00:00
|
|
|
|
Шаг
|
|
|
|
|
</Typography>
|
|
|
|
|
<CustomNumberField
|
|
|
|
|
sx={{ maxWidth: "310px", width: "100%" }}
|
|
|
|
|
min={0}
|
|
|
|
|
max={100}
|
|
|
|
|
placeholder={"1"}
|
|
|
|
|
error={stepError}
|
|
|
|
|
value={String(question.content.step)}
|
|
|
|
|
onChange={({ target }) => {
|
|
|
|
|
updateQuestion(question.id, (question) => {
|
|
|
|
|
if (question.type !== "number") return;
|
|
|
|
|
|
|
|
|
|
question.content.step = Number(target.value);
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
onBlur={({ target }) => {
|
|
|
|
|
const min = Number(question.content.range.split("—")[0]);
|
|
|
|
|
const max = Number(question.content.range.split("—")[1]);
|
|
|
|
|
const range = max - min;
|
|
|
|
|
const step = Number(target.value);
|
|
|
|
|
|
|
|
|
|
if (step > max) {
|
|
|
|
|
updateQuestion(question.id, (question) => {
|
|
|
|
|
if (question.type !== "number") return;
|
|
|
|
|
|
|
|
|
|
question.content.step = max;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (range % step) {
|
2023-12-19 11:23:09 +00:00
|
|
|
|
setStepError(
|
|
|
|
|
`Шаг должен делить без остатка диапазон ${max} - ${min} = ${
|
|
|
|
|
max - min
|
|
|
|
|
}`
|
|
|
|
|
);
|
2023-12-15 20:05:16 +00:00
|
|
|
|
} else {
|
|
|
|
|
setStepError("");
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
2023-12-19 11:23:09 +00:00
|
|
|
|
<ButtonsOptions
|
|
|
|
|
switchState={switchState}
|
|
|
|
|
SSHC={SSHC}
|
|
|
|
|
question={question}
|
|
|
|
|
/>
|
2023-12-15 20:05:16 +00:00
|
|
|
|
<SwitchSlider switchState={switchState} question={question} />
|
|
|
|
|
</>
|
|
|
|
|
);
|
2023-08-24 11:09:47 +00:00
|
|
|
|
}
|