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

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

48 changes: 4 additions & 44 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
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 { Navbar } from './components/Navbar';
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>

<Navbar />
<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/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
HashRouter as Router,
Route,
Routes,
Navigate,
} from 'react-router-dom';
import { TabProvider } from './components/TabContext';
import { TabsPage } from './components/TabsPage';
import { PageNotFount } from './components/PageNotFound';
import { App } from './App';
import { HomePage } from './components/HomePage';

export const Root = () => {
return (
<Router>
<TabProvider>
<Routes>
<Route path="/" element={<App />}>
<Route path="home" element={<Navigate to="/" replace />} />
<Route index element={<HomePage />} />
<Route path="tabs/:tabId?" element={<TabsPage />} />
<Route path="*" element={<PageNotFount />} />
</Route>
</Routes>
</TabProvider>
</Router>
);
};
3 changes: 3 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HomePage = () => {
return <h1 className="title">Home page</h1>;
};
20 changes: 20 additions & 0 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NavLink } from 'react-router-dom';
import { getLinkClass } from '../services/services';

export const Navbar = () => (
<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>
);
3 changes: 3 additions & 0 deletions src/components/PageNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const PageNotFount = () => {
return <h1 className="title">Page not found</h1>;
};
35 changes: 35 additions & 0 deletions src/components/TabContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useContext } from 'react';
import { Tab } from '../types/Tab';

type TabContextType = {
tabs: Tab[];
};

export const TabContext = React.createContext<TabContextType | undefined>(
undefined,
);

type Props = {
children: React.ReactNode;
};

export const TabProvider: React.FC<Props> = ({ children }) => {
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 value = { tabs };

return <TabContext.Provider value={value}>{children}</TabContext.Provider>;
};

export const useTabContext = () => {
const context = useContext(TabContext);

if (!context) {
throw new Error('useTodoContext must be used within a TodoProvider');
}

return context;
};
20 changes: 20 additions & 0 deletions src/components/TabPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import classNames from 'classnames';
import { Link } from 'react-router-dom';
import { Tab } from '../types/Tab';

type Props = {
tab: Tab;
selectedTab?: Tab;
};

export const TabPage: React.FC<Props> = ({ tab, selectedTab }) => (
<li
key={tab.id}
data-cy="Tab"
className={classNames({
'is-active': selectedTab?.id === tab.id,
})}
>
<Link to={`/tabs/${tab.id}`}>{tab.title}</Link>
</li>
);
28 changes: 28 additions & 0 deletions src/components/TabsList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useParams } from 'react-router-dom';
import { useTabContext } from './TabContext';
import { TabPage } from './TabPage';

type Props = {};

export const TabsList: React.FC<Props> = () => {
const { tabs } = useTabContext();
const { tabId } = useParams();
const selectedTab = tabs.find(tab => tab.id === tabId);
const defaultContent = 'Please select a tab';

return (
<>
<div className="tabs is-boxed">
<ul>
{tabs.map(tab => (
<TabPage key={tab.id} tab={tab} selectedTab={selectedTab} />
))}
</ul>
</div>

<div className="block" data-cy="TabContent">
{selectedTab?.content || defaultContent}
</div>
</>
);
};
10 changes: 10 additions & 0 deletions src/components/TabsPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Outlet } from 'react-router-dom';
import { TabsList } from './TabsList';

export const TabsPage = () => (
<>
<h1 className="title">Tabs page</h1>
<TabsList />
<Outlet />
</>
);
11 changes: 4 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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>,
);
const container = document.getElementById('root') as HTMLElement;

createRoot(container).render(<Root />);
4 changes: 4 additions & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import classNames from 'classnames';

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