tag-feature-item.tsx


Overview

The tag-feature-item.tsx file defines a React functional component named TagFeatureItem. This component is designed to manage and render a dynamic list of "tag features" within a form context using the react-hook-form library. Each tag feature consists of a selectable tag and an associated frequency number input.

This component integrates with the application's knowledge base configuration and tag list fetching hooks to dynamically populate tag options based on external knowledge sources. It provides UI controls to add, remove, and select tags with frequency values, ensuring that tag options remain unique across the list.


Detailed Explanation

Component: TagFeatureItem

Purpose

TagFeatureItem provides an interface to manipulate a list of tags with associated frequency values as part of a larger form. It is used in contexts where user input for tags related to knowledge configurations is needed. It interacts tightly with knowledge base configurations to fetch tag options and dynamically filters available tags to prevent duplicates.

Usage Example

import { useForm, FormProvider } from 'react-hook-form';
import { TagFeatureItem } from './tag-feature-item';

const ExampleForm = () => {
  const methods = useForm({
    defaultValues: {
      tag_feas: [{ tag: '', frequency: 0 }],
    },
  });

  const onSubmit = (data) => {
    console.log(data.tag_feas);
  };

  return (
    <FormProvider {...methods}>
      <form onSubmit={methods.handleSubmit(onSubmit)}>
        <TagFeatureItem />
        <button type="submit">Submit</button>
      </form>
    </FormProvider>
  );
};

Imports and Dependencies


Key Constants and Variables


Core Logic and Flow

  1. Fetching Tag Options:

    • useFetchTagListByKnowledgeIds provides list of tags based on tagKnowledgeIds.

    • setKnowledgeIds(tagKnowledgeIds) triggers fetching tags based on current knowledge base IDs.

    • options array is memoized and constructed from fetched list to be used in dropdowns.

  2. Filtering Tag Options:

    • filterOptions(index) returns available tags for the tag at index by excluding tags already selected in other list items.

    • This ensures tag uniqueness across the form list.

  3. Form Management:

    • Uses useFormContext() to access the form state.

    • Uses useFieldArray() to manage dynamic array of tag features.

    • Each tag feature has two fields:

      • tag: string, selected from dropdown.

      • frequency: number, input via number input (range 0-10).

  4. Rendering:

    • Renders a FormField wrapping the entire tag feature list.

    • For each item in fields, renders:

      • A SelectWithSearch dropdown for tag selection, with filtered options.

      • A NumberInput for frequency.

      • A CircleMinus icon button to remove the tag entry.

    • At the bottom, a dashed Button with a Plus icon to append a new tag entry with default values.


Props and Return Values

TagFeatureItem is a React functional component with no props. It relies entirely on the form context (useFormContext()) and external hooks for its data.


Important Implementation Details


Interaction with Other Parts of the System


Mermaid Component Diagram

componentDiagram
    component TagFeatureItem {
        +render()
        -filterOptions(index)
    }
    component useFetchTagListByKnowledgeIds
    component useFetchKnowledgeBaseConfiguration
    component useFormContext
    component useFieldArray
    component SelectWithSearch
    component NumberInput
    component Button

    TagFeatureItem --> useFetchTagListByKnowledgeIds : fetch tags
    TagFeatureItem --> useFetchKnowledgeBaseConfiguration : fetch config
    TagFeatureItem --> useFormContext : form data
    TagFeatureItem --> useFieldArray : dynamic list management
    TagFeatureItem --> SelectWithSearch : tag selection UI
    TagFeatureItem --> NumberInput : frequency input UI
    TagFeatureItem --> Button : add new tag button

Summary

The tag-feature-item.tsx file provides a reusable, form-integrated React component for managing a dynamic list of tag-frequency pairs. It smartly integrates with knowledge base configurations and tag fetching hooks to provide an up-to-date and unique set of tag options. The component enforces uniqueness of tags in the list, supports localization, and leverages a consistent UI framework.

This component is intended for use within larger forms where tagging and frequency weighting of tags are required, particularly in knowledge configuration contexts. It fits into the system by bridging dynamic data fetching and form state management with a user-friendly, responsive UI.


End of Documentation