feat: CustomNumberField

This commit is contained in:
IlyaDoronin 2023-09-12 17:36:22 +03:00
parent c05bbeace3
commit a4bc0f5c76
4 changed files with 70 additions and 7 deletions

@ -2,7 +2,7 @@ import { useParams } from "react-router-dom";
import { Box, Typography } from "@mui/material"; import { Box, Typography } from "@mui/material";
import ButtonsOptions from "../ButtonsOptions"; import ButtonsOptions from "../ButtonsOptions";
import React from "react"; import React from "react";
import CustomTextField from "@ui_kit/CustomTextField"; import CustomNumberField from "@ui_kit/CustomNumberField";
import SwitchSlider from "./switchSlider"; import SwitchSlider from "./switchSlider";
import { questionStore, updateQuestionsList } from "@root/questions"; import { questionStore, updateQuestionsList } from "@root/questions";
@ -34,7 +34,7 @@ export default function SliderOptions({ totalIndex }: Props) {
<Box sx={{ gap: "10px", display: "flex", flexDirection: "column" }}> <Box sx={{ gap: "10px", display: "flex", flexDirection: "column" }}>
<Typography>Выбор значения из диапазона</Typography> <Typography>Выбор значения из диапазона</Typography>
<Box sx={{ display: "flex", alignItems: "center", gap: "20px" }}> <Box sx={{ display: "flex", alignItems: "center", gap: "20px" }}>
<CustomTextField <CustomNumberField
placeholder={"0"} placeholder={"0"}
text={ text={
listQuestions[quizId][totalIndex].content.range.split("—")[0] listQuestions[quizId][totalIndex].content.range.split("—")[0]
@ -50,7 +50,7 @@ export default function SliderOptions({ totalIndex }: Props) {
}} }}
/> />
<Typography></Typography> <Typography></Typography>
<CustomTextField <CustomNumberField
placeholder={"100"} placeholder={"100"}
text={ text={
listQuestions[quizId][totalIndex].content.range.split("—")[1] listQuestions[quizId][totalIndex].content.range.split("—")[1]
@ -72,11 +72,12 @@ export default function SliderOptions({ totalIndex }: Props) {
display: "flex", display: "flex",
alignItems: "center", alignItems: "center",
justifyContent: "space-between", justifyContent: "space-between",
gap: "50px",
}} }}
> >
<Box> <Box sx={{ width: "100%" }}>
<Typography>Начальное значение</Typography> <Typography>Начальное значение</Typography>
<CustomTextField <CustomNumberField
placeholder={"50"} placeholder={"50"}
text={String(listQuestions[quizId][totalIndex].content.start)} text={String(listQuestions[quizId][totalIndex].content.start)}
onChange={({ target }) => { onChange={({ target }) => {
@ -88,9 +89,9 @@ export default function SliderOptions({ totalIndex }: Props) {
}} }}
/> />
</Box> </Box>
<Box> <Box sx={{ width: "100%" }}>
<Typography>Шаг</Typography> <Typography>Шаг</Typography>
<CustomTextField <CustomNumberField
placeholder={"1"} placeholder={"1"}
text={String(listQuestions[quizId][totalIndex].content.step)} text={String(listQuestions[quizId][totalIndex].content.step)}
onChange={({ target }) => { onChange={({ target }) => {

@ -30,6 +30,7 @@ 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;
@ -140,6 +141,7 @@ 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: "",

@ -0,0 +1,57 @@
import { useState } from "react";
import CustomTextField from "./CustomTextField";
import type { ChangeEvent, KeyboardEvent, FocusEvent } from "react";
import type { SxProps, Theme } from "@mui/material";
interface CustomNumberFieldProps {
placeholder: string;
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void;
onBlur?: (event: FocusEvent<HTMLInputElement>) => void;
text?: string;
sx?: SxProps<Theme>;
min?: number;
max?: number;
}
export default function CustomNumberField({
placeholder,
text,
sx,
onChange,
onKeyDown,
onBlur,
min = -999999999,
max = 999999999,
}: CustomNumberFieldProps) {
const [value, setValue] = useState<string>("");
const onInputChange = (event: ChangeEvent<HTMLInputElement>) => {
const inputValue = event.target.value;
if (
Number(inputValue) >= min &&
Number(inputValue) <= max &&
(inputValue === "" ||
inputValue.match(/^\d*$/) ||
(inputValue[0] === "-" && inputValue.slice(1).match(/^\d*$/)))
) {
setValue(inputValue);
onChange?.({ ...event, target: { ...event.target, value: inputValue } });
}
};
return (
<CustomTextField
placeholder={placeholder}
text={text}
sx={sx}
onChange={onInputChange}
onKeyDown={onKeyDown}
onBlur={onBlur}
value={value}
/>
);
}

@ -10,6 +10,7 @@ import type { ChangeEvent, KeyboardEvent, FocusEvent } from "react";
interface CustomTextFieldProps { interface CustomTextFieldProps {
placeholder: string; placeholder: string;
value?: string;
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;
@ -19,6 +20,7 @@ interface CustomTextFieldProps {
export default function CustomTextField({ export default function CustomTextField({
placeholder, placeholder,
value,
text, text,
sx, sx,
onChange, onChange,
@ -32,6 +34,7 @@ export default function CustomTextField({
<TextField <TextField
defaultValue={text} defaultValue={text}
fullWidth fullWidth
value={value}
placeholder={placeholder} placeholder={placeholder}
onChange={onChange} onChange={onChange}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}