Skip to content
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

Solution #1763

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 4 additions & 43 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,16 @@
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 { Nav } from './components/Nav/Nav';
import { Outlet } from 'react-router-dom';

export const App = () => (
<>
{/* Also requires <html class="has-navbar-fixed-top"> */}
<nav
className="navbar is-light is-fixed-top is-mobile has-shadow"
data-cy="Nav"
>
<div className="container">
<div className="navbar-brand">
<a href="/" className="navbar-item is-active">
Home
</a>
<a href="/tabs" className="navbar-item">
Tabs
</a>
</div>
</div>
</nav>

<Nav />
<div className="section">
<div className="container">
<h1 className="title">Home page</h1>
<h1 className="title">Tabs page</h1>
<h1 className="title">Page not found</h1>

<div className="tabs is-boxed">
<ul>
<li data-cy="Tab" className="is-active">
<a href="#/">Tab 1</a>
</li>
<li data-cy="Tab">
<a href="#/">Tab 2</a>
</li>
<li data-cy="Tab">
<a href="#/">Tab 3</a>
</li>
</ul>
</div>

<div className="block" data-cy="TabContent">
Please select a tab
</div>
<Outlet />
</div>
</div>
</>
Expand Down
28 changes: 28 additions & 0 deletions src/Roots.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { HashRouter, Navigate } from 'react-router-dom';
import { Routes, Route } from 'react-router-dom';
import { lazy, Suspense } from 'react';
import { App } from './App';

const HomePage = lazy(() => import('./pages/HomePage/HomePage'));
const TabsPage = lazy(() => import('./pages/TabsPage/TabsPage'));
const NotFoundPage = lazy(() => import('./pages/NotFoundPage/NotFoundPage'));

export const Root = () => (
<HashRouter>
<Suspense>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="/home" element={<Navigate to="/" />} />

<Route path="tabs" element={<TabsPage />}>
<Route index element={<TabsPage />} />
<Route path=":tabId" element={<TabsPage />} />
</Route>

<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
</Suspense>
</HashRouter>
);
26 changes: 26 additions & 0 deletions src/components/Nav/Nav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NavLink } from 'react-router-dom';
import cn from 'classnames';
import { navLinks } from '../../constants/navLinks';

export const Nav = () => (
<nav
className="navbar is-light is-fixed-top is-mobile has-shadow"
data-cy="Nav"
>
<div className="container">
<div className="navbar-brand">
{navLinks.map(({ path, title, id }) => (
<NavLink
key={id}
to={path}
className={({ isActive }) =>
cn('navbar-item', { 'is-active': isActive })
}
>
{title}
</NavLink>
))}
</div>
</div>
</nav>
);
38 changes: 38 additions & 0 deletions src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import cn from 'classnames';
import { tabs } from '../../constants/tabs';
import { Link } from 'react-router-dom';
import { FC } from 'react';

interface TabsProps {
tabId: string | undefined;
}

export const Tabs: FC<TabsProps> = ({ tabId }) => {
const activeTab = tabs.find(({ id }) => id === tabId);

return (
<div data-cy="TabsComponent">
<div className="tabs is-boxed">
<ul>
{tabs.map(({ id, title }) => (
<li
className={cn({
'is-active': id === tabId,
})}
data-cy="Tab"
key={id}
>
<Link to={id} data-cy="TabLink">
{title}
</Link>
</li>
))}
</ul>
</div>

<div className="block" data-cy="TabContent">
{!activeTab ? 'Please select a tab' : activeTab.content}
</div>
</div>
);
};
12 changes: 12 additions & 0 deletions src/constants/navLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const navLinks = [
{
id: '0',
path: '/',
title: 'Home',
},
{
id: '1',
path: '/tabs',
title: 'Tabs',
},
];
5 changes: 5 additions & 0 deletions src/constants/tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export 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' },
];
9 changes: 2 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { createRoot } from 'react-dom/client';
import { HashRouter } from 'react-router-dom';
import { App } from './App';
import { Root } from './Roots';

createRoot(document.getElementById('root') as HTMLElement).render(
<HashRouter>
<App />
</HashRouter>,
);
createRoot(document.getElementById('root') as HTMLElement).render(<Root />);
3 changes: 3 additions & 0 deletions src/pages/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const HomePage = () => <h1 className="title">Home page</h1>;

export default HomePage;
3 changes: 3 additions & 0 deletions src/pages/NotFoundPage/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const NotFoundPage = () => <h1 className="title">Page not found</h1>;

export default NotFoundPage;
15 changes: 15 additions & 0 deletions src/pages/TabsPage/TabsPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useParams } from 'react-router-dom';
import { Tabs } from '../../components/Tabs/Tabs';

const TabsPage = () => {
const { tabId } = useParams();

return (
<>
<h1 className="title">Tabs page</h1>
<Tabs tabId={tabId} />
</>
);
};

export default TabsPage;
Loading