dynamic-tool.tsx


Overview

dynamic-tool.tsx defines a React functional component named DynamicTool that provides a dynamic form interface for managing a list of "tools". Each tool consists primarily of a component_name field which is edited using a rich text/prompt editor component (PromptEditor). Users can add new tools or remove existing ones dynamically. The component leverages react-hook-form for form state management and validation, ensuring seamless integration into a larger form.

This component is designed to be embedded within a form context and allows users to flexibly create, edit, and delete multiple tool entries in a user-friendly way. It is memoized for performance optimization.


Detailed Explanation

Imports Summary


Component: DynamicTool

Purpose

DynamicTool renders a dynamic list of tool entries. Each entry contains an editable field (component_name) managed by PromptEditor. Users can add new tools or remove existing tools dynamically.

Hooks and State

Render Logic

Props

The component does not receive external props; it relies on being used within a parent form context.

Return Value

The component returns React elements representing the dynamic tool list UI.


Usage Example

import { useForm, FormProvider } from 'react-hook-form';
import DynamicTool from './dynamic-tool';

function ParentForm() {
  const methods = useForm({
    defaultValues: {
      tools: [{ component_name: 'Initial tool' }],
    },
  });

  const onSubmit = (data) => {
    console.log('Form data:', 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" {
        +useFormContext()
        +useFieldArray(name='tools')
        +render()
    }
    component "PromptEditor" <<child>>
    component "FormField" <<child>>
    component "Button" <<child>>

    DynamicTool --> FormField : renders
    FormField --> PromptEditor : renders
    DynamicTool --> Button : renders (Add, Remove)

Summary

dynamic-tool.tsx is a reusable React component designed for dynamic management of a list of tool entries within a form. It uses react-hook-form for dynamic field array management, integrates a rich text prompt editor for input, and provides UI buttons for adding/removing entries. The component is memoized for performance and depends on shared UI components and form context for full functionality. It fits into a larger form system where users can edit multiple dynamic tools as part of form submission.