Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(web): keep typed slider values on the slider's step grid (#18847)

chanx097f1a5646ed
diff --git a/web/src/components/llm-setting-items/slider.tsx b/web/src/components/llm-setting-items/slider.tsx
index 4d73ebd406..54f3afe72d 100644
--- a/web/src/components/llm-setting-items/slider.tsx
+++ b/web/src/components/llm-setting-items/slider.tsx
@@ -16,7 +16,7 @@
import { useTranslate } from '@/hooks/common-hooks';
import { cn } from '@/lib/utils';
-import { forwardRef } from 'react';
+import { forwardRef, useMemo } from 'react';
import { useFormContext } from 'react-hook-form';
import NumberInput from '../originui/number-input';
import { SingleFormSlider } from '../ui/dual-range-slider';
@@ -65,6 +65,13 @@ export const SliderInputSwitchFormField = forwardRef<
const disabled = !form.watch(checkName);
const { t } = useTranslate('chat');
+ // The typed value must stay on the slider's grid: a step of 0.01 allows two
+ // decimals, an integer step (or none) allows none.
+ const precision = useMemo(
+ () => String(step ?? '').split('.')[1]?.length ?? 0,
+ [step],
+ );
+
return (
<FormField
control={form.control}
@@ -115,6 +122,7 @@ export const SliderInputSwitchFormField = forwardRef<
max={max}
min={min}
step={step}
+ precision={precision}
{...field}
hideIcons
onChange={(value: number) => {
diff --git a/web/src/components/originui/number-input.tsx b/web/src/components/originui/number-input.tsx
index 6518c3ae90..565d2ac2f2 100644
--- a/web/src/components/originui/number-input.tsx
+++ b/web/src/components/originui/number-input.tsx
@@ -23,6 +23,21 @@ interface NumberInputProps {
hideIcons?: boolean;
integer?: boolean;
inputClassName?: string;
+ precision?: number;
+}
+
+// Truncate on the decimal string instead of `Math.trunc(v * 10 ** decimals)`,
+// which loses precision on values such as 0.29 (0.29 * 100 === 28.999...).
+function limitDecimals(value: number, decimals: number) {
+ const [integerPart, fractionPart = ''] = String(value).split('.');
+ if (fractionPart.length <= decimals) {
+ return value;
+ }
+ return Number(
+ decimals === 0
+ ? integerPart
+ : `${integerPart}.${fractionPart.slice(0, decimals)}`,
+ );
}
// Keys that would introduce a fractional part or exponent notation in
@@ -44,6 +59,7 @@ const NumberInput = forwardRef<
hideIcons = false,
integer = false,
inputClassName,
+ precision,
...props
},
ref,
@@ -100,6 +116,16 @@ const NumberInput = forwardRef<
}
if (!isNaN(newValue)) {
+ const limitedValue =
+ precision === undefined ? newValue : limitDecimals(newValue, precision);
+
+ // The state below may not change when extra decimals are dropped
+ // (0.12 -> 0.123 -> 0.12), so React would not re-render and the browser
+ // would keep showing the rejected digits. Write the DOM value directly.
+ if (limitedValue !== newValue) {
+ e.target.value = String(limitedValue);
+ }
+
// Pasted decimals bypass the keydown guard; reject them in integer
// mode instead of rounding silently.
if (integer && !Number.isInteger(newValue)) {
@@ -109,9 +135,9 @@ const NumberInput = forwardRef<
// (e.g. deleting "1024" → "102" when min=512), so the controlled input
// never snaps back mid-edit. Out-of-range values are not propagated to
// the form; handleBlur clamps them into range on focus loss.
- setValue(newValue);
- if (newValue >= min && newValue <= max) {
- onChange?.(newValue);
+ setValue(limitedValue);
+ if (limitedValue >= min && limitedValue <= max) {
+ onChange?.(limitedValue);
}
}
};