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 #1775

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ or [React Tabs](https://github.com/mate-academy/react_tabs#react-tabs).
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_tabs-with-router/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://imondok03.github.io/react_tabs-with-router/) and add it to the PR description.

79 changes: 34 additions & 45 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,45 @@
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';
import { Outlet, NavLink, useLocation, Navigate } from 'react-router-dom';
import classNames from 'classnames';
import React from 'react';

// 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' },
// ];
export const App = () => {
const getLinkClass = ({ isActive }: { isActive: boolean }) => {
return classNames('navbar-item', { 'is-active': isActive });
};

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>
const { pathname } = useLocation();

<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>
if (pathname === '/home') {
return <Navigate to="/" replace />;
}

<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>
return (
<>
{/* 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">
<NavLink to="/" className={getLinkClass}>
Home
</NavLink>
<NavLink to="/tabs" className={getLinkClass}>
Tabs
</NavLink>
</div>
</div>
</nav>

<div className="block" data-cy="TabContent">
Please select a tab
<div className="section">
<div className="container">
<Outlet />
</div>
</div>
</div>
</>
);
</>
);
};
5 changes: 5 additions & 0 deletions src/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export const HomePage = () => {
return <h1 className="title">Home page</h1>;
};
20 changes: 20 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { HashRouter, Route, Routes } from 'react-router-dom';
import { App } from './App';
import React from 'react';
import { TabsPage } from './TabsPage';
import { HomePage } from './HomePage';

export const Root = () => (
<HashRouter>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="tabs">
<Route index element={<TabsPage />} />
<Route path=":tabId" element={<TabsPage />} />
</Route>
<Route path="*" element={<h1 className="title">Page not found</h1>} />
</Route>
</Routes>
</HashRouter>
);
48 changes: 48 additions & 0 deletions src/TabsPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import classNames from 'classnames';
import React from 'react';
import { useMemo } from 'react';
import { Link, Outlet, useParams } from 'react-router-dom';

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

const tabs = useMemo(() => {
return [
{ 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 selectedTab = tabs.find(tab => tab.id === tabId);

return (
<>
<h1 className="title">Tabs page</h1>
<div className="tabs is-boxed">
<ul>
{tabs.map(item => {
const { id, title } = item;

return (
<li
data-cy="Tab"
key={id}
className={classNames({
'is-active': id === tabId,
})}
>
<Link to={`/tabs/${id}`}> {title} </Link>
</li>
);
})}
</ul>
</div>

<div className="block" data-cy="TabContent">
{selectedTab ? selectedTab.content : 'Please select a tab'}
</div>
<Outlet />
</>
);
};
10 changes: 3 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { createRoot } from 'react-dom/client';
import { HashRouter } from 'react-router-dom';
import { App } from './App';
import { Root } from './Root';
import React from 'react';

createRoot(document.getElementById('root') as HTMLElement).render(
<HashRouter>
<App />
</HashRouter>,
);
createRoot(document.getElementById('root') as HTMLElement).render(<Root />);