mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-20 21:06:54 +08:00
### What problem does this PR solve? Feat: Bind options to the parser operator form. #9869 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -1,13 +1,20 @@
|
||||
import { FormContainer } from '@/components/form-container';
|
||||
import { SelectWithSearch } from '@/components/originui/select-with-search';
|
||||
import { RAGFlowFormItem } from '@/components/ragflow-form';
|
||||
import { BlockButton, Button } from '@/components/ui/button';
|
||||
import { Form } from '@/components/ui/form';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { buildOptions } from '@/utils/form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useHover } from 'ahooks';
|
||||
import { Trash2 } from 'lucide-react';
|
||||
import { memo, useCallback } from 'react';
|
||||
import { useFieldArray, useForm } from 'react-hook-form';
|
||||
import { memo, useCallback, useRef } from 'react';
|
||||
import {
|
||||
UseFieldArrayRemove,
|
||||
useFieldArray,
|
||||
useForm,
|
||||
useFormContext,
|
||||
} from 'react-hook-form';
|
||||
import { z } from 'zod';
|
||||
import { FileType, initialParserValues } from '../../constant';
|
||||
import { useFormValues } from '../../hooks/use-form-values';
|
||||
@ -34,6 +41,51 @@ const FileFormatWidgetMap = {
|
||||
[FileType.Image]: ImageFormFields,
|
||||
};
|
||||
|
||||
type ParserItemProps = {
|
||||
name: string;
|
||||
index: number;
|
||||
fieldLength: number;
|
||||
remove: UseFieldArrayRemove;
|
||||
};
|
||||
|
||||
function ParserItem({ name, index, fieldLength, remove }: ParserItemProps) {
|
||||
const form = useFormContext();
|
||||
const ref = useRef(null);
|
||||
const isHovering = useHover(ref);
|
||||
|
||||
const prefix = `${name}.${index}`;
|
||||
const fileFormat = form.getValues(`${name}.${index}.fileFormat`);
|
||||
|
||||
const Widget =
|
||||
fileFormat && fileFormat in FileFormatWidgetMap
|
||||
? FileFormatWidgetMap[fileFormat as keyof typeof FileFormatWidgetMap]
|
||||
: OutputFormatFormField;
|
||||
return (
|
||||
<section
|
||||
className={cn('space-y-5 p-5 rounded-md', {
|
||||
'bg-state-error-5': isHovering,
|
||||
})}
|
||||
>
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-text-primary text-sm">Parser {index}</span>
|
||||
{index > 0 && (
|
||||
<Button variant={'ghost'} onClick={() => remove(index)} ref={ref}>
|
||||
<Trash2 />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<RAGFlowFormItem
|
||||
name={buildFieldNameWithPrefix(`fileFormat`, prefix)}
|
||||
label="File Formats"
|
||||
>
|
||||
<SelectWithSearch options={FileFormatOptions}></SelectWithSearch>
|
||||
</RAGFlowFormItem>
|
||||
<Widget prefix={prefix} fileType={fileFormat as FileType}></Widget>
|
||||
{index < fieldLength - 1 && <Separator />}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export const FormSchema = z.object({
|
||||
parser: z.array(
|
||||
z.object({
|
||||
@ -67,35 +119,22 @@ const ParserForm = ({ node }: INextOperatorForm) => {
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
{fields.map((field, index) => {
|
||||
const prefix = `${name}.${index}`;
|
||||
const fileFormat = form.getValues(`${name}.${index}.fileFormat`);
|
||||
|
||||
const Widget =
|
||||
fileFormat && fileFormat in FileFormatWidgetMap
|
||||
? FileFormatWidgetMap[
|
||||
fileFormat as keyof typeof FileFormatWidgetMap
|
||||
]
|
||||
: OutputFormatFormField;
|
||||
return (
|
||||
<FormContainer key={field.id}>
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-text-primary text-sm">Parser {index}</span>
|
||||
<Button variant={'ghost'} onClick={() => remove(index)}>
|
||||
<Trash2 />
|
||||
</Button>
|
||||
</div>
|
||||
<RAGFlowFormItem
|
||||
name={buildFieldNameWithPrefix(`fileFormat`, prefix)}
|
||||
label="File Formats"
|
||||
>
|
||||
<SelectWithSearch options={FileFormatOptions}></SelectWithSearch>
|
||||
</RAGFlowFormItem>
|
||||
<Widget prefix={prefix}></Widget>
|
||||
</FormContainer>
|
||||
);
|
||||
})}
|
||||
<BlockButton onClick={add}>Add Parser</BlockButton>
|
||||
<form className="px-5">
|
||||
{fields.map((field, index) => {
|
||||
return (
|
||||
<ParserItem
|
||||
key={field.id}
|
||||
name={name}
|
||||
index={index}
|
||||
fieldLength={fields.length}
|
||||
remove={remove}
|
||||
></ParserItem>
|
||||
);
|
||||
})}
|
||||
<BlockButton onClick={add} type="button">
|
||||
Add Parser
|
||||
</BlockButton>
|
||||
</form>
|
||||
<div className="p-5">
|
||||
<Output list={outputList}></Output>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user