From 468d00534d90e3866eeca2e7fe1d25858d14cb68 Mon Sep 17 00:00:00 2001 From: Artemis67 <59344227+Artemis67@users.noreply.github.com> Date: Sat, 10 Feb 2024 19:16:52 -0500 Subject: [PATCH] feat: Allow scientific notation in spinbox #2239 --- ui/src/spinbox.tsx | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/ui/src/spinbox.tsx b/ui/src/spinbox.tsx index 6f58cfccae..216d20f7fc 100644 --- a/ui/src/spinbox.tsx +++ b/ui/src/spinbox.tsx @@ -114,8 +114,30 @@ export const const [head, tail = ''] = val.split('.') setVal(`${head}.${tail.slice(0, precision)}`) } - else setVal(String(newValue)) - return newValue + else { + // Check if the value is in scientific notation + if (isScientific) { + // If the value is in scientific notation, check if it's within the specified range + if (numericValue < 1e-4 || numericValue > 1e6) { + // If it's not within the range, transform the value into decimal + const decimalValue = scientificToDecimal(numericValue); + setVal(decimalValue); + return decimalValue; + } + } else { + // If the value is in decimal notation, check if it's within the specified range + if (numericValue < 1e-4 || numericValue > 1e6) { + // If it's not within the range, transform the value into scientific notation + const scientificValue = numericValue.toExponential(); + setVal(scientificValue); + return scientificValue; + } + } + + // If the value is within the specified range or not in scientific notation, keep the original value + setVal(val); + } + return newValue }, [max, min, parseValue, precision]), handleOnInput = React.useCallback((val: U) => { wave.args[name] = val @@ -148,4 +170,4 @@ export const disabled={disabled} /> ) - } \ No newline at end of file + }