Skip to content

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

1.6 KB
Raw Blame
import { RAGFlowFormItem } from '@/components/ragflow-form';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Trash2 } from 'lucide-react';
import { useCallback } from 'react';
import { useFieldArray, useFormContext } from 'react-hook-form';
import { DynamicFormHeader, FormListHeaderProps } from './dynamic-fom-header';

type DynamicStringFormProps = { name: string } & FormListHeaderProps;
export function DynamicStringForm({ name, label }: DynamicStringFormProps) {
  const form = useFormContext();

  const { fields, append, remove } = useFieldArray({
    name: name,
    control: form.control,
  });

  // append is not a change event, so trigger manually to surface
  // the validation errors of the freshly added empty row.
  const handleAdd = useCallback(() => {
    append({ value: '' });
    form.trigger(name);
  }, [append, form, name]);

  return (
    <section>
      <DynamicFormHeader label={label} onClick={handleAdd}></DynamicFormHeader>
      <div className="space-y-4">
        {fields.map((field, index) => (
          <div key={field.id} className="flex items-start gap-2">
            <RAGFlowFormItem
              name={`${name}.${index}.value`}
              label="delimiter"
              labelClassName="!hidden"
              className="flex-1 !m-0"
            >
              <Input className="!m-0"></Input>
            </RAGFlowFormItem>
            <Button
              type="button"
              variant={'ghost'}
              onClick={() => remove(index)}
            >
              <Trash2 />
            </Button>
          </div>
        ))}
      </div>
    </section>
  );
}