2018-07-21 00:42:28 +02:00
|
|
|
import React, { PureComponent } from 'react'
|
2018-04-07 00:29:51 +02:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
2018-04-11 00:28:52 +02:00
|
|
|
import PageContainerHeader from './page-container-header'
|
|
|
|
import PageContainerFooter from './page-container-footer'
|
|
|
|
|
2018-07-21 00:42:28 +02:00
|
|
|
export default class PageContainer extends PureComponent {
|
2018-04-07 00:29:51 +02:00
|
|
|
static propTypes = {
|
2018-04-11 00:28:52 +02:00
|
|
|
// PageContainerHeader props
|
2018-07-19 02:47:01 +02:00
|
|
|
backButtonString: PropTypes.string,
|
|
|
|
backButtonStyles: PropTypes.object,
|
|
|
|
onBackButtonClick: PropTypes.func,
|
2018-04-11 00:28:52 +02:00
|
|
|
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,
|
2018-04-11 00:28:52 +02:00
|
|
|
// Content props
|
2018-07-19 02:47:01 +02:00
|
|
|
contentComponent: PropTypes.node,
|
2018-04-11 00:28:52 +02:00
|
|
|
// PageContainerFooter props
|
|
|
|
cancelText: PropTypes.string,
|
2018-07-19 02:47:01 +02:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onCancel: PropTypes.func,
|
2018-04-11 00:28:52 +02:00
|
|
|
onSubmit: PropTypes.func,
|
|
|
|
submitText: PropTypes.string,
|
2018-07-19 02:47:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
2018-07-21 00:42:28 +02:00
|
|
|
activeTabIndex: this.props.defaultActiveTabIndex || 0,
|
2018-07-19 02:47:01 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:42:28 +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)
|
|
|
|
|
2018-07-21 00:42:28 +02:00
|
|
|
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),
|
2018-07-21 00:42:28 +02:00
|
|
|
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
|
|
|
|
|
2018-07-21 00:42:28 +02:00
|
|
|
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 () {
|
2018-04-11 00:28:52 +02:00
|
|
|
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">
|
2018-04-11 00:28:52 +02:00
|
|
|
<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()}
|
2018-04-11 00:28:52 +02:00
|
|
|
/>
|
|
|
|
<div className="page-container__content">
|
2018-07-19 02:47:01 +02:00
|
|
|
{ this.renderContent() }
|
2018-04-11 00:28:52 +02:00
|
|
|
</div>
|
|
|
|
<PageContainerFooter
|
|
|
|
onCancel={onCancel}
|
|
|
|
cancelText={cancelText}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
submitText={submitText}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
2018-04-07 00:29:51 +02:00
|
|
|
</div>
|
2018-04-11 00:28:52 +02:00
|
|
|
)
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
}
|