tag-tabs.tsx
Overview
tag-tabs.tsx defines a React functional component TagTabs that provides a segmented control interface for toggling between two different tag visualization components: a tag word cloud and a tag table. It leverages Ant Design's Segmented component for the tab control and dynamically renders either the TagWordCloud or TagTable component based on the user's selection.
This component is designed to be a reusable UI element within a larger knowledge configuration or tag management system, allowing users to seamlessly switch between different visual representations of tags.
Detailed Explanation
Enumerations
TagType
enum TagType {
Cloud = 'cloud',
Table = 'table',
}
Purpose: Defines the two modes of tag visualization supported by the component.
Values:
Cloud: Represents the tag word cloud view.Table: Represents the tag table view.
Constants
TagContentMap
const TagContentMap = {
[TagType.Cloud]: <TagWordCloud></TagWordCloud>,
[TagType.Table]: <TagTable></TagTable>,
};
Purpose: Maps each
TagTypeto its corresponding React component to be rendered.Usage: Allows easy lookup and rendering of the correct component based on the current selected tab.
React Component
TagTabs
export function TagTabs()
Type: React Functional Component
Purpose: Renders a segmented tab control that toggles between two tag display components (
TagWordCloudandTagTable), using localized labels.State:
value(TagType): Tracks the currently selected tab, initialized toTagType.Cloud.
Hooks:
useState<TagType>: Manages the currently selected tab state.useTranslation: Provides i18n translation functionality.
Internal Variables:
options: An array of objects representing the segmented control options. Each option includes:label: Localized string for the tab label (e.g. "Tag Cloud", "Tag Table").value: CorrespondingTagTypevalue.
Render Output:
A
<section>element containing:An Ant Design
<Segmented>component configured with:value: current selected tab.options: tab options with localized labels.onChange: event handler to update the selected tab state.
The content component corresponding to the selected tab (either
<TagWordCloud />or<TagTable />).
Parameters
The TagTabs component does not accept any props.
Return Value
Returns JSX that renders the segmented tab UI and the selected tag visualization component.
Usage Example
import React from 'react';
import { TagTabs } from './tag-tabs';
function App() {
return (
<div>
<h1>Tag Visualization</h1>
<TagTabs />
</div>
);
}
Important Implementation Details
Uses
antd'sSegmentedcomponent to create a tab-like segmented control for switching between views.Utilizes
react-i18nextfor internationalized tab labels. The translation keys follow the pattern:knowledgeConfiguration.tagCloudandknowledgeConfiguration.tagTable.Uses
lodash'supperFirstto capitalize the first letter of the dynamic tab key string when composing translation keys.The selected component is rendered dynamically by looking up
TagContentMap[value].State management is simple and local, using React's
useStatehook.The design separates the tab control from the content views, promoting modularity and easy extension if new tag views are added.
Interaction with Other Parts of the System
Imports:
TagWordCloudandTagTablecomponents are imported from sibling files (./tag-word-cloudand./tag-table). These components are responsible for rendering the actual tag data in different visual formats.The component relies on the i18n system for localized labels, so it integrates with the overall app's translation infrastructure.
Uses Ant Design UI components, so it depends on the Ant Design library styling and behavior.
Exports:
Exports the
TagTabscomponent to be used wherever a tag visualization switcher is required.
Diagram
componentDiagram
component TagTabs {
+useState<TagType> value
+useTranslation() t
+options: SegmentedLabeledOption[]
+render(): JSX.Element
}
component Segmented {
+value: TagType
+options: SegmentedLabeledOption[]
+onChange(val: TagType)
}
component TagWordCloud
component TagTable
TagTabs --> Segmented : renders
TagTabs --> TagWordCloud : renders when value = 'cloud'
TagTabs --> TagTable : renders when value = 'table'
Summary
The tag-tabs.tsx file provides a clean, simple React component for toggling between two tag visualization modes within a UI. It combines Ant Design's segmented control with i18n support and modular tag display components to allow easy extension and maintainability within a larger tag or knowledge management system.