All files / ima/page/renderer ViewAdapter.js

22.22% Statements 2/9
100% Branches 0/0
0% Functions 0/5
22.22% Lines 2/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74        5x                                                                                                                                       5x  
import PropTypes from 'prop-types';
import React from 'react';
import ns from '../../namespace';
 
ns.namespace('ima.page.renderer');
 
/**
 * An adapter component providing the current page controller's state to the
 * page view component through its properties.
 */
export default class ViewAdapter extends React.Component {
  /**
	 * @inheritdoc
	 * @return {{$Utils: function(*): ?Error}}
	 */
  static get childContextTypes() {
    return {
      $Utils: PropTypes.object.isRequired
    };
  }
 
  /**
	 * Initializes the adapter component.
	 *
	 * @param {{
	 *          state: Object<string, *>,
	 *          view: function(new:React.Component, Object<string, *>)
	 *        }} props Component properties, containing the actual page view
	 *        and the initial page state to pass to the view.
	 */
  constructor(props) {
    super(props.props);
 
    /**
		 * The current page state as provided by the controller.
		 *
		 * @type {Object<string, *>}
		 */
    this.state = props.state;
 
    /**
		 * The actual page view to render.
		 *
		 * @type {function(new:React.Component, Object<string, *>)}
		 */
    this._view = props.view;
  }
 
  /**
	 * @inheritdoc
	 */
  componentWillReceiveProps(newProps) {
    this.setState(newProps.state);
  }
 
  /**
	 * @inheritdoc
	 */
  render() {
    return React.createElement(this._view, this.state);
  }
 
  /**
	 * @inheritdoc
	 */
  getChildContext() {
    return {
      $Utils: this.props.$Utils
    };
  }
}
 
ns.ima.page.renderer.ViewAdapter = ViewAdapter;