-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tabs component #1557
base: main
Are you sure you want to change the base?
tabs component #1557
Conversation
WalkthroughA new tabbed interface component has been introduced to the frontend application. The implementation includes a React component ( Changes
Sequence DiagramsequenceDiagram
participant User
participant TabComponent
participant TabItem
User->>TabComponent: Click/Navigate Tab
TabComponent->>TabComponent: Update activeIndex
TabComponent->>TabItem: Trigger onClick handler
TabItem-->>User: Render/Respond
Poem
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (2)
frontend/app/element/tabs.stories.tsx (1)
17-31
: Consider improving the story implementation.A few suggestions to enhance the story:
- Replace console.log with more meaningful action handlers for demonstration purposes
- Move the styling to a dedicated style file or story decorator instead of inline styles
export const DefaultTabs: Story = { render: () => { const tabs = [ - { label: "Node 1", onClick: () => console.log("Node 1 Clicked") }, - { label: "Node 2", onClick: () => console.log("Node 2 Clicked") }, - { label: "Node 3", onClick: () => console.log("Node 3 Clicked") }, + { label: "Node 1", onClick: () => { /* Add meaningful demo action */ } }, + { label: "Node 2", onClick: () => { /* Add meaningful demo action */ } }, + { label: "Node 3", onClick: () => { /* Add meaningful demo action */ } }, ]; return ( - <div style={{ padding: "20px", backgroundColor: "#000", color: "#fff" }}> + <div className="story-container"> <Tabs tabs={tabs} /> </div> ); }, };frontend/app/element/tabs.tsx (1)
16-18
: Consider implementing controlled/uncontrolled pattern.The component could benefit from:
- Supporting both controlled and uncontrolled modes
- Validating the active index
const Tabs: React.FC<TabsProps> = ({ tabs, + activeIndex: controlledIndex, + defaultActiveIndex = 0, + onChange, }) => { - const [activeIndex, setActiveIndex] = useState(0); + const [internalIndex, setInternalIndex] = useState(defaultActiveIndex); + const activeIndex = controlledIndex ?? internalIndex; + + const setActiveIndex = (index: number) => { + if (index < 0 || index >= tabs.length) return; + setInternalIndex(index); + onChange?.(index); + };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
frontend/app/element/tabs.scss
(1 hunks)frontend/app/element/tabs.stories.tsx
(1 hunks)frontend/app/element/tabs.tsx
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- frontend/app/element/tabs.scss
🔇 Additional comments (1)
frontend/app/element/tabs.stories.tsx (1)
1-15
: LGTM!
The metadata setup and imports are properly structured.
export const TabsWithAlerts: Story = { | ||
render: () => { | ||
const tabs = [ | ||
{ label: "Node 1", onClick: () => alert("Node 1 Clicked") }, | ||
{ label: "Node 2", onClick: () => alert("Node 2 Clicked") }, | ||
{ label: "Node 3", onClick: () => alert("Node 3 Clicked") }, | ||
]; | ||
|
||
return ( | ||
<div style={{ padding: "20px", backgroundColor: "#000", color: "#fff" }}> | ||
<Tabs tabs={tabs} /> | ||
</div> | ||
); | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Replace alert() with a more user-friendly notification system.
Using alert()
is not recommended as it:
- Blocks the UI
- Provides a poor user experience
- Doesn't represent real-world usage
Consider using a toast notification or a more elegant feedback mechanism.
export const TabsWithAlerts: Story = {
render: () => {
const tabs = [
- { label: "Node 1", onClick: () => alert("Node 1 Clicked") },
- { label: "Node 2", onClick: () => alert("Node 2 Clicked") },
- { label: "Node 3", onClick: () => alert("Node 3 Clicked") },
+ {
+ label: "Node 1",
+ onClick: () => {
+ // Use a toast notification or custom feedback component
+ }
+ },
// Similar changes for other tabs
];
return (
- <div style={{ padding: "20px", backgroundColor: "#000", color: "#fff" }}>
+ <div className="story-container">
<Tabs tabs={tabs} />
</div>
);
},
};
Committable suggestion skipped: line range outside the PR's diff.
type Tab = { | ||
label: string; | ||
onClick: () => void; | ||
}; | ||
|
||
type TabsProps = { | ||
tabs: Tab[]; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance type definitions for better accessibility and flexibility.
The current type definitions could be improved to support:
- ARIA labels for better accessibility
- Optional IDs for tabs
- Disabled state for tabs
type Tab = {
label: string;
+ id?: string;
+ ariaLabel?: string;
+ disabled?: boolean;
onClick: () => void;
};
type TabsProps = {
tabs: Tab[];
+ ariaLabel?: string;
+ defaultActiveIndex?: number;
};
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
type Tab = { | |
label: string; | |
onClick: () => void; | |
}; | |
type TabsProps = { | |
tabs: Tab[]; | |
}; | |
type Tab = { | |
label: string; | |
id?: string; | |
ariaLabel?: string; | |
disabled?: boolean; | |
onClick: () => void; | |
}; | |
type TabsProps = { | |
tabs: Tab[]; | |
ariaLabel?: string; | |
defaultActiveIndex?: number; | |
}; |
return ( | ||
<div className="tabs-container"> | ||
<div className="tabs-list" role="tablist"> | ||
{tabs.map((tab, index) => ( | ||
<div | ||
key={index} | ||
role="tab" | ||
tabIndex={activeIndex === index ? 0 : -1} | ||
aria-selected={activeIndex === index} | ||
className={`tab-item ${activeIndex === index ? "active" : ""}`} | ||
onClick={() => { | ||
tab.onClick(); | ||
setActiveIndex(index); | ||
}} | ||
onKeyDown={(e) => handleKeyDown(e, index)} | ||
> | ||
{tab.label} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance accessibility and semantic markup.
The render method should:
- Use
<button>
elements for better semantics - Add additional ARIA attributes
- Support disabled state styling
return (
- <div className="tabs-container">
+ <div
+ className="tabs-container"
+ aria-label={ariaLabel}
+ >
<div className="tabs-list" role="tablist">
{tabs.map((tab, index) => (
- <div
+ <button
key={index}
+ id={tab.id}
role="tab"
tabIndex={activeIndex === index ? 0 : -1}
aria-selected={activeIndex === index}
+ aria-label={tab.ariaLabel}
+ aria-controls={`${tab.id}-panel`}
+ disabled={tab.disabled}
className={`tab-item ${
activeIndex === index ? "active" : ""
+ tab.disabled ? "disabled" : ""
}`}
onClick={() => {
+ if (tab.disabled) return;
tab.onClick();
setActiveIndex(index);
}}
onKeyDown={(e) => handleKeyDown(e, index)}
>
{tab.label}
- </div>
+ </button>
))}
</div>
</div>
);
Committable suggestion skipped: line range outside the PR's diff.
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>, index: number) => { | ||
if (event.key === "ArrowRight") { | ||
setActiveIndex((prevIndex) => (prevIndex + 1) % tabs.length); | ||
} else if (event.key === "ArrowLeft") { | ||
setActiveIndex((prevIndex) => (prevIndex - 1 + tabs.length) % tabs.length); | ||
} else if (event.key === "Enter" || event.key === " ") { | ||
event.preventDefault(); | ||
tabs[index].onClick(); | ||
setActiveIndex(index); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance keyboard navigation support.
The keyboard handler should:
- Support Home/End keys for first/last tab navigation
- Prevent default on arrow keys to avoid page scrolling
- Skip disabled tabs if implemented
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>, index: number) => {
+ const { key } = event;
+ let newIndex = index;
- if (event.key === "ArrowRight") {
+ if (key === "ArrowRight" || key === "ArrowLeft") {
event.preventDefault();
- setActiveIndex((prevIndex) => (prevIndex + 1) % tabs.length);
+ newIndex = key === "ArrowRight"
+ ? (index + 1) % tabs.length
+ : (index - 1 + tabs.length) % tabs.length;
- } else if (event.key === "ArrowLeft") {
- setActiveIndex((prevIndex) => (prevIndex - 1 + tabs.length) % tabs.length);
- } else if (event.key === "Enter" || event.key === " ") {
+ } else if (key === "Home" || key === "End") {
+ event.preventDefault();
+ newIndex = key === "Home" ? 0 : tabs.length - 1;
+ } else if (key === "Enter" || key === " ") {
event.preventDefault();
- tabs[index].onClick();
- setActiveIndex(index);
+ if (!tabs[index].disabled) {
+ tabs[index].onClick();
+ setActiveIndex(index);
+ }
+ return;
}
+
+ // Find next non-disabled tab
+ while (tabs[newIndex]?.disabled) {
+ newIndex = key === "ArrowRight" || key === "Home"
+ ? (newIndex + 1) % tabs.length
+ : (newIndex - 1 + tabs.length) % tabs.length;
+ }
+
+ setActiveIndex(newIndex);
};
Committable suggestion skipped: line range outside the PR's diff.
Summary by CodeRabbit
New Features
Tabs
component in Storybook:DefaultTabs
andTabsWithAlerts
.Style
Documentation
Tabs
component and its variations.