Created
January 11, 2019 10:51
-
-
Save daniele-zurico/87bf0b4f7c154dd3dad91060fb9b5c1a to your computer and use it in GitHub Desktop.
CustomTabs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { createContext, useState, useContext } from "react"; | |
const context = createContext({ | |
activeTabId: "a", | |
changeTab: (id: string) => {} | |
}); | |
const Tab = ({ id, children }: any) => { | |
const tab = useContext(context); | |
return <div onClick={() => tab.changeTab(id)}>{children}</div>; | |
}; | |
const TabPanel = ({ whenActive, children }: any) => { | |
const tab = useContext(context); | |
return tab.activeTabId === whenActive ? children : null; | |
}; | |
const TabSwitcher = ({ children }: any) => { | |
const [activeTabId, setActiveTab] = useState<string>("a"); | |
const changeTab = (newTabId: any) => { | |
setActiveTab(newTabId); | |
}; | |
return ( | |
<context.Provider | |
value={{ | |
activeTabId: activeTabId, | |
changeTab: changeTab | |
}} | |
> | |
{children} | |
</context.Provider> | |
); | |
}; | |
export default TabSwitcher; | |
export { Tab, TabPanel }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment