dynamic-tool.tsx
Overview
dynamic-tool.tsx is a React functional component that provides a dynamic, form-based UI for managing a list of "tools." Each tool consists primarily of a component_name property, which is edited via a rich text editor component called PromptEditor. The component leverages the react-hook-form library to integrate with a larger form context, supporting dynamic addition and removal of tool entries. It is optimized with React's memo for performance to prevent unnecessary re-renders.
This component is designed to be embedded within a parent form and relies heavily on form context and state management provided externally. The UI uses custom buttons and form controls from a shared UI library, and integrates icons from the lucide-react package.
Detailed Explanation
Component: DynamicTool
Purpose:
To render a dynamic list of tool inputs where each tool can be edited and removed, and new tools can be added. It handles form state synchronization for an array of tools under the form field named "tools".
Dependencies:
react-hook-form: Provides form context and field array management.PromptEditor: A custom editor component for inputtingcomponent_name.UI components (
Button,BlockButton,FormControl,FormField,FormItem,FormMessage) for consistent UI/UX.lucide-reacticonXfor the remove button.React's
memofor performance optimization.
Props:
This component does not receive any props; instead, it uses
useFormContext()fromreact-hook-formto connect to the parent form.
State and Form Management:
Uses
useFormContext()to access the form's control and state.Uses
useFieldArraywith the name"tools"to manage an array of tool items.fields: Current array of tool fields with IDs for React keys.append: Function to add a new tool.remove: Function to remove a tool by index.
Rendered UI:
For each tool in the
fieldsarray:Renders a
FormFieldbound to the pathtools.[index].component_name.Inside the
FormField, renders thePromptEditorcomponent configured to edit the tool'scomponent_name.A remove button with an
Xicon to delete the tool entry.
Below the list of tools:
Displays any form validation messages related to the
"tools"field.A block-level "Add" button to append a new empty tool.
Methods and Callbacks:
append({ component_name: '' }): Adds a new empty tool entry.remove(index): Removes the tool entry at the given index.
Usage Example
import { useForm, FormProvider } from 'react-hook-form';
import DynamicTool from './dynamic-tool';
const MyForm = () => {
const methods = useForm({
defaultValues: {
tools: [{ component_name: 'Initial tool' }]
}
});
const onSubmit = (data) => {
console.log('Form Submission:', data);
};
return (
<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(onSubmit)}>
<DynamicTool />
<button type="submit">Submit</button>
</form>
</FormProvider>
);
};
Important Implementation Details
Form Integration: The component relies on
useFormContext()— it must be rendered inside aFormProvideror a parent usinguseForm.Dynamic Field Array:
useFieldArrayenables dynamic manipulation of nested array fields, which is essential for adding/removing tools without losing form state.Performance: The component is wrapped with
React.memoto prevent re-rendering unless props or form state it depends on change.Field Naming Convention: Uses nested path strings like
"tools.0.component_name"to bind each editor to its corresponding form field.UI Consistency: Utilizes shared UI components for form elements and buttons, ensuring consistent styling and behavior.
Accessibility: Buttons use semantic HTML (
type="button") and include icons for clarity.
Interaction with Other Parts of the System
Parent Form: This component is a child within a larger form managed by
react-hook-form. It depends on the parent form to provide form context.PromptEditor: The editing UI for each tool's
component_nameis handled by thePromptEditorcomponent, which likely supports rich text or prompt editing features.UI Library: Uses shared UI components (
Button,FormField, etc.) from the application’s UI library, ensuring visual consistency.Iconography: Uses
lucide-reactfor vector icons.State Management: Form state is lifted and managed by
react-hook-format a higher level.
Mermaid Component Diagram
componentDiagram
component DynamicTool {
+form: FormContext
+fields: Tool[]
+append(tool: Tool)
+remove(index: number)
+render()
}
component PromptEditor {
+props: { value, onChange, showToolbar }
}
component Button
component BlockButton
component FormField
component FormItem
component FormControl
component FormMessage
component XIcon
DynamicTool --> PromptEditor : renders for each tool
DynamicTool --> FormField : binds form state
DynamicTool --> Button : remove tool buttons
DynamicTool --> BlockButton : add tool button
DynamicTool --> FormItem
DynamicTool --> FormControl
DynamicTool --> FormMessage
Button --> XIcon
Summary
The dynamic-tool.tsx file exports a memoized React component that dynamically manages a list of tools inside a form. It integrates tightly with react-hook-form for state management, using useFieldArray to manage an array of tool objects. Each tool's key editable field is component_name, edited via the PromptEditor component. The UI allows users to add and remove tools dynamically, and displays validation messages from the form context. This component is designed to be reusable within any parent form that provides react-hook-form context.
If you need additional details about PromptEditor or the UI components, please consult their respective documentation files.