next-dynamic-input-variable.tsx
Overview
This file defines React components for dynamically managing input variables within a flow-based system, likely part of a complex UI workflow editor or builder. It provides a form interface to add, remove, and configure variables that can be either references to other components or direct user inputs.
The main functionalities include:
Rendering a dynamic list of variable input fields.
Allowing users to choose variable types (reference or input).
Dynamically switching input controls based on variable type.
Providing UI elements to add or remove variables.
Encapsulating the form inside a collapsible UI for better UX.
This file heavily utilizes the React Hook Form library for form state management and i18next for internationalization. It also integrates with custom UI components and hooks from the application ecosystem.
Detailed Explanation
Interfaces and Enums
IProps
interface IProps {
node?: RAGFlowNodeType;
}
Purpose: Defines the props accepted by the main components in this file.
Properties:
node(optional): Represents a node in the flow, typed asRAGFlowNodeType, which provides context such as node ID and parent ID.
Enum: VariableType
enum VariableType {
Reference = 'reference',
Input = 'input',
}
Defines the two types of variables supported:
Reference: Variable refers to another component by ID.Input: Variable is a direct user input value.
Function: getVariableName
const getVariableName = (type: string) =>
type === VariableType.Reference ? 'component_id' : 'value';
Purpose: Returns the form field name based on the variable type.
Parameters:
type(string): The variable type.
Returns:
'component_id'if the type isReference, otherwise'value'.Usage: Dynamically selects which form field to bind (
component_idorvalue) depending on the variable type.
Component: DynamicVariableForm
export function DynamicVariableForm({ node }: IProps) { ... }
Purpose
Renders a dynamic form interface to manage a list of variables associated with a flow node. Each variable can be either a reference or an input. Users can add new variables or remove existing ones.
Implementation Details
Uses
useFormContextanduseFieldArrayfromreact-hook-formto manage the form state of an array of variables under thequeryfield.Uses
useTranslationfromreact-i18nextfor localized UI text.Uses a custom hook
useBuildVariableOptionsto fetch or build options for reference variables based on the current node.Defines a static list of options for variable types (
Reference,Input).For each variable item:
Renders a select dropdown to pick variable type.
Conditionally renders either:
A select dropdown to pick a referenced component, or
A text input field for direct input.
Provides a trash icon button to remove the variable.
Provides an "Add Variable" button that appends a new variable entry.
Props
node(optional): The current flow node, used to derive options for reference variables.
Return
JSX element rendering the variable form UI.
Usage Example
<DynamicVariableForm node={someNodeObject} />
Component: DynamicInputVariable
export function DynamicInputVariable({ node }: IProps) { ... }
Purpose
Wraps the DynamicVariableForm inside a collapsible UI component for better UX, allowing users to expand or collapse the input variables section.
Implementation Details
Uses
Collapsible,CollapsibleTrigger, andCollapsibleContentcomponents for the collapsible panel.The trigger includes a title and an icon button with a downward arrow icon.
The content contains the
DynamicVariableFormcomponent.Uses
useTranslationfor the "Input" label.
Props
node(optional): Passed down toDynamicVariableForm.
Return
JSX element rendering a collapsible section with the dynamic variable form.
Usage Example
<DynamicInputVariable node={someNodeObject} />
Important Implementation Details and Algorithms
Dynamic form management: Uses
react-hook-form'suseFieldArrayto dynamically add or remove form fields representing input variables. This enables complex form structures with minimal boilerplate.Dynamic field naming: The
getVariableNamehelper function dynamically chooses which property (component_idorvalue) of each variable record to bind the input to, depending on the variable type.State reset on type change: When the user changes the variable type dropdown, the corresponding other field values (
valueorcomponent_id) are reset to avoid stale data.Custom options fetching: The
useBuildVariableOptionshook likely fetches or constructs a list of valid reference options based on the current node context, promoting modularity and separation of concerns.Internationalization: All user-facing text strings are wrapped with
t()to support multiple languages seamlessly.
Interaction With Other Parts of the System
UI Components: Relies on custom UI components (
Button,Input,RAGFlowSelect,Collapsible,Form*components) which are part of the application's design system.Icons: Uses SVG icons (
SideDown,Plus,Trash2) imported from assets andlucide-react.Form State: Integrates tightly with
react-hook-formfor form state management, assuming aFormProviderhigher in the component tree.Flow Data: Uses the
RAGFlowNodeTypeinterface from the data layer, indicating this component is part of a flow/graph editing interface.Hooks: Uses
useBuildVariableOptionswhich is an application-specific hook to fetch variable options dynamically.Localization: Uses
react-i18nextfor translations, implying the app supports multiple languages.
Mermaid Component Diagram
componentDiagram
component DynamicInputVariable {
+props: IProps
+renders Collapsible
+contains DynamicVariableForm
}
component DynamicVariableForm {
+props: IProps
+uses useFormContext, useFieldArray
+renders variable list UI
+handles add/remove variable
+renders variable type selector (RAGFlowSelect)
+renders variable value input (Input or RAGFlowSelect)
}
component useBuildVariableOptions {
+input: node.id, node.parentId
+output: options for reference variables
}
DynamicInputVariable --> DynamicVariableForm
DynamicVariableForm --> useBuildVariableOptions
Summary
This file provides a sophisticated UI for managing dynamic input variables within a flow node context. It leverages modern React form management, custom UI components, and contextual hooks to provide a flexible, localized, and user-friendly interface for variable configuration. The main components are DynamicVariableForm (the core form) and DynamicInputVariable (a collapsible wrapper). The file is part of a larger system managing flow nodes and their data dependencies.