import { useState } from "react"; import { useParams } from "react-router-dom"; import { Box, Typography, useMediaQuery, useTheme } from "@mui/material"; import ButtonsOptions from "../ButtonsOptions"; import CustomNumberField from "@ui_kit/CustomNumberField"; import SwitchSlider from "./switchSlider"; import { questionStore, updateQuestionsList } from "@root/questions"; import type { QuizQuestionNumber } from "../../../model/questionTypes/number"; interface Props { totalIndex: number; } export default function SliderOptions({ totalIndex }: Props) { 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(""); const quizId = Number(useParams().quizId); const { listQuestions } = questionStore(); const question = listQuestions[quizId][totalIndex] as QuizQuestionNumber; const SSHC = (data: string) => { setSwitchState(data); }; return ( <> Выбор значения из диапазона { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, range: `${target.value}—${ question.content.range.split("—")[1] }`, }, }); }} onBlur={({ target }) => { const start = question.content.start; const min = Number(target.value); const max = Number(question.content.range.split("—")[1]); if (min >= max) { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, range: `${max - 1 >= 0 ? max - 1 : 0}—${ question.content.range.split("—")[1] }`, }, }); } if (start < min) { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, start: min }, }); } }} /> { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, range: `${question.content.range.split("—")[0]}—${ target.value }`, }, }); }} onBlur={({ target }) => { const start = question.content.start; const step = question.content.step; const min = Number(question.content.range.split("—")[0]); const max = Number(target.value); const range = max - min; if (max <= min) { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, range: `${question.content.range.split("—")[0]}—${ min + 1 >= 100 ? 100 : min + 1 }`, }, }); } if (start > max) { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, start: max }, }); } if (step > max) { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, step: max }, }); if (range % step) { setStepError(`Шаг должен делить без остатка диапазон ${max} - ${min} = ${max - min}`); } else { setStepError(""); } } }} /> Начальное значение { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, start: Number(target.value) }, }); }} /> Шаг { updateQuestionsList(quizId, totalIndex, { content: { ...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) { updateQuestionsList(quizId, totalIndex, { content: { ...question.content, step: max }, }); } if (range % step) { setStepError(`Шаг должен делить без остатка диапазон ${max} - ${min} = ${max - min}`); } else { setStepError(""); } }} /> ); }