Tab Switcher
Create a React component named TabSwitcher that displays multiple tabs and allows users to switch between them. Each tab should display corresponding content when selected.
Features
- Displays multiple tab buttons with clear labels.
- Highlights the currently active tab visually.
- Updates the displayed content dynamically when a user clicks a tab.
- Maintains the active tab state internally using React hooks.
- Provides meaningful attributes to facilitate automated testing.
π§Ύ Functionality Requirements
β Initial State
- The component should load with the "Home" tab active by default.
- The content area should display:
"Welcome to the Home tab!"
β Tabs
The following tabs must be rendered as horizontal buttons:
- Home: Displays
"Welcome to the Home tab!" - Profile: Displays
"This is your Profile." - Settings: Displays
"Adjust your Settings here."
β Switching Tabs
- Clicking on a tab button switches the active tab.
- The content area updates immediately to reflect the selected tabβs content.
- The active tab button should have a distinct visual style (highlighted).
β Active Tab Styling
- The active tab button should:
- Use a different background color and text color.
- Non-active tabs should:
- Have default styling.
- Include a subtle hover effect.
β Repeated Clicks on Active Tab
- Clicking an already active tab:
- Should not change the state or content.
- Should retain the current content and style.
π Edge Cases & Constraints
- Gracefully handle:
- Unexpected tab IDs.
- Empty or malformed tab arrays.
- Must not cause page reloads or unexpected side effects.
- Must maintain state properly when tabs are clicked rapidly.
- Styling should clearly distinguish between active and inactive tabs.
- Component should be easily extendable to support additional tabs.
Examples of Input and Output
-
Initial render
β€ Content:"Welcome to the Home tab!" -
Click on "Profile" tab
β€ Content:"This is your Profile." -
Click on "Settings" tab
β€ Content:"Adjust your Settings here." -
Click "Settings" tab again
β€ Content remains"Adjust your Settings here."
β Testing Requirements
- Verify all tab buttons render correctly.
- Verify default active tab is Home.
- Verify clicking each tab updates the content correctly.
- Verify the active tab button has the proper active styling.
- Verify clicking the same tab multiple times retains content and active style.
- Use
data-testidattributes to reliably select elements during tests.
π·οΈ Data Test IDs for Testing
tab-button-homeβ for the Home tab buttontab-button-profileβ for the Profile tab buttontab-button-settingsβ for the Settings tab buttontab-contentβ for the content display area
π‘ Solution Approach
- Use React functional components with the
useStatehook to track the currently active tab. - Store tab data (
id,label,content) in an array for easy mapping and rendering. - Render tab buttons dynamically using
Array.map(), assigning theactiveclass conditionally based on the active tab state. - Handle button clicks to update the active tab state, causing a re-render of the content.
- Display content by finding the active tab object from the array.
- Add meaningful
data-testidattributes to support reliable automated testing. - Style active/inactive tabs distinctly using CSS classes.
- Write tests using @testing-library/react to simulate user interaction and ensure UI updates correctly.
- Use semantic HTML and ensure usability for keyboard navigation and screen readers.
Companies:
Solve Similar questions π₯
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
