tag-feature-item.tsx
Overview
tag-feature-item.tsx is a React functional component designed for managing and rendering a dynamic list of tag-feature configurations within a form. It provides an interface to add, select, and configure multiple tags along with their associated frequency values. This component integrates with a knowledge base system by fetching configuration and tag data using custom hooks and ensures the user selects unique tags across the list.
The component leverages Ant Design's form-related components (Form, Select, InputNumber, Button) and icons for UI consistency and usability. It also supports internationalization via the react-i18next library.
Detailed Component Explanation
TagFeatureItem
Description
Renders a form item that allows users to manage a list of tags, each with a frequency value. Users can dynamically add or remove tags. The tag options are fetched based on configured knowledge base IDs and filtered to avoid duplicate selections in the list.
Usage
Used within a form context (wrapped inside an Ant Design Form) when tag-feature configuration is required from the user, particularly for knowledge base or tagging systems.
Internal Mechanics and Hooks Used
useFetchKnowledgeBaseConfiguration: Custom hook that fetches the knowledge base configuration object.
useFetchTagListByKnowledgeIds: Custom hook that fetches a list of tags filtered by knowledge base IDs.
useTranslation: Hook from react-i18next for internationalized strings.
Form.useFormInstance: Ant Design hook to get the current form instance for reading and manipulating form data.
Props
This component does not receive any props directly; it relies on the form context provided by Ant Design's Form component.
Internal Constants and Variables
FieldKey: A constant string 'tag_feas' used as the form list field name.
form: The form instance to access and manipulate form data.
knowledgeConfiguration: The fetched knowledge base configuration data.
tagKnowledgeIds: Extracted knowledge base IDs from the configuration for tag filtering.
list: The list of tags fetched from the knowledge base.
options: Mapped list of tag options formatted for the component.filterOptions(index): A function that filters out tag options already selected in other list items, preventing duplicates.Component Structure and Behavior Fetching configuration and tags:The component first fetches knowledge base configuration.Extracts tag-related knowledge base IDs (tag_kb_ids).Uses these IDs to fetch the list of available tags.Filtering tag options:When rendering each item, filterOptions ensures that tags already selected in other items are not available for selection, ensuring unique tags per list item.Dynamic list management:Implements Ant Design's Form.List to manage a dynamic array of tag-frequency pairs.Users can add a new tag-frequency pair by clicking the "Add Tag" button.Each item includes:A dropdown select for choosing a tag (required).An input number for setting frequency (required, 0-10).A minus (remove) icon to delete the item from the list.Localization:Labels, placeholders, and button text leverage the t function from react-i18next for translation support.Code Example of Use import React from 'react'; import { Form } from 'antd'; import { TagFeatureItem } from './tag-feature-item'; const ExampleForm = () => ( <Form> {/* Other form items */} <TagFeatureItem /> {/* Submit button, etc. */} </Form> ); export default ExampleForm; Important Implementation Details Uniqueness enforcement:The filterOptions function dynamically excludes tags already selected in other form list items from the select dropdown, avoiding duplicate tag selection.Dynamic knowledge base integration:The component listens for changes in knowledge base configuration and updates the knowledge IDs accordingly via setKnowledgeIds from the useFetchTagListByKnowledgeIds hook.Form integration:Uses Ant Design's Form.List to manage an array of form items, handling validation and dynamic addition/removal seamlessly.Performance optimization:Uses React hooks such as useMemo and useCallback to memoize computed values and functions, preventing unnecessary re-renders and computations.Interaction with Other System Parts Knowledge base system:The component depends on the knowledge base configuration and tag data fetched from external sources (via hooks). It reacts to changes in these data sources to update its UI.Form infrastructure:Meant to be embedded within an Ant Design Form, interacting with form validation, submission, and data management.Internationalization system:Uses react-i18next for translation keys such as 'knowledgeConfiguration.tags', 'common.pleaseSelect', etc., ensuring multi-language support.Hooks dependencies: useFetchKnowledgeBaseConfiguration provides configuration data.useFetchTagListByKnowledgeIds manages fetching tag lists filtered by knowledge IDs and exposes a method to update these IDs.Mermaid Component Diagram componentDiagram component TagFeatureItem { +Render Form.Item (label) +Render Form.List (name='tag_feas') +Add/Remove Tag-Frequency Items +Uses useFetchKnowledgeBaseConfiguration() +Uses useFetchTagListByKnowledgeIds() +Uses useTranslation() } component Form { +useFormInstance() +Form.Item +Form.List } component useFetchKnowledgeBaseConfiguration { +Returns knowledgeConfiguration } component useFetchTagListByKnowledgeIds { +Returns list +setKnowledgeIds(ids) } component react-i18next { +useTranslation() +t(key) } TagFeatureItem --> Form : uses form context TagFeatureItem --> useFetchKnowledgeBaseConfiguration : fetch config TagFeatureItem --> useFetchTagListByKnowledgeIds : fetch tags TagFeatureItem --> react-i18next : translation Summary TagFeatureItem is a reusable React component designed to manage a list of unique tags with configurable frequencies, tightly integrated with knowledge base data and forms. It ensures a clean user experience through dynamic option filtering, validation, and localization, supporting the application's tagging and knowledge base configuration workflows.