import React, { useEffect, useRef, useState } from 'react'; import { InputGroup, Form, Row, Col, Collapse, ToggleButton } from 'react-bootstrap'; import { emit_update } from '../socket'; import { DocStringComponent } from './DocStringComponent'; import { Slider } from '@mui/material'; import { NumberComponent } from './NumberComponent'; import { getIdFromFullAccessPath } from '../utils/stringUtils'; interface SliderComponentProps { name: string; min: number; max: number; parentPath?: string; value: number; readOnly: boolean; docString: string; stepSize: number; isInstantUpdate: boolean; addNotification: (message: string) => void; } export const SliderComponent = React.memo((props: SliderComponentProps) => { const renderCount = useRef(0); const [open, setOpen] = useState(false); const { name, parentPath, value, min, max, stepSize, readOnly, docString, isInstantUpdate, addNotification } = props; const fullAccessPath = parentPath.concat('.' + name); const id = getIdFromFullAccessPath(fullAccessPath); useEffect(() => { renderCount.current++; }); useEffect(() => { addNotification(`${parentPath}.${name} changed to ${value}.`); }, [props.value]); useEffect(() => { addNotification(`${parentPath}.${name}.min changed to ${min}.`); }, [props.min]); useEffect(() => { addNotification(`${parentPath}.${name}.max changed to ${max}.`); }, [props.max]); useEffect(() => { addNotification(`${parentPath}.${name}.stepSize changed to ${stepSize}.`); }, [props.stepSize]); const emitSliderUpdate = ( name: string, parentPath: string, value: number, callback?: (ack: unknown) => void, min: number = props.min, max: number = props.max, stepSize: number = props.stepSize ) => { emit_update( name, parentPath, { value: value, min: min, max: max, step_size: stepSize }, callback ); }; const handleOnChange = (event, newNumber: number | number[]) => { // This will never be the case as we do not have a range slider. However, we should // make sure this is properly handled. if (Array.isArray(newNumber)) { newNumber = newNumber[0]; } emitSliderUpdate(name, parentPath, newNumber); }; const handleValueChange = (newValue: number, valueType: string) => { switch (valueType) { case 'min': emitSliderUpdate(name, parentPath, value, undefined, newValue); break; case 'max': emitSliderUpdate(name, parentPath, value, undefined, min, newValue); break; case 'stepSize': emitSliderUpdate(name, parentPath, value, undefined, min, max, newValue); break; default: break; } }; return (
{process.env.NODE_ENV === 'development' && (

Render count: {renderCount.current}

)} {name} handleOnChange(event, newNumber)} min={min} max={max} step={stepSize} marks={[ { value: min, label: `${min}` }, { value: max, label: `${max}` } ]} /> null} /> setOpen(!open)} type="checkbox" checked={open} value="" className="btn" variant="light" aria-controls="slider-settings" aria-expanded={open}> Min Value handleValueChange(Number(e.target.value), 'min')} /> Max Value handleValueChange(Number(e.target.value), 'max')} /> Step Size handleValueChange(Number(e.target.value), 'stepSize')} />
); });