feat: SliderOptions logic

This commit is contained in:
IlyaDoronin 2023-09-12 18:23:56 +03:00
parent a4bc0f5c76
commit e9313dc1e6
3 changed files with 83 additions and 14 deletions

@ -36,7 +36,9 @@ export default function SliderOptions({ totalIndex }: Props) {
<Box sx={{ display: "flex", alignItems: "center", gap: "20px" }}> <Box sx={{ display: "flex", alignItems: "center", gap: "20px" }}>
<CustomNumberField <CustomNumberField
placeholder={"0"} placeholder={"0"}
text={ min={0}
max={99}
value={
listQuestions[quizId][totalIndex].content.range.split("—")[0] listQuestions[quizId][totalIndex].content.range.split("—")[0]
} }
onChange={({ target }) => { onChange={({ target }) => {
@ -48,11 +50,31 @@ export default function SliderOptions({ totalIndex }: Props) {
content: clonContent, content: clonContent,
}); });
}} }}
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,
});
}
}}
/> />
<Typography></Typography> <Typography></Typography>
<CustomNumberField <CustomNumberField
placeholder={"100"} placeholder={"100"}
text={ min={0}
max={100}
value={
listQuestions[quizId][totalIndex].content.range.split("—")[1] listQuestions[quizId][totalIndex].content.range.split("—")[1]
} }
onChange={({ target }) => { onChange={({ target }) => {
@ -64,6 +86,24 @@ export default function SliderOptions({ totalIndex }: Props) {
content: clonContent, content: clonContent,
}); });
}} }}
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,
});
}
}}
/> />
</Box> </Box>
</Box> </Box>
@ -79,7 +119,8 @@ export default function SliderOptions({ totalIndex }: Props) {
<Typography>Начальное значение</Typography> <Typography>Начальное значение</Typography>
<CustomNumberField <CustomNumberField
placeholder={"50"} placeholder={"50"}
text={String(listQuestions[quizId][totalIndex].content.start)} min={0}
value={String(listQuestions[quizId][totalIndex].content.start)}
onChange={({ target }) => { onChange={({ target }) => {
const clonContent = listQuestions[quizId][totalIndex].content; const clonContent = listQuestions[quizId][totalIndex].content;
clonContent.start = Number(target.value); clonContent.start = Number(target.value);
@ -87,13 +128,30 @@ export default function SliderOptions({ totalIndex }: Props) {
content: clonContent, content: clonContent,
}); });
}} }}
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,
},
});
}
}}
/> />
</Box> </Box>
<Box sx={{ width: "100%" }}> <Box sx={{ width: "100%" }}>
<Typography>Шаг</Typography> <Typography>Шаг</Typography>
<CustomNumberField <CustomNumberField
min={0}
max={100}
placeholder={"1"} placeholder={"1"}
text={String(listQuestions[quizId][totalIndex].content.step)} value={String(listQuestions[quizId][totalIndex].content.step)}
onChange={({ target }) => { onChange={({ target }) => {
const clonContent = listQuestions[quizId][totalIndex].content; const clonContent = listQuestions[quizId][totalIndex].content;
clonContent.step = Number(target.value); clonContent.step = Number(target.value);
@ -101,6 +159,25 @@ export default function SliderOptions({ totalIndex }: Props) {
content: clonContent, content: clonContent,
}); });
}} }}
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,
},
});
}
}}
/> />
</Box> </Box>
</Box> </Box>

@ -30,7 +30,6 @@ export interface Question {
required: boolean; required: boolean;
deleted: true; deleted: true;
page: number; page: number;
completed: boolean;
content: { content: {
variants: Variants[]; variants: Variants[];
hint: Hint; hint: Hint;
@ -141,7 +140,6 @@ export const createQuestion = (quizId: number) => {
required: true, required: true,
deleted: true, deleted: true,
page: 0, page: 0,
completed: false,
content: { content: {
largeCheck: false, largeCheck: false,
large: "", large: "",

@ -1,4 +1,3 @@
import { useState } from "react";
import CustomTextField from "./CustomTextField"; import CustomTextField from "./CustomTextField";
import type { ChangeEvent, KeyboardEvent, FocusEvent } from "react"; import type { ChangeEvent, KeyboardEvent, FocusEvent } from "react";
@ -9,7 +8,7 @@ interface CustomNumberFieldProps {
onChange?: (event: ChangeEvent<HTMLInputElement>) => void; onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void; onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void;
onBlur?: (event: FocusEvent<HTMLInputElement>) => void; onBlur?: (event: FocusEvent<HTMLInputElement>) => void;
text?: string; value: string;
sx?: SxProps<Theme>; sx?: SxProps<Theme>;
min?: number; min?: number;
max?: number; max?: number;
@ -17,7 +16,7 @@ interface CustomNumberFieldProps {
export default function CustomNumberField({ export default function CustomNumberField({
placeholder, placeholder,
text, value,
sx, sx,
onChange, onChange,
onKeyDown, onKeyDown,
@ -25,8 +24,6 @@ export default function CustomNumberField({
min = -999999999, min = -999999999,
max = 999999999, max = 999999999,
}: CustomNumberFieldProps) { }: CustomNumberFieldProps) {
const [value, setValue] = useState<string>("");
const onInputChange = (event: ChangeEvent<HTMLInputElement>) => { const onInputChange = (event: ChangeEvent<HTMLInputElement>) => {
const inputValue = event.target.value; const inputValue = event.target.value;
@ -37,8 +34,6 @@ export default function CustomNumberField({
inputValue.match(/^\d*$/) || inputValue.match(/^\d*$/) ||
(inputValue[0] === "-" && inputValue.slice(1).match(/^\d*$/))) (inputValue[0] === "-" && inputValue.slice(1).match(/^\d*$/)))
) { ) {
setValue(inputValue);
onChange?.({ ...event, target: { ...event.target, value: inputValue } }); onChange?.({ ...event, target: { ...event.target, value: inputValue } });
} }
}; };
@ -46,7 +41,6 @@ export default function CustomNumberField({
return ( return (
<CustomTextField <CustomTextField
placeholder={placeholder} placeholder={placeholder}
text={text}
sx={sx} sx={sx}
onChange={onInputChange} onChange={onInputChange}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}