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

Develop #1773

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Develop #1773

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://asushko25.github.io/react_tabs-with-router/) and add it to the PR description.

29 changes: 16 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"classnames": "^2.5.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.25.1",
"react-router-dom": "^6.28.0",
"react-transition-group": "^4.4.5"
},
"devDependencies": {
Expand Down
69 changes: 26 additions & 43 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,38 @@ 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, Outlet } from 'react-router-dom';
import classNames from 'classnames';

const getLinkClass = ({ isActive }: { isActive: boolean }) =>
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>
<html className="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>

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

<div className="block" data-cy="TabContent">
Please select a tab
</nav>
<div className="section">
<div className="container">
<Outlet />
</div>
</div>
</div>
</html>
</>
);
22 changes: 22 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HashRouter, Route, Routes, Navigate } from 'react-router-dom';
import { App } from './App';
import { Home } from './pages/Home';
import { Tabs } from './pages/Tabs';
import { NotFound } from './pages/NotFound';

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

<Route path="tabs">
<Route path=":tabId?" element={<Tabs />} />
</Route>

<Route path="*" element={<NotFound />} />
</Route>
</Routes>
</HashRouter>
);
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 './Root';

createRoot(document.getElementById('root') as HTMLElement).render(
<HashRouter>
<App />
</HashRouter>,
);
createRoot(document.getElementById('root') as HTMLElement).render(<Root />);
5 changes: 5 additions & 0 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const Home = () => (
<div className="container">
<h1 className="title">Home page</h1>
</div>
);
5 changes: 5 additions & 0 deletions src/pages/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const NotFound = () => (
<div className="container">
<h1 className="title">Page not found</h1>
</div>
);
39 changes: 39 additions & 0 deletions src/pages/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import classNames from 'classnames';
import { useState } from 'react';
import { NavLink } from 'react-router-dom';

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 Tabs = () => {
const [activeTab, setActiveTab] = useState('');

return (
<div className="container">
<h1 className="title">Tabs page</h1>

<div className="tabs is-boxed">
<ul>
{tabs.map(tab => (
<li
key={tab.id}
data-cy="Tab"
className={classNames('', { 'is-active': activeTab === tab.id })}
onClick={() => setActiveTab(tab.id)}
>
<NavLink to={`/tabs/${tab.id}`}>{tab.title}</NavLink>
</li>
))}
</ul>
</div>

<div className="block" data-cy="TabContent">
{tabs.find(tab => tab.id === activeTab)?.content ||
'Please select a tab'}
</div>
</div>
);
};