RoutedTabs is now a functional component

This commit is contained in:
Alex Corey
2019-04-17 13:47:23 -04:00
parent 76a7a76e81
commit ca6153c955
4 changed files with 83 additions and 114 deletions

View File

@@ -1,55 +1,44 @@
import React from 'react';
import {
withRouter
} from 'react-router-dom';
import {
Tab,
Tabs
} from '@patternfly/react-core';
class RoutedTabs extends React.Component {
constructor (props) {
super(props);
export default function RoutedTabs (props) {
const { history, tabsArray } = props;
const getActiveTabId = () => {
if (history && history.location.pathname) {
const matchTab = tabsArray.find(selectedTab => selectedTab.link
=== history.location.pathname);
return matchTab.id;
}
return 0;
};
this.handleTabSelect = this.handleTabSelect.bind(this);
function handleTabSelect (event, eventKey) {
if (history && history.location.pathname) {
const tab = tabsArray.find(tabElement => tabElement.id === eventKey);
history.push(tab.link);
}
}
getActiveTabId () {
const { history, tabsArray } = this.props;
const matchTab = tabsArray.find(selectedTab => selectedTab.link === history.location.pathname);
return matchTab ? matchTab.id : 0;
}
handleTabSelect (event, eventKey) {
const { history, tabsArray } = this.props;
const tab = tabsArray.find(tabElement => tabElement.id === eventKey);
history.push(tab.link);
}
render () {
const { tabsArray } = this.props;
return (
<Tabs
activeKey={this.getActiveTabId()}
onSelect={this.handleTabSelect}
>
{tabsArray.map(tabElement => (
<Tab
className={`${tabElement.name}`}
aria-label={`${tabElement.name}`}
eventKey={tabElement.id}
key={tabElement.id}
link={tabElement.link}
title={tabElement.name}
/>
))}
</Tabs>
);
}
return (
<Tabs
activeKey={getActiveTabId()}
onSelect={handleTabSelect}
>
{tabsArray.map(tabElement => (
<Tab
className={`${tabElement.name}`}
aria-label={`${tabElement.name}`}
eventKey={tabElement.id}
key={tabElement.id}
link={tabElement.link}
title={tabElement.name}
/>
))}
</Tabs>
);
}
export { RoutedTabs as _RoutedTabs };
export default withRouter(RoutedTabs);