Skip to content

Commit

Permalink
Fix code readability, use PureComponent over Component
Browse files Browse the repository at this point in the history
  • Loading branch information
alextsg committed Aug 22, 2018
1 parent 01c0c98 commit d7d141c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class PageContainerHeader extends Component {
<div className={
classnames(
'page-container__header',
tabs && 'page-container__header--no-padding-bottom'
{ 'page-container__header--no-padding-bottom': Boolean(tabs) }
)
}>

Expand Down
45 changes: 15 additions & 30 deletions ui/app/components/page-container/page-container.component.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'

import PageContainerHeader from './page-container-header'
import PageContainerFooter from './page-container-footer'

export default class PageContainer extends Component {
export default class PageContainer extends PureComponent {
static propTypes = {
// PageContainerHeader props
backButtonString: PropTypes.string,
Expand All @@ -28,25 +28,11 @@ export default class PageContainer extends Component {
}

state = {
activeTabIndex: 0,
activeTabIndex: this.props.defaultActiveTabIndex || 0,
}

componentDidMount () {
const { defaultActiveTabIndex } = this.props

if (defaultActiveTabIndex) {
this.setState({ activeTabIndex: defaultActiveTabIndex })
}
}

handleTabClick (tabIndex) {
const { activeTabIndex } = this.state

if (tabIndex !== activeTabIndex) {
this.setState({
activeTabIndex: tabIndex,
})
}
handleTabClick (activeTabIndex) {
this.setState({ activeTabIndex })
}

renderTabs () {
Expand All @@ -58,12 +44,12 @@ export default class PageContainer extends Component {

const numberOfTabs = React.Children.count(tabsComponent.props.children)

return React.Children.map(tabsComponent.props.children, (child, index) => {
return React.Children.map(tabsComponent.props.children, (child, tabIndex) => {
return child && React.cloneElement(child, {
onClick: index => this.handleTabClick(index),
tabIndex: index,
isActive: numberOfTabs > 1 && index === this.state.activeTabIndex,
key: index,
tabIndex,
isActive: numberOfTabs > 1 && tabIndex === this.state.activeTabIndex,
key: tabIndex,
className: 'page-container__tab',
activeClassName: 'page-container__tab--selected',
})
Expand All @@ -83,13 +69,12 @@ export default class PageContainer extends Component {
renderContent () {
const { contentComponent, tabsComponent } = this.props

switch (true) {
case Boolean(contentComponent):
return contentComponent
case Boolean(tabsComponent):
return this.renderActiveTabContent()
default:
return null
if (contentComponent) {
return contentComponent
} else if (tabsComponent) {
return this.renderActiveTabContent()
} else {
return null
}
}

Expand Down
9 changes: 7 additions & 2 deletions ui/app/components/tabs/tab/tab.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const Tab = props => {
return (
<li
className={classnames(
className || 'tab',
isActive && (activeClassName || 'tab--active'),
className,
{ [activeClassName]: isActive },
)}
onClick={event => {
event.preventDefault()
Expand All @@ -30,4 +30,9 @@ Tab.propTypes = {
activeClassName: PropTypes.string,
}

Tab.defaultProps = {
className: 'tab',
activeClassName: 'tab--active',
}

export default Tab

0 comments on commit d7d141c

Please sign in to comment.