frontPanel/src/pages/Questions/SliderOptions/SliderOptions.tsx

194 lines
6.7 KiB
TypeScript
Raw Normal View History

2023-09-06 13:20:12 +00:00
import { useParams } from "react-router-dom";
import { Box, Typography } from "@mui/material";
import ButtonsOptions from "../ButtonsOptions";
import React from "react";
2023-09-12 14:36:22 +00:00
import CustomNumberField from "@ui_kit/CustomNumberField";
import SwitchSlider from "./switchSlider";
2023-09-06 08:01:44 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
2023-08-24 11:09:47 +00:00
interface Props {
totalIndex: number;
}
2023-08-24 11:09:47 +00:00
export default function SliderOptions({ totalIndex }: Props) {
const [switchState, setSwitchState] = React.useState("setting");
2023-09-06 13:20:12 +00:00
const quizId = Number(useParams().quizId);
2023-09-06 08:01:44 +00:00
const { listQuestions } = questionStore();
2023-09-06 13:20:12 +00:00
2023-08-24 11:09:47 +00:00
const SSHC = (data: string) => {
setSwitchState(data);
};
2023-08-24 11:09:47 +00:00
return (
<>
<Box
sx={{
width: "100%",
maxWidth: "640px",
display: "flex",
padding: "20px",
flexDirection: "column",
gap: "20px",
}}
>
<Box sx={{ gap: "10px", display: "flex", flexDirection: "column" }}>
<Typography>Выбор значения из диапазона</Typography>
<Box sx={{ display: "flex", alignItems: "center", gap: "20px" }}>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-06 08:01:44 +00:00
placeholder={"0"}
2023-09-12 15:23:56 +00:00
min={0}
max={99}
value={
2023-09-06 13:20:12 +00:00
listQuestions[quizId][totalIndex].content.range.split("—")[0]
}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-06 08:01:44 +00:00
clonContent.range = `${target.value}${
2023-09-06 13:20:12 +00:00
listQuestions[quizId][totalIndex].content.range.split("—")[1]
2023-09-06 08:01:44 +00:00
}`;
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
const min = Number(target.value);
const max = Number(
listQuestions[quizId][totalIndex].content.range.split("—")[1]
);
if (min >= max) {
const clonContent = listQuestions[quizId][totalIndex].content;
clonContent.range = `${max - 1 >= 0 ? max - 1 : 0}${
listQuestions[quizId][totalIndex].content.range.split(
"—"
)[1]
}`;
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
}
}}
2023-09-06 08:01:44 +00:00
/>
2023-08-24 11:09:47 +00:00
<Typography></Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-06 08:01:44 +00:00
placeholder={"100"}
2023-09-12 15:23:56 +00:00
min={0}
max={100}
value={
2023-09-06 13:20:12 +00:00
listQuestions[quizId][totalIndex].content.range.split("—")[1]
}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-06 08:01:44 +00:00
clonContent.range = `${
2023-09-06 13:20:12 +00:00
listQuestions[quizId][totalIndex].content.range.split("—")[0]
2023-09-06 08:01:44 +00:00
}${target.value}`;
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
const min = Number(
listQuestions[quizId][totalIndex].content.range.split("—")[0]
);
const max = Number(target.value);
if (max <= min) {
const clonContent = listQuestions[quizId][totalIndex].content;
clonContent.range = `${
listQuestions[quizId][totalIndex].content.range.split(
"—"
)[0]
}${min + 1 >= 100 ? 100 : min + 1}`;
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
}
}}
2023-09-06 08:01:44 +00:00
/>
2023-08-24 11:09:47 +00:00
</Box>
</Box>
<Box
sx={{
display: "flex",
alignItems: "center",
justifyContent: "space-between",
2023-09-12 14:36:22 +00:00
gap: "50px",
2023-08-24 11:09:47 +00:00
}}
>
2023-09-12 14:36:22 +00:00
<Box sx={{ width: "100%" }}>
2023-08-24 11:09:47 +00:00
<Typography>Начальное значение</Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-06 08:01:44 +00:00
placeholder={"50"}
2023-09-12 15:23:56 +00:00
min={0}
value={String(listQuestions[quizId][totalIndex].content.start)}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-06 08:01:44 +00:00
clonContent.start = Number(target.value);
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
const start = Number(target.value);
const min = Number(
listQuestions[quizId][totalIndex].content.range.split("—")[0]
);
if (start < min) {
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
start: min,
},
});
}
}}
2023-09-06 08:01:44 +00:00
/>
2023-08-24 11:09:47 +00:00
</Box>
2023-09-12 14:36:22 +00:00
<Box sx={{ width: "100%" }}>
2023-08-24 11:09:47 +00:00
<Typography>Шаг</Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-12 15:23:56 +00:00
min={0}
max={100}
2023-09-06 08:01:44 +00:00
placeholder={"1"}
2023-09-12 15:23:56 +00:00
value={String(listQuestions[quizId][totalIndex].content.step)}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-06 08:01:44 +00:00
clonContent.step = Number(target.value);
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
const min = Number(
listQuestions[quizId][totalIndex].content.range.split("—")[0]
);
const max = Number(
listQuestions[quizId][totalIndex].content.range.split("—")[1]
);
const range = max - min;
const step = Number(target.value);
if (step > range) {
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
step: range,
},
});
}
}}
2023-09-06 08:01:44 +00:00
/>
2023-08-24 11:09:47 +00:00
</Box>
</Box>
</Box>
<ButtonsOptions
switchState={switchState}
SSHC={SSHC}
totalIndex={totalIndex}
/>
<SwitchSlider switchState={switchState} totalIndex={totalIndex} />
</>
);
}