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

Open
wants to merge 3 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
45 changes: 15 additions & 30 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
import React from 'react';
import React, { useState } from 'react';
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';
import { Tabs } from './components/Tabs/Tabs';

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' },
];

export const App = () => (
<div className="section">
<h1 className="title">Selected tab is Tab 1</h1>
export const App = () => {
const [activeTabId, setActiveTabId] = useState(tabs[0].id);

<div data-cy="TabsComponent">
<div className="tabs is-boxed">
<ul>
<li className="is-active" data-cy="Tab">
<a href="#tab-1" data-cy="TabLink">
Tab 1
</a>
</li>
const activeTab = tabs.find(el => el.id === activeTabId);

<li data-cy="Tab">
<a href="#tab-2" data-cy="TabLink">
Tab 2
</a>
</li>
return (
<div className="section">
<h1 className="title">Selected tab is {activeTab.title}</h1>

<li data-cy="Tab">
<a href="#tab-3" data-cy="TabLink">
Tab 3
</a>
</li>
</ul>
</div>

<div className="block" data-cy="TabContent">
Some text 1
</div>
<Tabs
tabs={tabs}
activeTabId={activeTabId}
onTabSelected={setActiveTabId}
/>
</div>
</div>
);
);
};
43 changes: 42 additions & 1 deletion src/components/Tabs/Tabs.jsx
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
export const Tabs = () => {};
export const Tabs = ({ tabs, activeTabId, onTabSelected }) => {
const selectedTab = id => {
if (id !== activeTabId) {
onTabSelected(id);
}
};

const active =
tabs.find(el => el.id === activeTabId) === undefined
? tabs[0].id
: activeTabId;

const { content } = tabs.find(el => el.id === active);

return (
<div data-cy="TabsComponent">
<div className="tabs is-boxed">
<ul>
{tabs.map(tab => (
<li
className={tab.id === active ? 'is-active' : ''}
data-cy="Tab"
key={tab.id}
>
<a
href={`#${tab.id}`}
data-cy="TabLink"
onClick={() => selectedTab(tab.id)}
>
{tab.title}
</a>
</li>
))}
</ul>
</div>

<div className="block" data-cy="TabContent">
{content}
</div>
</div>
);
};
Loading