From 32ecc9520ff7ff33e6f979350822fc54d75d0569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Tue, 8 Aug 2023 15:52:31 +0200 Subject: [PATCH] feat: adding NumberComponent to SliderComponent --- frontend/src/components/NumberComponent.tsx | 29 +++++-- frontend/src/components/SliderComponent.tsx | 83 +++++++++++++-------- 2 files changed, 74 insertions(+), 38 deletions(-) diff --git a/frontend/src/components/NumberComponent.tsx b/frontend/src/components/NumberComponent.tsx index 676660d..d93973f 100644 --- a/frontend/src/components/NumberComponent.tsx +++ b/frontend/src/components/NumberComponent.tsx @@ -15,6 +15,13 @@ interface NumberComponentProps { docString: string; isInstantUpdate: boolean; unit?: string; + showName?: boolean; + customEmitUpdate?: ( + name: string, + parent_path: string, + value: number, + callback?: (ack: unknown) => void + ) => void; } // TODO: highlight the digit that is being changed by setting both selectionStart and @@ -102,6 +109,14 @@ const handleDeleteKey = ( export const NumberComponent = React.memo((props: NumberComponentProps) => { const { name, parent_path, readOnly, docString, isInstantUpdate, unit } = props; + + // Whether to show the name infront of the component (false if used with a slider) + const showName = props.showName !== undefined ? props.showName : true; + // If emitUpdate is passed, use this instead of the emit_update from the socket + // Also used when used with a slider + const emitUpdate = + props.customEmitUpdate !== undefined ? props.customEmitUpdate : emit_update; + const renderCount = useRef(0); // Create a state for the cursor position const [cursorPosition, setCursorPosition] = useState(null); @@ -199,7 +214,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => { selectionEnd )); } else if (key === 'Enter' && !isInstantUpdate) { - emit_update(name, parent_path, Number(newValue)); + emitUpdate(name, parent_path, Number(newValue)); return; } else { console.debug(key); @@ -208,7 +223,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => { // Update the input value and maintain the cursor position if (isInstantUpdate) { - emit_update(name, parent_path, Number(newValue)); + emitUpdate(name, parent_path, Number(newValue)); } setInputString(newValue); @@ -220,20 +235,20 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => { const handleBlur = () => { if (!isInstantUpdate) { // If not in "instant update" mode, emit an update when the input field loses focus - emit_update(name, parent_path, Number(inputString)); + emitUpdate(name, parent_path, Number(inputString)); } }; return ( -
- {process.env.NODE_ENV === 'development' && ( +
+ {process.env.NODE_ENV === 'development' && showName && (

Render count: {renderCount.current}

)}
-
+
- {name} + {showName && {name}} { renderCount.current++; }); - const { name, parent_path, value, min, max, stepSize, readOnly, docString } = props; + const { + name, + parent_path, + value, + min, + max, + stepSize, + readOnly, + docString, + isInstantUpdate + } = props; - const socketEmit = ( - newNumber: number, + const emitSliderUpdate = ( + name: string, + parent_path: string, + value: number, + callback?: (ack: unknown) => void, min: number = props.min, max: number = props.max, stepSize: number = props.stepSize ) => { - emit_update(name, parent_path, { - value: newNumber, - min: min, - max: max, - step_size: stepSize - }); + emit_update( + name, + parent_path, + { + 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 @@ -45,19 +64,19 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => { if (Array.isArray(newNumber)) { newNumber = newNumber[0]; } - socketEmit(newNumber, min, max, stepSize); + emitSliderUpdate(name, parent_path, newNumber); }; const handleValueChange = (newValue: number, valueType: string) => { switch (valueType) { case 'min': - socketEmit(value, newValue, max, stepSize); + emitSliderUpdate(name, parent_path, value, undefined, newValue); break; case 'max': - socketEmit(value, min, newValue, stepSize); + emitSliderUpdate(name, parent_path, value, undefined, min, newValue); break; case 'stepSize': - socketEmit(value, min, max, newValue); + emitSliderUpdate(name, parent_path, value, undefined, min, max, newValue); break; default: break; @@ -65,24 +84,21 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => { }; return ( -
+
{process.env.NODE_ENV === 'development' && (

Render count: {renderCount.current}

)} - - - {name} - - {/* */} + + {name} + + handleOnChange(event, newNumber)} @@ -94,14 +110,19 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => { { value: max, label: `${max}` } ]} /> - {/* */} - {/* */} + + +