-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
add solution #1319
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
|
||
export const App = () => { | ||
const [activeTabId, setActiveTabId] = useState(tabs[0].id); | ||
const selectedTab = getSelectedTabTitle(tabs, activeTabId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
/> | ||
</div> | ||
</div> | ||
); | ||
); | ||
}; |
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> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Tab'; |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
}; | ||
|
||
function checkAvailiableTabId(id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a typo in the function name |
||
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)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this commented-out line as it references the old function name |
||
onTabSelected={onTabSelected} | ||
/> | ||
))} | ||
</ul> | ||
</div> | ||
|
||
<div className="block" data-cy="TabContent"> | ||
{selectedTab.content} | ||
</div> | ||
</div> | ||
); | ||
}; |
There was a problem hiding this comment.
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 returntab.title
instead of the whole tab object.