Skip to content

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

4.7 KB
Raw Blame
/*
 *  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import { FormLayout } from '@/constants/form';
import { cn } from '@/lib/utils';
import { forwardRef, ReactNode, useMemo } from 'react';
import { useFormContext } from 'react-hook-form';
import NumberInput from './originui/number-input';
import { SingleFormSlider } from './ui/dual-range-slider';
import {
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from './ui/form';

export type FormLayoutType = {
  layout?: FormLayout;
};

type SliderInputFormFieldProps = {
  max?: number;
  min?: number;
  step?: number;
  name: string;
  label: string;
  tooltip?: ReactNode;
  defaultValue?: number;
  className?: string;
  numberInputClassName?: string;
  percentage?: boolean;
  integer?: boolean;
  sliderTestId?: string;
  numberInputTestId?: string;
} & FormLayoutType;

export const SliderInputFormField = forwardRef<
  HTMLDivElement,
  SliderInputFormFieldProps
>(
  (
    {
      max,
      min,
      step,
      label,
      name,
      tooltip,
      defaultValue,
      className,
      numberInputClassName,
      layout = FormLayout.Horizontal,
      percentage = false,
      integer = false,
      sliderTestId,
      numberInputTestId,
    },
    ref,
  ) => {
    const form = useFormContext();

    const isHorizontal = useMemo(
      () => layout !== FormLayout.Vertical,
      [layout],
    );
    const displayMax = percentage ? (max || 1) * 100 : max;
    const displayMin = percentage ? (min || 0) * 100 : min;
    const displayStep = percentage ? (step || 0.01) * 100 : step;
    return (
      <FormField
        control={form.control}
        name={name}
        defaultValue={defaultValue || 0}
        render={({ field }) => (
          <FormItem
            ref={ref}
            className={cn({
              'flex items-center gap-1 space-y-0': isHorizontal,
            })}
          >
            <FormLabel
              tooltip={tooltip}
              className={cn({
                'text-sm whitespace-break-spaces w-1/4': isHorizontal,
              })}
            >
              {label}
            </FormLabel>
            <div
              className={cn(
                'flex items-center gap-4 justify-between',
                { 'w-3/4': isHorizontal },
                className,
              )}
            >
              <FormControl>
                <SingleFormSlider
                  {...field}
                  value={
                    percentage ? Math.round(field.value * 100) : field.value
                  }
                  onChange={(value) =>
                    field.onChange(percentage ? Math.round(value) / 100 : value)
                  }
                  max={displayMax}
                  min={displayMin}
                  step={displayStep}
                  data-testid={sliderTestId}
                ></SingleFormSlider>
              </FormControl>
              <FormControl>
                <NumberInput
                  className={cn(
                    'h-6 w-16 p-0 text-center bg-bg-input border border-border-button text-text-secondary',
                    '[appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none',
                    numberInputClassName,
                  )}
                  max={displayMax}
                  min={displayMin}
                  step={displayStep}
                  hideIcons
                  integer={integer}
                  value={
                    percentage ? Math.round(field.value * 100) : field.value
                  }
                  onChange={(val) => {
                    const value = Number(val || 0);
                    if (value >= 0) {
                      field.onChange(
                        percentage ? Math.round(value) / 100 : value,
                      );
                    }
                  }}
                  data-testid={numberInputTestId}
                ></NumberInput>
              </FormControl>
            </div>
            <FormMessage />
          </FormItem>
        )}
      />
    );
  },
);

SliderInputFormField.displayName = 'SliderInputFormField';