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:

Props:

State and Form Management:

Rendered UI:

Methods and Callbacks:


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


Interaction with Other Parts of the System


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.