import {GlobalVariable} from "@gongt/ts-stl-library/pattern/global-page-data"; import * as PropTypes from "prop-types"; import * as React from "react"; import {WrapComponent} from "./render"; export interface PropsType { global: GlobalVariable; } export interface GlobalContextContent { global: GlobalVariable; } export interface GlobalContext { context: GlobalContextContent; } export function WithGlobalContext>(component: T): T { if (!component.contextTypes) { component.contextTypes = {}; } if (!component.contextTypes.global) { component.contextTypes.global = PropTypes.object; } return component; } @WithGlobalContext export class ReactEmptyWrapper extends React.Component { state: {subProps: any} = {subProps: null}; static propTypes = { Component: PropTypes.instanceOf(React.Component).isRequired, componentName: PropTypes.string, props: PropTypes.func, }; constructor(props, context) { super(props, context); this.state.subProps = this.props.props(this.context.global); } componentWillReceiveProps(nextProps, nextContext) { this.setState({ subProps: this.props.props(this.context.global), }); } render() { const Cm = this.props.Component; if (!Cm) { return

ReactEmptyWrapper: Invalid Child Component: {this.props.componentName}.

{React.Children.only(this.props.children)}
; } console.log('ReactEmptyWrapper render(): ', Cm.displayName || Cm.name); return {React.Children.only(this.props.children)} ; } } export class GlobalContextProvider extends React.Component { static childContextTypes: React.ValidationMap = { global: PropTypes.object, }; getChildContext(): GlobalContextContent { return { global: this.props.global, }; } render() { return React.Children.only(this.props.children); } }