-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabs.jsx
31 lines (28 loc) · 1.41 KB
/
tabs.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { useState } from "react"
import "./tabs.css"
export default function Tabs({ tabsContent, onChange }) {
const [currentTabIndex, setCurrentTabIndex] = useState(0)
function handleOnclick(getCurrentIndex) {
/* here the function recieves the index of selected tab as (getCurrentIndex) */
setCurrentTabIndex(getCurrentIndex) /* then we update the index of current tab by setCurrentTabIndex(getCurrentTabIndex) which is amount of the index we just sent by clicking */
onChange(getCurrentIndex) /* here onChange tells the parrent component that the index has changed and the parent component changes the content of the tab. */
}
return (
<div className="wrapper">
<div className="heading">
{
tabsContent.map((tabItem, index) =>
<div className={currentTabIndex === index ? 'active-tab' : ''} onClick={()=> handleOnclick(index)} /* ()=> makes the function to be called when it is clicked... if we dont use () => it calls the function for all 3 tabs and gives us error */ /* ()=> handleOnClick(index) this index will send the index of selected tab to the function */
key={tabItem.label}>
<button className="label">{tabItem.label}</button>
</div>)
}
</div>
<div className="content">
{
tabsContent[currentTabIndex] && tabsContent[currentTabIndex].content
}
</div>
</div>
)
}