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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -31,5 +31,5 @@ The `Tabs` page should also show a `Tabs` component implemented in [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://artyomwhite.github.io/react_tabs-with-router/) and add it to the PR description.

24 changes: 12 additions & 12 deletions package-lock.json

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

69 changes: 21 additions & 48 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';
import { Navigate, Route, Routes } from 'react-router-dom';
import { Navigation } from './components/Nav';
import { HomePage } from './components/pagesRout/Home';
import { ErrorPage } from './components/pagesRout/NotFound';
import { TabsPage } from './components/pagesRout/Tabs';

// 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 = () => (
<>
{/* 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>

<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>
</div>
</div>
</>
);
export const App = () => {
return (
<>
<Navigation />
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="tabs">
<Route index element={<TabsPage />} />
<Route path=":tabId" element={<TabsPage />} />
</Route>
<Route path="*" element={<ErrorPage />} />
<Route path="/home" element={<Navigate to="/" />} />
</Routes>
</>
);
};
22 changes: 22 additions & 0 deletions src/components/Nav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

const getActiveLink = ({ isActive }: { isActive: boolean }) => classNames(
'navbar-item', { 'is-active': isActive },
);

export const Navigation = () => {
return (
<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={getActiveLink}>Home</NavLink>
<NavLink to="/tabs" className={getActiveLink}>Tabs</NavLink>
</div>
</div>
</nav>
);
};
9 changes: 9 additions & 0 deletions src/components/pagesRout/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const HomePage = () => {
return (
<div className="section">
<div className="container">
<h1 className="title">Home page</h1>
</div>
</div>
);
};
9 changes: 9 additions & 0 deletions src/components/pagesRout/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const ErrorPage = () => {
return (
<div className="section">
<div className="container">
<h1 className="title">Page not found</h1>
</div>
</div>
);
};
45 changes: 45 additions & 0 deletions src/components/pagesRout/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import classNames from 'classnames';
import { Link, useParams } 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 TabsPage = () => {
const { tabId } = useParams();
const selectedTab = tabs.find(tab => tab.id === tabId);

return (

<div className="section">
<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(null, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
classNames(null, {
classNames({

'is-active': tab.id === tabId,
})
}
>
<Link to={`../${tab.id}`}>{tab.title}</Link>
</li>
))}
</ul>
</div>

<div className="block" data-cy="TabContent">
{selectedTab?.content || 'Please select a tab'}
</div>

</div>

</div>
);
};