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

add solution #1319

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
68 changes: 35 additions & 33 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
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>

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

<li data-cy="Tab">
<a href="#tab-2" data-cy="TabLink">
Tab 2
</a>
</li>

<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>
function getSelectedTabTitle(tabsList, tabId) {
return tabsList.find(tab => tab.id === tabId);

Choose a reason for hiding this comment

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

The getSelectedTabTitle function should return the title of the tab, not the entire tab object. Modify the function to return tab.title instead of the whole tab object.

}

export const App = () => {
const [activeTabId, setActiveTabId] = useState(tabs[0].id);
const selectedTab = getSelectedTabTitle(tabs, activeTabId);

Choose a reason for hiding this comment

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

The selectedTab variable is being used here, but its necessity was questioned in the previous review. Please ensure that this variable is required according to the task description, or remove it if it's not needed.


function setNewActiveTab(tabId) {
const isInTabs = tabs.find(tab => tab.id === tabId);

if (isInTabs === undefined) {
setActiveTabId(tabs[0].id);

return;
}

setActiveTabId(tabId);
}

return (
<div className="section">
<h1 className="title">Selected tab is {selectedTab.title}</h1>

<Tabs
tabs={tabs}
activeTabId={activeTabId}
onTabSelected={tabId => setNewActiveTab(tabId)}
selectedTab={selectedTab}

Choose a reason for hiding this comment

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

The selectedTab prop is not mentioned in the task description as a required prop for the Tabs component. Ensure that this prop is necessary, and consider removing it if it's not needed.

/>
</div>
</div>
);
);
};
22 changes: 22 additions & 0 deletions src/components/Tab/Tab.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import classNames from 'classnames';

export const Tab = ({ tab, activeTabId, onTabSelected }) => {
return (
<li
className={classNames({ 'is-active': activeTabId === tab.id })}
data-cy="Tab"
>
<a
href={`#${tab.id}`}
onClick={() => {
if (activeTabId !== tab.id) {
onTabSelected(tab.id);
}
}}
data-cy="TabLink"
>
{tab.title}
</a>
</li>
);
};
1 change: 1 addition & 0 deletions src/components/Tab/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Tab';
45 changes: 44 additions & 1 deletion src/components/Tabs/Tabs.jsx
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
export const Tabs = () => {};
import { Tab } from '../Tab';

export const Tabs = ({ tabs, activeTabId, onTabSelected }) => {
const getSelectedTabTitle = (tabsList, tabId) => {
return tabsList.find(tab => tab.id === tabId);

Choose a reason for hiding this comment

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

The getSelectedTabTitle function should return the title of the tab, not the entire tab object. Modify the function to return tab.title instead of the whole tab object.

};

function checkAvailiableTabId(id) {

Choose a reason for hiding this comment

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

There's a typo in the function name checkAvailiableTabId. Consider renaming it to checkAvailableTabId for correct spelling.

const isInTabs = tabs.find(tab => tab.id === id);

if (isInTabs === undefined) {
return tabs[0].id;
}

return id;
}

const selectedTab = getSelectedTabTitle(
tabs,
checkAvailiableTabId(activeTabId),
);

return (
<div data-cy="TabsComponent">
<div className="tabs is-boxed">
<ul>
{tabs.map(tab => (
<Tab
key={tab.id}
tab={tab}
activeTabId={checkAvailiableTabId(activeTabId)}
// activeTabId={checkAvailiableTabId(activeTabId)}

Choose a reason for hiding this comment

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

Remove this commented-out line as it references the old function name checkAvailiableTabId, which has been corrected to checkAvailableTabId. Keeping the code clean and free of unnecessary comments is a good practice.

onTabSelected={onTabSelected}
/>
))}
</ul>
</div>

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