diff --git a/src/App.tsx b/src/App.tsx index f587aebf0..4476bfa37 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,55 +1,66 @@ +import React, { Suspense } from 'react'; import 'bulma/css/bulma.css'; import '@fortawesome/fontawesome-free/css/all.css'; import './App.scss'; -// const tabs = [ -// { id: 'tab-1', title: 'Tab 1', content: 'Some text 1' }, -// { id: 'tab-2', title: 'Tab 2', content: 'Some text 2' }, -// { id: 'tab-3', title: 'Tab 3', content: 'Some text 3' }, -// ]; +import { NavLink, Navigate, Route, Routes } from 'react-router-dom'; +import cn from 'classnames'; -export const App = () => ( +const HomePage = () => { + return

Home page

; +}; + +const NotFoundPage = () => { + return

Page not found

; +}; + +const TabsPage = React.lazy(() => + import('./components/TabsPage').then(module => ({ + default: module.TabsPage, + })), +); + +export const App: React.FC = () => ( <> - {/* Also requires */}
-

Home page

-

Tabs page

-

Page not found

- -
- -
- -
- Please select a tab -
+ Loading...

}> + + } /> + + } /> + } /> + + } /> + } /> + +
diff --git a/src/components/Tabs.tsx b/src/components/Tabs.tsx new file mode 100644 index 000000000..664429bcb --- /dev/null +++ b/src/components/Tabs.tsx @@ -0,0 +1,29 @@ +import { FC } from 'react'; +import { Link } from 'react-router-dom'; +import cn from 'classnames'; +import { useTabs } from '../providers/TabsProvider'; + +export const Tabs: FC = () => { + const { tabs, activeTab } = useTabs(); + + return ( +
+
+ +
+
+ {activeTab ? activeTab.content : 'Please select a tab'} +
+
+ ); +}; diff --git a/src/components/TabsPage.tsx b/src/components/TabsPage.tsx new file mode 100644 index 000000000..14ccb32a2 --- /dev/null +++ b/src/components/TabsPage.tsx @@ -0,0 +1,10 @@ +import { FC } from 'react'; +import { TabsProvider } from '../providers/TabsProvider'; +import { Tabs } from './Tabs'; + +export const TabsPage: FC = () => ( + +

Tabs page

+ +
+); diff --git a/src/providers/TabsProvider.tsx b/src/providers/TabsProvider.tsx new file mode 100644 index 000000000..289c03c60 --- /dev/null +++ b/src/providers/TabsProvider.tsx @@ -0,0 +1,40 @@ +import React, { createContext, useContext, PropsWithChildren } from 'react'; +import { useParams } from 'react-router-dom'; +import { Tab } from '../types/Tab'; + +interface TabsContextI { + tabs: Tab[]; + activeTab?: Tab; +} + +const TABS: Tab[] = [ + { id: 'tab-1', title: 'Tab 1', content: 'Some text 1' }, + { id: 'tab-2', title: 'Tab 2', content: 'Some text 2' }, + { id: 'tab-3', title: 'Tab 3', content: 'Some text 3' }, +]; + +const TabsContext = createContext({ + tabs: TABS, + activeTab: undefined, +}); + +export const TabsProvider: React.FC = ({ children }) => { + const { tabId } = useParams<{ tabId?: string }>(); + const activeTab = TABS.find(tab => tab.id === tabId); + + return ( + + {children} + + ); +}; + +export const useTabs = (): TabsContextI => { + const context = useContext(TabsContext); + + if (!context) { + throw new Error('useTabs must be used within a TabsProvider'); + } + + return context; +};