1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 03:20:23 +01:00
metamask-extension/ui/app/components/page-container/page-container.component.js

123 lines
3.1 KiB
JavaScript
Raw Normal View History

import React, { PureComponent } from 'react'
2018-04-07 00:29:51 +02:00
import PropTypes from 'prop-types'
import PageContainerHeader from './page-container-header'
import PageContainerFooter from './page-container-footer'
export default class PageContainer extends PureComponent {
2018-04-07 00:29:51 +02:00
static propTypes = {
// PageContainerHeader props
2018-07-19 02:47:01 +02:00
backButtonString: PropTypes.string,
backButtonStyles: PropTypes.object,
onBackButtonClick: PropTypes.func,
onClose: PropTypes.func,
showBackButton: PropTypes.bool,
2018-07-19 02:47:01 +02:00
subtitle: PropTypes.string,
title: PropTypes.string.isRequired,
// Tabs-related props
defaultActiveTabIndex: PropTypes.number,
tabsComponent: PropTypes.node,
// Content props
2018-07-19 02:47:01 +02:00
contentComponent: PropTypes.node,
// PageContainerFooter props
cancelText: PropTypes.string,
2018-07-19 02:47:01 +02:00
disabled: PropTypes.bool,
onCancel: PropTypes.func,
onSubmit: PropTypes.func,
submitText: PropTypes.string,
2018-07-19 02:47:01 +02:00
}
state = {
activeTabIndex: this.props.defaultActiveTabIndex || 0,
2018-07-19 02:47:01 +02:00
}
handleTabClick (activeTabIndex) {
this.setState({ activeTabIndex })
2018-07-19 02:47:01 +02:00
}
renderTabs () {
const { tabsComponent } = this.props
if (!tabsComponent) {
return
}
const numberOfTabs = React.Children.count(tabsComponent.props.children)
return React.Children.map(tabsComponent.props.children, (child, tabIndex) => {
2018-07-19 02:47:01 +02:00
return child && React.cloneElement(child, {
onClick: index => this.handleTabClick(index),
tabIndex,
isActive: numberOfTabs > 1 && tabIndex === this.state.activeTabIndex,
key: tabIndex,
2018-07-19 02:47:01 +02:00
className: 'page-container__tab',
activeClassName: 'page-container__tab--selected',
})
})
}
renderActiveTabContent () {
const { tabsComponent } = this.props
const { children } = tabsComponent.props
const { activeTabIndex } = this.state
return children[activeTabIndex]
? children[activeTabIndex].props.children
: children.props.children
}
renderContent () {
const { contentComponent, tabsComponent } = this.props
if (contentComponent) {
return contentComponent
} else if (tabsComponent) {
return this.renderActiveTabContent()
} else {
return null
2018-07-19 02:47:01 +02:00
}
}
2018-04-07 00:29:51 +02:00
render () {
const {
title,
subtitle,
onClose,
showBackButton,
onBackButtonClick,
backButtonStyles,
backButtonString,
onCancel,
cancelText,
onSubmit,
submitText,
disabled,
} = this.props
2018-04-07 00:29:51 +02:00
return (
<div className="page-container">
<PageContainerHeader
title={title}
subtitle={subtitle}
onClose={onClose}
showBackButton={showBackButton}
onBackButtonClick={onBackButtonClick}
backButtonStyles={backButtonStyles}
backButtonString={backButtonString}
2018-07-19 02:47:01 +02:00
tabs={this.renderTabs()}
/>
<div className="page-container__content">
2018-07-19 02:47:01 +02:00
{ this.renderContent() }
</div>
<PageContainerFooter
onCancel={onCancel}
cancelText={cancelText}
onSubmit={onSubmit}
submitText={submitText}
disabled={disabled}
/>
2018-04-07 00:29:51 +02:00
</div>
)
2018-04-07 00:29:51 +02:00
}
}