import * as React from "react"; import Box from "@mui/material/Box"; import Grid from "@mui/material/Grid2"; import Typography from "@mui/material/Typography"; import Slider, { SliderThumb } from "@mui/material/Slider"; import { inputProps } from "../interface/inputfieldProps"; import { DataContext } from "../context/Context"; import { TextField } from "@mui/material"; import { useEffect } from "react"; const CustomThumb = ({ children, theme, ...rest }) => ( {children} ); export default function InputSlider(props: inputProps) { const { required, errors, data, uischema, handleChange, path, schema, rootSchema, } = props; const uischemaData = uischema?.config?.main; const min = Number(uischemaData.min); const max = Number(uischemaData.max); const step = Number(uischemaData.step); const [sliderValue, setSliderValue] = React.useState( Number(data) || min ); const [textValue, setTextValue] = React.useState( data?.toString() || min?.toString() ); useEffect(() => { if(data) setSliderValue(Number(data)); setTextValue(data?.toString()); }, [data]); const { theme } = React.useContext(DataContext); const onTextFieldChange = (event: any, newValue: string) => { if (+event.target.value <= +uischemaData.max) { setTextValue(newValue); throttledFunction(Number(newValue)); } }; function throttle(fn: any, delay: any) { let timer: any; return function (...args) { clearTimeout(timer); timer = setTimeout(() => fn(...args), delay); }; } const throttledFunction = throttle((value: number) => { handleChange(path, value); }, 100); const marks = [ { value: min, label: min, }, { value: max, label: max, }, ]; return ( {uischemaData.label} Value:{" "} { onTextFieldChange(e, e.target.value); }} type="number" variant="outlined" size="small" sx={{ width: `${(textValue || '').length + 3}ch`, backgroundColor: "transparent", borderRadius: "5px", "& input": { color: theme.myTheme.palette.primary.dark, padding: "0px 0px 0px 4px", }, "& .MuiOutlinedInput-root": { "& fieldset": { border: "none", }, }, }} /> { setSliderValue(newValue as number); setTextValue(newValue.toString()); }} onChangeCommitted={() => throttledFunction(sliderValue)} aria-labelledby="input-slider" marks={marks} slots={{ thumb: (props) => ( ), }} > ); }