PopinFixedContent.jsx 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React from 'react'
  2. import classnames from 'classnames'
  3. import PropTypes from 'prop-types'
  4. class PopinFixedContent extends React.Component {
  5. constructor (props) {
  6. super(props)
  7. this.state = {
  8. rightPartOpen: false
  9. }
  10. }
  11. componentDidMount () {
  12. if (this.props.showRightPartOnLoad) this.setState({rightPartOpen: true})
  13. }
  14. handleToggleRightPart = () => {
  15. this.setState(prev => ({rightPartOpen: !prev.rightPartOpen}))
  16. }
  17. render () {
  18. return this.props.children.length === 2
  19. ? (
  20. <div className={classnames(
  21. 'wsContentGeneric__content',
  22. `${this.props.customClass}__content`,
  23. {'rightPartOpen': this.state.rightPartOpen, 'rightPartClose': !this.state.rightPartOpen}
  24. )}>
  25. <div className={classnames('wsContentGeneric__content__left', `${this.props.customClass}__content__left`)}>
  26. {this.props.children[0]}
  27. </div>
  28. <div className={classnames('wsContentGeneric__content__right', `${this.props.customClass}__content__right`)}>
  29. {React.cloneElement(this.props.children[1], {
  30. toggleRightPart: this.handleToggleRightPart,
  31. rightPartOpen: this.state.rightPartOpen
  32. })}
  33. </div>
  34. </div>
  35. )
  36. : (
  37. <div className={classnames('wsContentGeneric__content', `${this.props.customClass}__content`)}>
  38. {this.props.children}
  39. </div>
  40. )
  41. }
  42. }
  43. export default PopinFixedContent
  44. PopinFixedContent.propTypes = {
  45. customClass: PropTypes.string,
  46. children: (props, propName, componentName) => {
  47. if (Array.isArray(props) && props.length !== 2) {
  48. return new Error(`PropType Error: ${componentName} must have 1 or 2 children.`)
  49. } else if (typeof props !== 'object') {
  50. return new Error(`PropType Error: childrens of ${componentName} must have 1 or 2 children.`)
  51. }
  52. }
  53. }